svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.remoteclient / src / main / java / org / gvsig / remoteclient / utils / DateTime.java @ 40769
History | View | Annotate | Download (5.24 KB)
1 | 40559 | jjdelcerro | /**
|
---|---|---|---|
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 | 40435 | jjdelcerro | |
25 | package org.gvsig.remoteclient.utils; |
||
26 | import java.util.Calendar; |
||
27 | import java.util.Date; |
||
28 | import java.util.GregorianCalendar; |
||
29 | import java.util.Locale; |
||
30 | |||
31 | /**
|
||
32 | * This class contains static methods to manage Dates. It was principally
|
||
33 | * created because of some problems doing the "String do DateTime"
|
||
34 | * and the "DateTime to String" conversions.
|
||
35 | *
|
||
36 | * @author Jorge Piera Llodr� (piera_jor@gva.es)
|
||
37 | */
|
||
38 | public class DateTime { |
||
39 | |||
40 | /**
|
||
41 | * returns the current date
|
||
42 | *
|
||
43 | *
|
||
44 | * @return java.util.Date
|
||
45 | */
|
||
46 | public static Date getCurrentDate() { |
||
47 | Calendar cal = new GregorianCalendar(); |
||
48 | |||
49 | return cal.getTime();
|
||
50 | } |
||
51 | |||
52 | /**
|
||
53 | * It trnasforms one date in one String
|
||
54 | *
|
||
55 | *
|
||
56 | * @return A String
|
||
57 | * @param dtK Date
|
||
58 | * @param sFormat Date format. Example: "Y-m-d H:i:s.Z";
|
||
59 | */
|
||
60 | public static String dateToString(Date dtK, String sFormat) { |
||
61 | String sDate;
|
||
62 | int nYear;
|
||
63 | int nMonth;
|
||
64 | int nDay;
|
||
65 | int nHour;
|
||
66 | int nMinute;
|
||
67 | int nSecond;
|
||
68 | int nMS;
|
||
69 | Calendar clnK;
|
||
70 | String sf;
|
||
71 | int jc;
|
||
72 | clnK = Calendar.getInstance(Locale.US); |
||
73 | clnK.setTime(dtK); |
||
74 | nYear = clnK.get(Calendar.YEAR);
|
||
75 | nMonth = 1 + clnK.get(Calendar.MONTH); |
||
76 | nDay = clnK.get(Calendar.DAY_OF_MONTH);
|
||
77 | nHour = clnK.get(Calendar.HOUR_OF_DAY);
|
||
78 | nMinute = clnK.get(Calendar.MINUTE);
|
||
79 | nSecond = clnK.get(Calendar.SECOND);
|
||
80 | nMS = clnK.get(Calendar.MILLISECOND);
|
||
81 | sDate = "";
|
||
82 | for (jc = 0; jc < sFormat.length(); jc++) { |
||
83 | switch (sFormat.charAt(jc)) {
|
||
84 | case 'Y': |
||
85 | sDate += nYear; |
||
86 | break;
|
||
87 | case 'm': |
||
88 | sf = "" + nMonth;
|
||
89 | if (nMonth < 10) { |
||
90 | sf = "0" + sf;
|
||
91 | } |
||
92 | sDate += sf; |
||
93 | break;
|
||
94 | case 'd': |
||
95 | sf = "" + nDay;
|
||
96 | if (nDay < 10) { |
||
97 | sf = "0" + sf;
|
||
98 | } |
||
99 | sDate += sf; |
||
100 | break;
|
||
101 | case 'H': |
||
102 | sf = "" + nHour;
|
||
103 | if (nHour < 10) { |
||
104 | sf = "0" + sf;
|
||
105 | } |
||
106 | sDate += sf; |
||
107 | break;
|
||
108 | case 'i': |
||
109 | sf = "" + nMinute;
|
||
110 | if (nMinute < 10) { |
||
111 | sf = "0" + sf;
|
||
112 | } |
||
113 | sDate += sf; |
||
114 | break;
|
||
115 | case 's': |
||
116 | sf = "" + nSecond;
|
||
117 | if (nSecond < 10) { |
||
118 | sf = "0" + sf;
|
||
119 | } |
||
120 | sDate += sf; |
||
121 | break;
|
||
122 | case 'Z': |
||
123 | sf = "" + nMS;
|
||
124 | if (nMS < 10) { |
||
125 | sf = "0" + sf;
|
||
126 | } |
||
127 | sDate += sf; |
||
128 | break;
|
||
129 | default:
|
||
130 | sDate += sFormat.substring(jc, jc + 1);
|
||
131 | } |
||
132 | } |
||
133 | return sDate;
|
||
134 | } |
||
135 | |||
136 | /**
|
||
137 | * It transfoms one String in one Date
|
||
138 | *
|
||
139 | *
|
||
140 | * @return Date
|
||
141 | * @param sDate String
|
||
142 | */
|
||
143 | public static Date stringToDate(String sDate) { |
||
144 | Date dtRes;
|
||
145 | Calendar clnK;
|
||
146 | int nYear;
|
||
147 | int nMonth;
|
||
148 | int nDay;
|
||
149 | int nHour;
|
||
150 | int nMinute;
|
||
151 | int nSecond;
|
||
152 | int nMS;
|
||
153 | String sf;
|
||
154 | for (; sDate.length() < 23;) |
||
155 | sDate += "0";
|
||
156 | sf = sDate.substring(0, 4); |
||
157 | nYear = Integer.parseInt(sf);
|
||
158 | sf = sDate.substring(5, 7); |
||
159 | nMonth = Integer.parseInt(sf) - 1; |
||
160 | sf = sDate.substring(8, 10); |
||
161 | nDay = Integer.parseInt(sf);
|
||
162 | sf = sDate.substring(11, 13); |
||
163 | nHour = Integer.parseInt(sf);
|
||
164 | sf = sDate.substring(14, 16); |
||
165 | nMinute = Integer.parseInt(sf);
|
||
166 | sf = sDate.substring(17, 19); |
||
167 | nSecond = Integer.parseInt(sf);
|
||
168 | sf = sDate.substring(20, 23); |
||
169 | nMS = Integer.parseInt(sf);
|
||
170 | clnK = Calendar.getInstance(Locale.US); |
||
171 | clnK.set(nYear, nMonth, nDay, nHour, nMinute, nSecond); |
||
172 | clnK.set(Calendar.MILLISECOND, nMS);
|
||
173 | dtRes = new Date(); |
||
174 | dtRes = clnK.getTime(); |
||
175 | // sf=dateToString(dtRes,"Y-m-d H:i:s.Z");
|
||
176 | return dtRes;
|
||
177 | } |
||
178 | } |