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

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

global replacement of e.printStackTrace() by Main.error(e)

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