Java Date ISO Parse iso8601ToCalendar(String s)

Here you can find the source of iso8601ToCalendar(String s)

Description

Parses a String in ISO-8601 format (GMT zone) and returns an equivalent java.util.Calendar object.

License

Apache License

Parameter

Parameter Description
s a parameter

Exception

Parameter Description
ParseException if the the String can not be parsed

Return

a Calendar object

Declaration

public static synchronized Calendar iso8601ToCalendar(String s) throws ParseException 

Method Source Code


//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;
    }
}

Related

  1. getISODateInSeconds(String isoDate)
  2. getIsoStringDate(Date date)
  3. getISOStringFromDate(long time)
  4. iso8601(String date)
  5. iso86012date(String s)
  6. iso8601ToDate(String s)
  7. ISO8601ToSeconds(String iso8601)
  8. isoDateStringToDate(String dateString)
  9. isOneOf(String value, String... values)