Java Date Parse getDateFromISO8601(String dateString)

Here you can find the source of getDateFromISO8601(String dateString)

Description

Gets a Date object from the dateString

License

Open Source License

Parameter

Parameter Description
dateString The date as a string in ISO-8601 format

Return

a Java Date object.

Declaration

public static Date getDateFromISO8601(String dateString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class Main {
    /**/*from  w w w.j  a v a 2 s  .  co m*/
     * Gets a <code>Date</code> object from the <code>dateString</code>
     * @param dateString The date as a string in ISO-8601 format
     * @return a Java <code>Date</code> object.
     */
    public static Date getDateFromISO8601(String dateString) {
        String tz = "-08:00";
        if (dateString.endsWith(":00")) {
            tz = dateString.substring(dateString.length() - 6);
            //System.out.println( "debug: tz = " + tz );
        }
        String dateNoTimeZone = dateString.substring(0,
                dateString.length() - 6);
        //System.out.println( "debug: dateNoTimeZone = " + dateNoTimeZone );
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy-MM-dd'T'HH:mm:ss");
        TimeZone timezone = TimeZone.getTimeZone("GMT" + tz);
        Calendar cal = Calendar.getInstance(timezone);
        format.setCalendar(cal);
        Date date = null;
        try {
            date = format.parse(dateNoTimeZone);
        } catch (ParseException ex) {
        }
        return date;

    }
}

Related

  1. getDateFromFormat(Date date, String type)
  2. getDateFromFormattedDate(String dateStr, String format)
  3. getDateFromFormattedTimeStamp( String formattedTimeStamp)
  4. getDateFromIRODSValue(final String irodsValue)
  5. getDateFromISO8601(Object iso8601)
  6. getDateFromISO8601(String iso8601Date)
  7. getDateFromIsoString(String dateString)
  8. getDateFromISOString(String iso)
  9. getDateFromOffset(Date prevDate, int offset)