Here you can find the source of decode(String timestamp, Calendar calendar)
Parameter | Description |
---|---|
timestamp | The encoded timestamp. |
calendar | The instance to decode into and returnt, may be null. If the timestamp does not specify a timezone, the zone in this instance will be used. |
public static Calendar decode(String timestamp, Calendar calendar)
//package com.java2s; //License from project: Apache License import java.util.*; public class Main { public static final int MILLIS_MINUTE = 60 * 1000; public static final int MILLIS_HOUR = 60 * MILLIS_MINUTE; private static final Map<String, TimeZone> timezones = new HashMap<String, TimeZone>(); /**//from w w w .j a v a 2s . c o m * Converts a DSA encoded timestamp into a Java Calendar. DSA encoding is based * on ISO 8601 but allows for an unspecified timezone. * @param timestamp The encoded timestamp. * @param calendar The instance to decode into and returnt, may be null. If the timestamp does * not specify a timezone, the zone in this instance will be used. */ public static Calendar decode(String timestamp, Calendar calendar) { if (calendar == null) { calendar = Calendar.getInstance(); } try { char[] chars = timestamp.toCharArray(); int idx = 0; int year = convertDigits(chars[idx++], chars[idx++], chars[idx++], chars[idx++]); validateChar(chars[idx++], '-'); int month = convertDigits(chars[idx++], chars[idx++]) - 1; validateChar(chars[idx++], '-'); int day = convertDigits(chars[idx++], chars[idx++]); validateChar(chars[idx++], 'T'); int hour = convertDigits(chars[idx++], chars[idx++]); validateChar(chars[idx++], ':'); int minute = convertDigits(chars[idx++], chars[idx++]); validateChar(chars[idx++], ':'); int second = convertDigits(chars[idx++], chars[idx++]); int millis = 0; if ((chars.length > idx) && (chars[idx] == '.')) { idx++; millis = convertDigits('0', chars[idx++], chars[idx++], chars[idx++]); } // timezone offset sign if (idx < chars.length) { char sign = chars[idx++]; if (sign == 'Z') { calendar.setTimeZone(TimeZone.getTimeZone("GMT")); } else { int tzOff; if (sign != '+' && sign != '-') throw new Exception(); int hrOff = convertDigits(chars[idx++], chars[idx++]); int minOff = 0; //minutes are optional in 8601 if (idx < chars.length) { //minutes optional validateChar(chars[idx++], ':'); minOff = convertDigits(chars[idx++], chars[idx++]); } tzOff = (hrOff * MILLIS_HOUR) + (minOff * MILLIS_MINUTE); if (sign == '-') tzOff *= -1; String timeZoneName = "Offset" + tzOff; synchronized (timezones) { TimeZone timezone = timezones.get(timeZoneName); if (timezone == null) { timezone = new SimpleTimeZone(tzOff, timeZoneName); timezones.put(timeZoneName, timezone); calendar.setTimeZone(timezone); } } } } calendar.set(year, month, day, hour, minute, second); calendar.set(Calendar.MILLISECOND, millis); } catch (Exception x) { throw new IllegalArgumentException("Invalid timestamp: " + timestamp); } return calendar; } /** * Converts the characters into an int. */ private static int convertDigits(char tens, char ones) { return (toDigit(tens) * 10) + toDigit(ones); } /** * Converts the characters into an int. */ private static int convertDigits(char thousands, char hundreds, char tens, char ones) { return toDigit(thousands) * 1000 + toDigit(hundreds) * 100 + toDigit(tens) * 10 + toDigit(ones); } /** * Used for decoding timestamp, throws an IllegalStateException if the two characters are * not equal. */ private static void validateChar(char c1, char c2) { if (c1 != c2) throw new IllegalStateException(); } /** * Converts the character to a digit, throws an IllegalStateException if it isn't a * valid digit. */ private static int toDigit(char ch) { if (('0' <= ch) && (ch <= '9')) { return ch - '0'; } throw new IllegalStateException(); } }