Java Date Parse getDateFromDatestamp(String datestamp)

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

Description

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.

License

Educational Community License

Parameter

Parameter Description
datestamp A datestamp in UTC format.

Return

The dateFromDatestamp value

Declaration

public final static Date getDateFromDatestamp(String datestamp) throws ParseException 

Method Source Code


//package com.java2s;
/*//from w w  w. j a  v  a 2s.  c om
 *  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 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. dateTime(String s)
  2. datetimeParse(String datetime)
  3. DateTimeParse(String DateTimeString, String LMS)
  4. getDateFromCurrentAnd24HRTime(Date current, String hr24Time)
  5. getDateFromDateAge(Date currentDate, String age)
  6. getDateFromDateTimeUTCFormat(final String dateString)
  7. getDateFromDCItemTimestamp(String dateTimeStamp)
  8. getDateFromFileName(String fileName)
  9. getDateFromFormat(Date date, String type)