Java Date ISO Parse getISODateInSeconds(String isoDate)

Here you can find the source of getISODateInSeconds(String isoDate)

Description

Method to get an iso date in the format yyyy, yyyy-mm, or yyyy-mm-dd in GMT seconds

License

Open Source License

Parameter

Parameter Description
isoDate The isodate to convert to seconds

Return

GMT Seconds representing the given date

Declaration

public static Long getISODateInSeconds(String isoDate) 

Method Source Code

//package com.java2s;
/**/*from w  ww.java2  s  .c om*/
 * Archivists' Toolkit(TM) Copyright 2005-2007 Regents of the University of California, New York University, & Five Colleges, Inc.
 * All rights reserved.
 *
 * This software is free. You can redistribute it and / or modify it under the terms of the Educational Community License (ECL)
 * version 1.0 (http://www.opensource.org/licenses/ecl1.php)
 *
 * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ECL license for more details about permissions and limitations.
 *
 *
 * Archivists' Toolkit(TM)
 * http://www.archiviststoolkit.org
 * info@archiviststoolkit.org
 *
 *
 * Simple utility class for checking that an entered date is valid for the given database
 * and for handeling iso dates in the AT
 *
 * Created by IntelliJ IDEA.
 * User: Nathan Stevens
 * Date: Aug 28, 2008
 * Time: 10:46:58 AM
 * To change this template use File | Settings | File Templates.
 */

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

public class Main {
    /**
     * Method to get an iso date in the format yyyy, yyyy-mm, or yyyy-mm-dd in GMT seconds
     * @param isoDate The isodate to convert to seconds
     * @return GMT Seconds representing the given date 
     */
    public static Long getISODateInSeconds(String isoDate) {
        String dateString = "";
        SimpleDateFormat isodf = new SimpleDateFormat("yyyy-MM-dd");

        // first check the format of the isodate to see if anything need to be added to
        // get it to the yyyy-mm-dd format
        if (isoDate.matches("\\d{4}")) { // must just be year so add 1st month and 1st day
            dateString = isoDate + "-01-01";
        } else if (isoDate.matches("\\d{4}-\\d{2}")) { // must be yyyy-mm so add 1st day
            dateString = isoDate + "-01";
        } else if (isoDate.matches("\\d{8}")) { // must be YYYYMMDD
            dateString = isoDate;
            isodf = new SimpleDateFormat("yyyyMMdd");
        } else { // must be full date so use that
            dateString = isoDate;
        }

        // now convert to java date object and get the seconds from that
        try {
            Date testDate = isodf.parse(dateString);
            return testDate.getTime();
        } catch (ParseException pe) {
            return null;
        }
    }
}

Related

  1. getADTimeFromISO8601Date(String date)
  2. getCalendarFromISO8601String(final String timestamp, final TimeZone tz)
  3. getISODate(String date)
  4. getIsoStringDate(Date date)
  5. getISOStringFromDate(long time)
  6. iso8601(String date)
  7. iso86012date(String s)