svn-gvsig-desktop / tags / v1_0_2_Build_902 / applications / appCatalogYNomenclatorClient / src / es / gva / cit / catalogClient / utils / DateTime.java @ 10681
History | View | Annotate | Download (5.46 KB)
1 | 3566 | jorpiell | |
---|---|---|---|
2 | 3593 | jorpiell | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
3 | *
|
||
4 | * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
||
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 2
|
||
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
19 | *
|
||
20 | * For more information, contact:
|
||
21 | *
|
||
22 | * Generalitat Valenciana
|
||
23 | * Conselleria d'Infraestructures i Transport
|
||
24 | * Av. Blasco Ib??ez, 50
|
||
25 | * 46010 VALENCIA
|
||
26 | * SPAIN
|
||
27 | *
|
||
28 | * +34 963862235
|
||
29 | * gvsig@gva.es
|
||
30 | * www.gvsig.gva.es
|
||
31 | *
|
||
32 | * or
|
||
33 | *
|
||
34 | * IVER T.I. S.A
|
||
35 | * Salamanca 50
|
||
36 | * 46005 Valencia
|
||
37 | * Spain
|
||
38 | *
|
||
39 | * +34 963163400
|
||
40 | * dac@iver.es
|
||
41 | */
|
||
42 | 3231 | jorpiell | package es.gva.cit.catalogClient.utils; |
43 | import java.util.Calendar; |
||
44 | import java.util.Date; |
||
45 | import java.util.GregorianCalendar; |
||
46 | 3286 | jorpiell | import java.util.Locale; |
47 | 3231 | jorpiell | |
48 | /**
|
||
49 | 3821 | jorpiell | * This class contains static methods to manage Dates. It was principally
|
50 | * created because of some problems doing the "String do DateTime"
|
||
51 | * and the "DateTime to String" conversions.
|
||
52 | 3566 | jorpiell | *
|
53 | 3821 | jorpiell | * @author Jorge Piera Llodr? (piera_jor@gva.es)
|
54 | 3231 | jorpiell | */
|
55 | public class DateTime { |
||
56 | 3566 | jorpiell | |
57 | /**
|
||
58 | * returns the current date
|
||
59 | *
|
||
60 | *
|
||
61 | * @return java.util.Date
|
||
62 | */
|
||
63 | public static Date getCurrentDate() { |
||
64 | 3354 | jorpiell | Calendar cal = new GregorianCalendar(); |
65 | |||
66 | return cal.getTime();
|
||
67 | 3566 | jorpiell | } |
68 | 3286 | jorpiell | |
69 | 3566 | jorpiell | /**
|
70 | * It trnasforms one date in one String
|
||
71 | *
|
||
72 | *
|
||
73 | * @return A String
|
||
74 | * @param dtK Date
|
||
75 | * @param sFormat Date format. Example: "Y-m-d H:i:s.Z";
|
||
76 | */
|
||
77 | public static String dateToString(Date dtK, String sFormat) { |
||
78 | 3354 | jorpiell | String sDate;
|
79 | int nYear;
|
||
80 | int nMonth;
|
||
81 | int nDay;
|
||
82 | int nHour;
|
||
83 | int nMinute;
|
||
84 | int nSecond;
|
||
85 | int nMS;
|
||
86 | Calendar clnK;
|
||
87 | String sf;
|
||
88 | int jc;
|
||
89 | clnK = Calendar.getInstance(Locale.US); |
||
90 | clnK.setTime(dtK); |
||
91 | nYear = clnK.get(Calendar.YEAR);
|
||
92 | nMonth = 1 + clnK.get(Calendar.MONTH); |
||
93 | nDay = clnK.get(Calendar.DAY_OF_MONTH);
|
||
94 | nHour = clnK.get(Calendar.HOUR_OF_DAY);
|
||
95 | nMinute = clnK.get(Calendar.MINUTE);
|
||
96 | nSecond = clnK.get(Calendar.SECOND);
|
||
97 | nMS = clnK.get(Calendar.MILLISECOND);
|
||
98 | sDate = "";
|
||
99 | for (jc = 0; jc < sFormat.length(); jc++) { |
||
100 | switch (sFormat.charAt(jc)) {
|
||
101 | case 'Y': |
||
102 | sDate += nYear; |
||
103 | break;
|
||
104 | case 'm': |
||
105 | sf = "" + nMonth;
|
||
106 | if (nMonth < 10) { |
||
107 | sf = "0" + sf;
|
||
108 | } |
||
109 | sDate += sf; |
||
110 | break;
|
||
111 | case 'd': |
||
112 | sf = "" + nDay;
|
||
113 | if (nDay < 10) { |
||
114 | sf = "0" + sf;
|
||
115 | } |
||
116 | sDate += sf; |
||
117 | break;
|
||
118 | case 'H': |
||
119 | sf = "" + nHour;
|
||
120 | if (nHour < 10) { |
||
121 | sf = "0" + sf;
|
||
122 | } |
||
123 | sDate += sf; |
||
124 | break;
|
||
125 | case 'i': |
||
126 | sf = "" + nMinute;
|
||
127 | if (nMinute < 10) { |
||
128 | sf = "0" + sf;
|
||
129 | } |
||
130 | sDate += sf; |
||
131 | break;
|
||
132 | case 's': |
||
133 | sf = "" + nSecond;
|
||
134 | if (nSecond < 10) { |
||
135 | sf = "0" + sf;
|
||
136 | } |
||
137 | sDate += sf; |
||
138 | break;
|
||
139 | case 'Z': |
||
140 | sf = "" + nMS;
|
||
141 | if (nMS < 10) { |
||
142 | sf = "0" + sf;
|
||
143 | } |
||
144 | sDate += sf; |
||
145 | break;
|
||
146 | default:
|
||
147 | sDate += sFormat.substring(jc, jc + 1);
|
||
148 | } |
||
149 | } |
||
150 | return sDate;
|
||
151 | 3566 | jorpiell | } |
152 | 3821 | jorpiell | |
153 | 3566 | jorpiell | /**
|
154 | * It transfoms one String in one Date
|
||
155 | *
|
||
156 | *
|
||
157 | * @return Date
|
||
158 | * @param sDate String
|
||
159 | */
|
||
160 | public static Date stringToDate(String sDate) { |
||
161 | 3354 | jorpiell | Date dtRes;
|
162 | Calendar clnK;
|
||
163 | int nYear;
|
||
164 | int nMonth;
|
||
165 | int nDay;
|
||
166 | int nHour;
|
||
167 | int nMinute;
|
||
168 | int nSecond;
|
||
169 | int nMS;
|
||
170 | String sf;
|
||
171 | for (; sDate.length() < 23;) |
||
172 | sDate += "0";
|
||
173 | sf = sDate.substring(0, 4); |
||
174 | nYear = Integer.parseInt(sf);
|
||
175 | sf = sDate.substring(5, 7); |
||
176 | nMonth = Integer.parseInt(sf) - 1; |
||
177 | sf = sDate.substring(8, 10); |
||
178 | nDay = Integer.parseInt(sf);
|
||
179 | sf = sDate.substring(11, 13); |
||
180 | nHour = Integer.parseInt(sf);
|
||
181 | sf = sDate.substring(14, 16); |
||
182 | nMinute = Integer.parseInt(sf);
|
||
183 | sf = sDate.substring(17, 19); |
||
184 | nSecond = Integer.parseInt(sf);
|
||
185 | sf = sDate.substring(20, 23); |
||
186 | nMS = Integer.parseInt(sf);
|
||
187 | clnK = Calendar.getInstance(Locale.US); |
||
188 | clnK.set(nYear, nMonth, nDay, nHour, nMinute, nSecond); |
||
189 | clnK.set(Calendar.MILLISECOND, nMS);
|
||
190 | dtRes = new Date(); |
||
191 | dtRes = clnK.getTime(); |
||
192 | // sf=dateToString(dtRes,"Y-m-d H:i:s.Z");
|
||
193 | return dtRes;
|
||
194 | 3566 | jorpiell | } |
195 | } |