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