Here you can find the source of parseW3CDateTime(String date)
public static Date parseW3CDateTime(String date)
//package com.java2s; /**//from w w w. j av a 2 s .com * Copyright 2013-2014 Guoqiang Chen, Shanghai, China. All rights reserved. * * Email: subchen@gmail.com * URL: http://subchen.github.io/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.text.*; import java.util.Date; public class Main { private static final String[] RFC822_PATTENRS = { "EEE, dd MMM yy HH:mm:ss z", "EEE, dd MMM yy HH:mm z", "dd MMM yy HH:mm:ss z", "dd MMM yy HH:mm z", }; private static final String[] W3CDATETIME_PATTERNS = { "yyyy-MM-dd'T'HH:mm:ss.SSSz", "yyyy-MM-dd't'HH:mm:ss.SSSz", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", "yyyy-MM-dd't'HH:mm:ss.SSS'z'", "yyyy-MM-dd'T'HH:mm:ssz", "yyyy-MM-dd't'HH:mm:ssz", "yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd't'HH:mm:ss'z'", "yyyy-MM-dd'T'HH:mmz", "yyyy-MM'T'HH:mmz", "yyyy'T'HH:mmz", "yyyy-MM-dd't'HH:mmz", "yyyy-MM-dd'T'HH:mm'Z'", "yyyy-MM-dd't'HH:mm'z'", "yyyy-MM-dd", "yyyy-MM", "yyyy", }; private static final String[] STD_PATTERNS = { "yyyy-MM-dd HH:mm:ss,SSS", "yyyy-MM-dd HH:mm:ss.SSS", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM-dd", "yyyy/MM/dd HH:mm:ss,SSS", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM/dd", "yyyyMMddHHmmss", "yyyyMMdd", "hh:mm:ss,SSS", "hh:mm:ss.SSS", "hh:mm:ss", }; public static Date parseW3CDateTime(String date) { // if sDate has time on it, it injects 'GTM' before de TZ displacement // to allow the SimpleDateFormat parser to parse it properly int tIndex = date.indexOf("T"); if (tIndex > -1) { if (date.endsWith("Z")) { date = date.substring(0, date.length() - 1) + "+00:00"; } int tzdIndex = date.indexOf("+", tIndex); if (tzdIndex == -1) { tzdIndex = date.indexOf("-", tIndex); } if (tzdIndex > -1) { String pre = date.substring(0, tzdIndex); int secFraction = pre.indexOf(","); if (secFraction > -1) { pre = pre.substring(0, secFraction); } String post = date.substring(tzdIndex); date = pre + "GMT" + post; } } else { date += "T00:00GMT"; } return parseUsingPatterns(date, W3CDATETIME_PATTERNS); } public static Date parseUsingPatterns(String date, String[] patterns) { if (date == null || date.length() == 0) { return null; } date = date.trim(); for (String pattern : patterns) { SimpleDateFormat df = new SimpleDateFormat(pattern); df.setLenient(false); try { ParsePosition pp = new ParsePosition(0); Date d = df.parse(date, pp); if (d != null && pp.getIndex() == date.length()) { return d; } } catch (Exception e) { } } return null; } public static Date parse(String date) { Date d = parseUsingPatterns(date, STD_PATTERNS); if (d == null) { d = parseRFC822Date(date); } if (d == null) { d = parseW3CDateTime(date); } if (d == null) { try { d = DateFormat.getInstance().parse(date); } catch (ParseException e) { d = null; } } return d; } public static Date parseRFC822Date(String date) { int ipos = date.indexOf(" UT"); if (ipos > -1) { String pre = date.substring(0, ipos); String post = date.substring(ipos + 3); date = pre + " GMT" + post; } return parseUsingPatterns(date, RFC822_PATTENRS); } }