Java tutorial
//package com.java2s; //License from project: Open Source License import android.util.Log; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class Main { private static final String TAG = "MessageBeast_AnnotationUtility"; private static SimpleDateFormat mIso8601Format; private static SimpleDateFormat mIso8601WithMillisFormat; /** * Get a Date object corresponding to the provided ISO 8601 string * * @param date the date string * @return a Date object corresponding to the provided ISO 8601 string, or null * if the string cannot be parsed. */ public static Date getDateFromIso8601String(String date) { initFormatter(); try { return mIso8601WithMillisFormat.parse(date); } catch (ParseException e) { try { return mIso8601Format.parse(date); } catch (ParseException e1) { Log.e(TAG, e1.getMessage(), e1); } } return null; } private static void initFormatter() { if (mIso8601WithMillisFormat == null) { mIso8601WithMillisFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); mIso8601WithMillisFormat.setTimeZone(TimeZone.getTimeZone("UTC")); mIso8601Format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); mIso8601Format.setTimeZone(TimeZone.getTimeZone("UTC")); } } }