Java Millisecond Current Get getMillisFromISO8601(Object iso8601)

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

Description

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

License

MIT License

Parameter

Parameter Description
iso8601 an extended format ISO 8601 date-time string.

Exception

Parameter Description
ParseException if the date could not be parsed.

Return

the Unix epoch date-time for the given ISO 8601 date-time string.

Declaration

public static Long getMillisFromISO8601(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  w w w .j a va  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") };

    /**
     * 
     * 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
     * 
     * 
     * @param iso8601
     *          an extended format ISO 8601 date-time string.
     * 
     * @return the Unix epoch date-time for the given ISO 8601 date-time string.
     * 
     * @throws ParseException
     *           if the date could not be parsed.
     */
    public static Long getMillisFromISO8601(Object iso8601) throws ParseException {
        Date iso8601Date = getDateFromISO8601(iso8601);
        return iso8601Date == null ? null : iso8601Date.getTime();
    }

    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. getMillisFromRFC822String(Object rfc822)
  2. getMillisec_old(String isoDate)
  3. getMilliSecondToTomorrow(Date date)
  4. getMillins(String yyyyMMddHHmmss)
  5. getMillisDurationString(long millis)
  6. getMillisFromYYYYMMDD(Object yyyymmdd)
  7. getMillisId()
  8. getMillisOfTowDiffDate(String p_startDate, String p_endDate)