Java Date Parse getDateFromISO8601(Object iso8601)

Here you can find the source of getDateFromISO8601(Object iso8601)

Description

get Date From ISO

License

MIT License

Declaration

public static Date getDateFromISO8601(Object iso8601) throws ParseException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 *  Copyright ? 2012-2015 eBay Software Foundation
 *  This program is dual licensed under the MIT and Apache 2.0 licenses.
 *  Please see LICENSE for more information.
 *******************************************************************************/

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

public class Main {
    /**//from ww  w.ja v a  2  s  .c o m
     * Parses a specific ISO 8601 extended-format date-time string, format == "yyyy-MM-dd'T'HH:mm:ss" and returns the
     * resulting Date object.
     * 
     * FIX: In order to make EPL nested calls like:
     * com.ebay.jetstream.util.DateUtil.getMillisFromISO8601(com.ebay.jetstream.epl
     * .EPLUtilities.getAttributeValue(attributes.values, 'marketplace.transaction_date')) possible, we need this guy to
     * be able to accept Object which is a string actually
     * 
     * FIX for 5827 - we have support both 'T'-separated and ' '-separated formats
     * 
     * FIX for 11878 - now supported all 4 possible formats
     * 
     * @param iso8601
     *          an extended format ISO 8601 date-time string.
     * 
     * @return the Date object for the ISO 8601 date-time string.
     * 
     * @throws ParseException
     *           if the date could not be parsed.
     */

    private static final SimpleDateFormat[] s_formats = { new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"),
            new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss'Z'"), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"),
            new SimpleDateFormat("yyyy-MM-dd' 'HH:mm:ss") };

    public static Date getDateFromISO8601(Object iso8601) throws ParseException {
        if (iso8601 == null)
            return null;
        String sdate = iso8601.toString();

        // "2009-12-04T23:29:06" -> 0 1 -> 2
        // "2009-12-04 23:29:06" -> 0 0 -> 3
        // "2009-12-04T23:29:06Z" -> 2 1 -> 0
        // "2009-12-04 23:29:06Z" -> 2 0 -> 1

        final int a = sdate.indexOf('Z') >= 0 ? 2 : 0;
        final int b = sdate.indexOf('T') >= 0 ? 1 : 0;
        SimpleDateFormat formatter = s_formats[3 - a - b];
        synchronized (formatter) {
            return formatter.parse(sdate);
        }
        // I did check this option too. It's slightly worse in terms of performance
        // and much worse in terms of memory consumption, thus synchronized option was chosen.
        // SimpleDateFormat is quite heavy guy to instantiate it every time, that's why.
        // And it's not thread safe. :(
        // return new SimpleDateFormat(s_formats[3 - a - b]).parse(sdate);
    }
}

Related

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