Here you can find the source of iso8601ToCalendar(String s)
Parameter | Description |
---|---|
s | a parameter |
Parameter | Description |
---|---|
ParseException | if the the String can not be parsed |
public static synchronized Calendar iso8601ToCalendar(String s) throws ParseException
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; public class Main { /**// ww w.j av a 2 s . co m * Timezone is always GMT for SP web-services */ private static final TimeZone TIME_ZONE_GMT = TimeZone.getTimeZone("GMT+0"); private static final SimpleDateFormat ISO8601_DATE_FORMAT_MILLIS = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); private static final SimpleDateFormat ISO8601_DATE_FORMAT_SECS = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss'Z'"); private static final SimpleDateFormat ISO8601_DATE_FORMAT_MINS = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm'Z'"); private static final SimpleDateFormat ISO8601_DATE_FORMAT_DATE = new SimpleDateFormat("yyyy-MM-dd"); /** * Parses a String in ISO-8601 format (GMT zone) and returns an equivalent * java.util.Calendar object. * * @param s * @return a Calendar object * @throws ParseException if the the String can not be parsed */ public static synchronized Calendar iso8601ToCalendar(String s) throws ParseException { Date d = iso8601ToDate(s); Calendar c = Calendar.getInstance(TIME_ZONE_GMT); c.setTime(d); return c; } private static synchronized Date iso8601ToDate(String s) throws ParseException { Date d = null; try { d = ISO8601_DATE_FORMAT_MILLIS.parse(s); } catch (ParseException e1) { try { d = ISO8601_DATE_FORMAT_SECS.parse(s); } catch (ParseException e2) { try { d = ISO8601_DATE_FORMAT_MINS.parse(s); } catch (ParseException e3) { d = ISO8601_DATE_FORMAT_DATE.parse(s); } } } return d; } }