Java Date Long Format getLongFromDatestamp(String datestamp)

Here you can find the source of getLongFromDatestamp(String datestamp)

Description

Converts an ISO8601 UTC datastamp String of the form yyyy-MM-ddTHH:mm:ssZ to a long.

License

Educational Community License

Parameter

Parameter Description
datestamp A datestamp in UTC format.

Return

The longFromDatestamp value

Declaration

public final static long getLongFromDatestamp(String datestamp) throws ParseException 

Method Source Code


//package com.java2s;
/*/*w  w w. jav a  2  s  . c o m*/
 *  License and Copyright:
 *
 *  The contents of this file are subject to the Educational Community License v1.0 (the "License"); you may
 *  not use this file except in compliance with the License. You should have received a copy of the License
 *  along with this software; if not, you may obtain a copy of the License at
 *  http://www.opensource.org/licenses/ecl1.php.
 *
 *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
 *  either express or implied. See the License for the specific language governing rights and limitations
 *  under the License.
 *
 *  Copyright 2002-2009 by Digital Learning Sciences, University Corporation for Atmospheric Research (UCAR).
 *  All rights reserved.
 */

import java.util.*;
import java.text.*;

public class Main {
    /**
     *  Converts an ISO8601 UTC datastamp String of the form yyyy-MM-ddTHH:mm:ssZ to a long. See <a
     *  href="http://www.w3.org/TR/NOTE-datetime">ISO8601</a> and <a
     *  href="http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm#Dates"> OAI date info</a> for more
     *  info.
     *
     * @param  datestamp           A datestamp in UTC format.
     * @return                     The longFromDatestamp value
     * @exception  ParseException  If unable to interpret the datestamp.
     */
    public final static long getLongFromDatestamp(String datestamp) throws ParseException {
        return getDateFromDatestamp(datestamp, 0).getTime();
    }

    /**
     *  Converts an ISO8601 UTC datastamp String of the form yyyy-MM-ddTHH:mm:ssZ to a long. See <a
     *  href="http://www.w3.org/TR/NOTE-datetime">ISO8601</a> and <a
     *  href="http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm#Dates"> OAI date info</a> for more
     *  info.
     *
     * @param  datestamp           A datestamp in UTC format.
     * @param  increment           Number of seconds to increment the date, positive or negative, or 0 to leave
     *      unchanged.
     * @return                     The longFromDatestamp value
     * @exception  ParseException  If unable to interpret the datestamp.
     */
    public final static long getLongFromDatestamp(String datestamp, long increment) throws ParseException {
        return getDateFromDatestamp(datestamp, increment).getTime();
    }

    /**
     *  Converts an ISO8601 UTC datestamp String of the form yyyy-MM-ddTHH:mm:ssZ or the short form yyyy-MM-dd to a Java
     *  Date. See <a href="http://www.w3.org/TR/NOTE-datetime">ISO8601 </a> and <a
     *  href="http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm#Dates"> OAI date info</a> for more
     *  info. If the short form yyyy-MM-dd is given, this method adds the String t01:00:00z to it to produce an
     *  ISO8601 compliant date time.
     *
     * @param  datestamp           A datestamp in UTC format.
     * @return                     The dateFromDatestamp value
     * @exception  ParseException  If unable to interpret the datestamp.
     */
    public final static Date getDateFromDatestamp(String datestamp) throws ParseException {
        return getDateFromDatestamp(datestamp, 0);
    }

    /**
     *  Converts an ISO8601 UTC datastamp String of the form yyyy-MM-ddTHH:mm:ssZ or the short form yyyy-MM-dd to a Java
     *  Date. See <a href="http://www.w3.org/TR/NOTE-datetime">ISO8601 </a> and <a
     *  href="http://www.openarchives.org/OAI/2.0/openarchivesprotocol.htm#Dates"> OAI date info</a> for more
     *  info. If the short form yyyy-MM-dd is given, this method adds the String t01:00:00z to it to produce an
     *  ISO8601 compliant date time.
     *
     * @param  datestamp           A datestamp in UTC format.
     * @param  increment           Number of seconds to increment the date, positive or negative, or 0 to leave
     *      unchanged.
     * @return                     The dateFromDatestamp value
     * @exception  ParseException  If unable to interpret the datestamp.
     */
    public final static Date getDateFromDatestamp(String datestamp, long increment) throws ParseException {

        // Since we're using a Date parser, time begins at January 1, 1970, 00:00:00 GMT,
        // We want to return no matches rather than badArgument, so convert the year to 1970.
        int year = 0;
        try {
            year = Integer.parseInt(datestamp.substring(0, 4));
        } catch (Throwable e) {
            throw new ParseException("Year is malformed", 3);
        }
        if (year < 1970)
            datestamp = "1970" + datestamp.substring(4, datestamp.length());

        if (datestamp.length() == 10) {
            datestamp += "t01:00:00z";
        } else {
            datestamp = datestamp.toLowerCase();
        }

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd't'HH:mm:ss'z'");
        df.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
        Date date = df.parse(datestamp);
        if (increment != 0)
            date.setTime((date.getTime() + increment * 1000));
        //prtln("getDateFromDatestamp() returning: " + date.toString());
        return date;
    }
}

Related

  1. getLongDateString(Date date)
  2. getLongDateString(Date date, boolean includeTime)
  3. getLongDisplayDate(Date moment, TimeZone tz, Locale inLocale)
  4. getLongDisplayTime(long time)
  5. getLongFormatTime(java.util.Date date)
  6. getLongGmtDateString(Date date)
  7. getLongMillis(String date)
  8. getLongNowTime()
  9. getLongOracleDateTime(Date date)