source: josm/trunk/src/org/openstreetmap/josm/tools/DateUtils.java@ 6623

Last change on this file since 6623 was 6380, checked in by Don-vip, 10 years ago

update license/copyright information

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools;
3
4import java.text.ParsePosition;
5import java.text.SimpleDateFormat;
6import java.util.Calendar;
7import java.util.Date;
8import java.util.GregorianCalendar;
9import java.util.TimeZone;
10
11import javax.xml.datatype.DatatypeConfigurationException;
12import javax.xml.datatype.DatatypeFactory;
13import javax.xml.datatype.XMLGregorianCalendar;
14
15/**
16 * A static utility class dealing with parsing XML date quickly and formatting
17 * a date to the XML UTC format regardless of current locale.
18 *
19 * @author nenik
20 */
21public final class DateUtils {
22 private DateUtils() {}
23 /**
24 * A shared instance used for conversion between individual date fields
25 * and long millis time. It is guarded against conflict by the class lock.
26 * The shared instance is used because the construction, together
27 * with the timezone lookup, is very expensive.
28 */
29 private static GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
30 private static final DatatypeFactory XML_DATE;
31
32 static {
33 calendar.setTimeInMillis(0);
34
35 DatatypeFactory fact = null;
36 try {
37 fact = DatatypeFactory.newInstance();
38 } catch(DatatypeConfigurationException ce) {
39 ce.printStackTrace();
40 }
41 XML_DATE = fact;
42 }
43
44 public static synchronized Date fromString(String str) {
45 // "2007-07-25T09:26:24{Z|{+|-}01:00}"
46 if (checkLayout(str, "xxxx-xx-xxTxx:xx:xxZ") ||
47 checkLayout(str, "xxxx-xx-xxTxx:xx:xx") ||
48 checkLayout(str, "xxxx-xx-xxTxx:xx:xx+xx:00") ||
49 checkLayout(str, "xxxx-xx-xxTxx:xx:xx-xx:00")) {
50 calendar.set(
51 parsePart(str, 0, 4),
52 parsePart(str, 5, 2)-1,
53 parsePart(str, 8, 2),
54 parsePart(str, 11, 2),
55 parsePart(str, 14,2),
56 parsePart(str, 17, 2));
57
58 if (str.length() == 25) {
59 int plusHr = parsePart(str, 20, 2);
60 int mul = str.charAt(19) == '+' ? -3600000 : 3600000;
61 calendar.setTimeInMillis(calendar.getTimeInMillis()+plusHr*mul);
62 }
63
64 return calendar.getTime();
65 }
66 else if(checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxxZ") ||
67 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx") ||
68 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx+xx:00") ||
69 checkLayout(str, "xxxx-xx-xxTxx:xx:xx.xxx-xx:00")) {
70 calendar.set(
71 parsePart(str, 0, 4),
72 parsePart(str, 5, 2)-1,
73 parsePart(str, 8, 2),
74 parsePart(str, 11, 2),
75 parsePart(str, 14,2),
76 parsePart(str, 17, 2));
77 long millis = parsePart(str, 20, 3);
78 if (str.length() == 29)
79 millis += parsePart(str, 24, 2) * (str.charAt(23) == '+' ? -3600000 : 3600000);
80 calendar.setTimeInMillis(calendar.getTimeInMillis()+millis);
81
82 return calendar.getTime();
83 }
84 else
85 {
86 // example date format "18-AUG-08 13:33:03"
87 SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yy HH:mm:ss");
88 Date d = f.parse(str, new ParsePosition(0));
89 if(d != null)
90 return d;
91 }
92
93 try {
94 return XML_DATE.newXMLGregorianCalendar(str).toGregorianCalendar().getTime();
95 } catch (Exception ex) {
96 return new Date();
97 }
98 }
99
100 public static synchronized String fromDate(Date date) {
101 calendar.setTime(date);
102 XMLGregorianCalendar xgc = XML_DATE.newXMLGregorianCalendar(calendar);
103 if (calendar.get(Calendar.MILLISECOND) == 0) xgc.setFractionalSecond(null);
104 return xgc.toXMLFormat();
105 }
106
107 private static boolean checkLayout(String text, String pattern) {
108 if (text.length() != pattern.length()) return false;
109 for (int i=0; i<pattern.length(); i++) {
110 char pc = pattern.charAt(i);
111 char tc = text.charAt(i);
112 if(pc == 'x' && tc >= '0' && tc <= '9') continue;
113 else if(pc == 'x' || pc != tc) return false;
114 }
115 return true;
116 }
117
118 private static int parsePart(String str, int off, int len) {
119 return Integer.valueOf(str.substring(off, off+len));
120 }
121
122}
Note: See TracBrowser for help on using the repository browser.