Here you can find the source of parseDateAsUTC(String dateString)
Parameter | Description |
---|---|
dateString | the date string to parse |
public static Date parseDateAsUTC(String dateString)
//package com.java2s; /* The contents of this file are subject to the license and copyright terms * detailed in the license directory at the root of the source tree (also * available online at http://fedora-commons.org/license/). *//*from w w w. ja v a 2 s .com*/ import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { /** * Attempt to parse the given string of form: yyyy-MM-dd[THH:mm:ss[.SSS][Z]] * as a Date. If the string is not of that form, return null. * * @param dateString * the date string to parse * @return Date the date, if parse was successful; null otherwise */ public static Date parseDateAsUTC(String dateString) { if (dateString == null || dateString.length() == 0) { return null; } SimpleDateFormat formatter = new SimpleDateFormat(); formatter.setTimeZone(TimeZone.getTimeZone("UTC")); int length = dateString.length(); if (dateString.startsWith("-")) { length--; } if (dateString.endsWith("Z")) { if (length == 11) { formatter.applyPattern("yyyy-MM-dd'Z'"); } else if (length == 20) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"); } else if (length == 22) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.S'Z'"); } else if (length == 23) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SS'Z'"); } else if (length == 24) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); } } else { if (length == 10) { formatter.applyPattern("yyyy-MM-dd"); } else if (length == 19) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss"); } else if (length == 21) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.S"); } else if (length == 22) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SS"); } else if (length == 23) { formatter.applyPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"); } else if (dateString.endsWith("GMT") || dateString.endsWith("UTC")) { formatter.applyPattern("EEE, dd MMMM yyyyy HH:mm:ss z"); } } try { return formatter.parse(dateString); } catch (ParseException e) { return null; } } }