Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / utils / DateTime.java @ 40559

History | View | Annotate | Download (6.4 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24

    
25
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
26
*
27
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
28
*
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
*
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
*
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
42
*
43
* For more information, contact:
44
*
45
*  Generalitat Valenciana
46
*   Conselleria d'Infraestructures i Transport
47
*   Av. Blasco Ib��ez, 50
48
*   46010 VALENCIA
49
*   SPAIN
50
*
51
*      +34 963862235
52
*   gvsig@gva.es
53
*      www.gvsig.gva.es
54
*
55
*    or
56
*
57
*   IVER T.I. S.A
58
*   Salamanca 50
59
*   46005 Valencia
60
*   Spain
61
*
62
*   +34 963163400
63
*   dac@iver.es
64
*/
65
package org.gvsig.remoteclient.utils;
66
import java.util.Calendar;
67
import java.util.Date;
68
import java.util.GregorianCalendar;
69
import java.util.Locale;
70

    
71
/**
72
 * This class contains static methods to manage Dates. It was principally 
73
 * created because of some problems doing the "String do DateTime" 
74
 * and the "DateTime to String" conversions.  
75
 * 
76
 * @author Jorge Piera Llodr� (piera_jor@gva.es)
77
 */
78
public class DateTime {
79

    
80
/**
81
 * returns the current date
82
 * 
83
 * 
84
 * @return java.util.Date
85
 */
86
    public static Date getCurrentDate() {        
87
        Calendar cal = new GregorianCalendar();
88
 
89
        return cal.getTime();
90
    } 
91

    
92
/**
93
 * It trnasforms one date in one String
94
 * 
95
 * 
96
 * @return A String
97
 * @param dtK Date
98
 * @param sFormat Date format. Example: "Y-m-d H:i:s.Z";
99
 */
100
    public static String dateToString(Date dtK, String sFormat) {        
101
        String sDate;
102
        int nYear;
103
        int nMonth;
104
        int nDay;
105
        int nHour;
106
        int nMinute;
107
        int nSecond;
108
        int nMS;
109
        Calendar clnK;
110
        String sf;
111
        int jc;
112
        clnK = Calendar.getInstance(Locale.US);
113
        clnK.setTime(dtK);
114
        nYear = clnK.get(Calendar.YEAR);
115
        nMonth = 1 + clnK.get(Calendar.MONTH);
116
        nDay = clnK.get(Calendar.DAY_OF_MONTH);
117
        nHour = clnK.get(Calendar.HOUR_OF_DAY);
118
        nMinute = clnK.get(Calendar.MINUTE);
119
        nSecond = clnK.get(Calendar.SECOND);
120
        nMS = clnK.get(Calendar.MILLISECOND);
121
        sDate = "";
122
        for (jc = 0; jc < sFormat.length(); jc++) {
123
            switch (sFormat.charAt(jc)) {
124
                case 'Y':
125
                    sDate += nYear;
126
                    break;
127
                case 'm':
128
                    sf = "" + nMonth;
129
                    if (nMonth < 10) {
130
                        sf = "0" + sf;
131
                    }
132
                    sDate += sf;
133
                    break;
134
                case 'd':
135
                    sf = "" + nDay;
136
                    if (nDay < 10) {
137
                        sf = "0" + sf;
138
                    }
139
                    sDate += sf;
140
                    break;
141
                case 'H':
142
                    sf = "" + nHour;
143
                    if (nHour < 10) {
144
                        sf = "0" + sf;
145
                    }
146
                    sDate += sf;
147
                    break;
148
                case 'i':
149
                    sf = "" + nMinute;
150
                    if (nMinute < 10) {
151
                        sf = "0" + sf;
152
                    }
153
                    sDate += sf;
154
                    break;
155
                case 's':
156
                    sf = "" + nSecond;
157
                    if (nSecond < 10) {
158
                        sf = "0" + sf;
159
                    }
160
                    sDate += sf;
161
                    break;
162
                case 'Z':
163
                    sf = "" + nMS;
164
                    if (nMS < 10) {
165
                        sf = "0" + sf;
166
                    }
167
                    sDate += sf;
168
                    break;
169
                default:
170
                    sDate += sFormat.substring(jc, jc + 1);
171
            }
172
        }
173
        return sDate;
174
    } 
175
   
176
/**
177
 * It transfoms one String in one Date
178
 * 
179
 * 
180
 * @return Date
181
 * @param sDate String
182
 */
183
    public static Date stringToDate(String sDate) {        
184
        Date dtRes;
185
        Calendar clnK;
186
        int nYear;
187
        int nMonth;
188
        int nDay;
189
        int nHour;
190
        int nMinute;
191
        int nSecond;
192
        int nMS;
193
        String sf;
194
        for (; sDate.length() < 23;)
195
            sDate += "0";
196
        sf = sDate.substring(0, 4);
197
        nYear = Integer.parseInt(sf);
198
        sf = sDate.substring(5, 7);
199
        nMonth = Integer.parseInt(sf) - 1;
200
        sf = sDate.substring(8, 10);
201
        nDay = Integer.parseInt(sf);
202
        sf = sDate.substring(11, 13);
203
        nHour = Integer.parseInt(sf);
204
        sf = sDate.substring(14, 16);
205
        nMinute = Integer.parseInt(sf);
206
        sf = sDate.substring(17, 19);
207
        nSecond = Integer.parseInt(sf);
208
        sf = sDate.substring(20, 23);
209
        nMS = Integer.parseInt(sf);
210
        clnK = Calendar.getInstance(Locale.US);
211
        clnK.set(nYear, nMonth, nDay, nHour, nMinute, nSecond);
212
        clnK.set(Calendar.MILLISECOND, nMS);
213
        dtRes = new Date();
214
        dtRes = clnK.getTime();
215
        //            sf=dateToString(dtRes,"Y-m-d H:i:s.Z");
216
        return dtRes;
217
    } 
218
 }