1 | /*
|
---|
2 | * Copyright (C) 2008 Petr Nejedly <P.Nejedly@sh.cvut.cz>
|
---|
3 | *
|
---|
4 | * This program is free software; you can redistribute it and/or modify
|
---|
5 | * it under the terms of the GNU General Public License as published by
|
---|
6 | * the Free Software Foundation; either version 2 of the License, or
|
---|
7 | * (at your option) any later version.
|
---|
8 | *
|
---|
9 | * This program is distributed in the hope that it will be useful,
|
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | * GNU General Public License for more details.
|
---|
13 |
|
---|
14 | * You should have received a copy of the GNU General Public License along
|
---|
15 | * with this program; if not, write to the Free Software Foundation, Inc.,
|
---|
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.openstreetmap.josm.tools;
|
---|
20 |
|
---|
21 | import java.text.ParsePosition;
|
---|
22 | import java.text.SimpleDateFormat;
|
---|
23 | import java.util.Calendar;
|
---|
24 | import java.util.Date;
|
---|
25 | import java.util.GregorianCalendar;
|
---|
26 | import java.util.TimeZone;
|
---|
27 |
|
---|
28 | import javax.xml.datatype.DatatypeConfigurationException;
|
---|
29 | import javax.xml.datatype.DatatypeFactory;
|
---|
30 | import javax.xml.datatype.XMLGregorianCalendar;
|
---|
31 |
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * A static utility class dealing with parsing XML date quickly and formatting
|
---|
35 | * a date to the XML UTC format regardless of current locale.
|
---|
36 | *
|
---|
37 | * @author nenik
|
---|
38 | */
|
---|
39 | public final class DateUtils {
|
---|
40 | private DateUtils() {}
|
---|
41 | /**
|
---|
42 | * A shared instance used for conversion between individual date fields
|
---|
43 | * and long millis time. It is guarded against conflict by the class lock.
|
---|
44 | * The shared instance is used because the construction, together
|
---|
45 | * with the timezone lookup, is very expensive.
|
---|
46 | */
|
---|
47 | private static GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
|
---|
48 | private static final DatatypeFactory XML_DATE;
|
---|
49 |
|
---|
50 | static {
|
---|
51 | calendar.setTimeInMillis(0);
|
---|
52 |
|
---|
53 | DatatypeFactory fact = null;
|
---|
54 | try {
|
---|
55 | fact = DatatypeFactory.newInstance();
|
---|
56 | } catch(DatatypeConfigurationException ce) {
|
---|
57 | ce.printStackTrace();
|
---|
58 | }
|
---|
59 | XML_DATE = fact;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public static synchronized Date fromString(String str) {
|
---|
63 | // "2007-07-25T09:26:24{Z|{+|-}01:00}"
|
---|
64 | if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
|
---|
65 | checkLayout(str, "xxxx-xx-xxTxx:xx:xx") ||
|
---|
66 | checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") ||
|
---|
67 | checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) {
|
---|
68 | calendar.set(
|
---|
69 | parsePart(str, 0, 4),
|
---|
70 | parsePart(str, 5, 2)-1,
|
---|
71 | parsePart(str, 8, 2),
|
---|
72 | parsePart(str, 11, 2),
|
---|
73 | parsePart(str, 14,2),
|
---|
74 | parsePart(str, 17, 2));
|
---|
75 |
|
---|
76 | if (str.length() == 25) {
|
---|
77 | int plusHr = parsePart(str, 20, 2);
|
---|
78 | int mul = str.charAt(19) == '+' ? -3600000 : 3600000;
|
---|
79 | calendar.setTimeInMillis(calendar.getTimeInMillis()+plusHr*mul);
|
---|
80 | }
|
---|
81 |
|
---|
82 | return calendar.getTime();
|
---|
83 | }
|
---|
84 | else if(checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") ||
|
---|
85 | checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") ||
|
---|
86 | checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") ||
|
---|
87 | checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) {
|
---|
88 | calendar.set(
|
---|
89 | parsePart(str, 0, 4),
|
---|
90 | parsePart(str, 5, 2)-1,
|
---|
91 | parsePart(str, 8, 2),
|
---|
92 | parsePart(str, 11, 2),
|
---|
93 | parsePart(str, 14,2),
|
---|
94 | parsePart(str, 17, 2));
|
---|
95 | long millis = parsePart(str, 20, 3);
|
---|
96 | if (str.length() == 29)
|
---|
97 | millis += parsePart(str, 24, 2) * (str.charAt(23) == '+' ? -3600000 : 3600000);
|
---|
98 | calendar.setTimeInMillis(calendar.getTimeInMillis()+millis);
|
---|
99 |
|
---|
100 | return calendar.getTime();
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | // example date format "18-AUG-08 13:33:03"
|
---|
105 | SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
|
---|
106 | Date d = f.parse(str, new ParsePosition(0));
|
---|
107 | if(d != null)
|
---|
108 | return d;
|
---|
109 | }
|
---|
110 |
|
---|
111 | try {
|
---|
112 | return XML_DATE.newXMLGregorianCalendar(str).toGregorianCalendar().getTime();
|
---|
113 | } catch (Exception ex) {
|
---|
114 | return new Date();
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | public static synchronized String fromDate(Date date) {
|
---|
119 | calendar.setTime(date);
|
---|
120 | XMLGregorianCalendar xgc = XML_DATE.newXMLGregorianCalendar(calendar);
|
---|
121 | if (calendar.get(Calendar.MILLISECOND) == 0) xgc.setFractionalSecond(null);
|
---|
122 | return xgc.toXMLFormat();
|
---|
123 | }
|
---|
124 |
|
---|
125 | private static boolean checkLayout(String text, String pattern) {
|
---|
126 | if (text.length() != pattern.length()) return false;
|
---|
127 | for (int i=0; i<pattern.length(); i++) {
|
---|
128 | char pc = pattern.charAt(i);
|
---|
129 | char tc = text.charAt(i);
|
---|
130 | if(pc == 'x' && tc >= '0' && tc <= '9') continue;
|
---|
131 | else if(pc == 'x' || pc != tc) return false;
|
---|
132 | }
|
---|
133 | return true;
|
---|
134 | }
|
---|
135 |
|
---|
136 | private static int parsePart(String str, int off, int len) {
|
---|
137 | return Integer.valueOf(str.substring(off, off+len));
|
---|
138 | }
|
---|
139 |
|
---|
140 | }
|
---|