Here you can find the source of parseRFC3339Date(String datestring)
Parameter | Description |
---|---|
datestring | datestring |
Parameter | Description |
---|
public static Date parseRFC3339Date(String datestring) throws java.text.ParseException
//package com.java2s; //License from project: Open Source License import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { public static final String RFC3399_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; /**//from ww w . j a v a 2s .c o m * To deal with RFC3339 Date * * @param datestring datestring * @return Date * @throws java.text.ParseException in case of error */ public static Date parseRFC3339Date(String datestring) throws java.text.ParseException { Date d = new Date(); // if there is no time zone, we don't need to do any special parsing. if (datestring.endsWith("Z")) { SimpleDateFormat s = new SimpleDateFormat(RFC3399_DATE_FORMAT);// spec for RFC3339 s.setTimeZone(TimeZone.getTimeZone("UTC")); try { d = s.parse(datestring); } catch (java.text.ParseException pe) {// try again with optional decimals s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"); s.setTimeZone(TimeZone.getTimeZone("UTC")); s.setLenient(true); d = s.parse(datestring); } return d; } // step one, split off the timezone. char timezoneSeparator; if (datestring.contains("+")) { timezoneSeparator = '+'; } else { timezoneSeparator = '-'; } String firstpart = datestring.substring(0, datestring.lastIndexOf(timezoneSeparator)); String secondpart = datestring.substring(datestring.lastIndexOf(timezoneSeparator)); // step two, remove the colon from the timezone offset secondpart = secondpart.substring(0, secondpart.indexOf(':')) + secondpart.substring(secondpart.indexOf(':') + 1); datestring = firstpart + secondpart; SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");// spec for RFC3339 s.setTimeZone(TimeZone.getTimeZone("UTC")); try { d = s.parse(datestring); } catch (java.text.ParseException pe) {// try again with optional decimals s = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");// spec for RFC3339 (with fractional seconds) s.setTimeZone(TimeZone.getTimeZone("UTC")); s.setLenient(true); d = s.parse(datestring); } return d; } }