Java Parse Date parseDate(final String date)

Here you can find the source of parseDate(final String date)

Description

parse Date

License

Open Source License

Declaration

public static Date parseDate(final String date) 

Method Source Code

//package com.java2s;
/*//w  ww  . ja v  a  2 s .c om
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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 GNU Lesser General Public License for more details.
*
* Copyright (c) 2008 - 2009 Pentaho Corporation and Contributors.  All rights reserved.
*/

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

import java.util.Date;

public class Main {
    private static final String[] DATEFORMATS = new String[] { "yyyy-MM-dd'T'hh:mm:ss.SSS z",
            "yyyy-MM-dd'T'hh:mm:ss z", "yyyy-MM-dd'T'hh:mm:ss.SSS'Z'", "yyyy-MM-dd'T'hh:mm:ss'Z'",
            "yyyy-MM-dd'T'hh:mm:ss.SSS", "yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd zzz", "yyyy-MM-dd'Z'", "yyyy-MM-dd" };
    private static final long SECONDS = 1000;
    private static final long MINUTES = 60 * SECONDS;
    private static final long HOURS = 60 * MINUTES;

    public static Date parseDate(final String date) {
        if (date.startsWith("PT")) {
            return parseDuration(date);
        }
        final SimpleDateFormat dateFormat = new SimpleDateFormat();
        dateFormat.setLenient(false);
        for (int i = 0; i < DATEFORMATS.length; i++) {
            try {
                final String dateformat = DATEFORMATS[i];
                dateFormat.applyPattern(dateformat);
                return dateFormat.parse(date);
            } catch (ParseException e) {
                // ignore
            }
        }
        return null;
    }

    public static Date parseDuration(final String duration) {
        if (duration.startsWith("PT") == false) {
            return null;
        }

        double div = 1;
        long date = 0;
        int item = 0;
        final char[] chars = duration.toCharArray();
        for (int i = 1; i < chars.length; i++) {
            final char c = chars[i];

            if (c == 'T') {
                item = 0;
                div = 1;
                continue;
            }
            if (Character.isDigit(c)) {
                div *= 10;
                item = item * 10 + ((int) c - '0');
            } else if (c == 'H') {
                date += item * HOURS;
                item = 0;
                div = 1;
            } else if (c == 'M') {
                date += item * MINUTES;
                item = 0;
                div = 1;
            } else if (c == 'S') {
                date += item * SECONDS;
                item = 0;
                div = 1;
            } else if (c == '.') {
                div = 1;
            } else {
                return null;
            }
        }
        date += (item / div) * 1000;
        return new Date(date);
    }
}

Related

  1. parseDate(DateFormat f, String date)
  2. parseDate(final Date date)
  3. parseDate(final String date)
  4. parseDate(final String date)
  5. parseDate(final String date)
  6. parseDate(final String date, final List patterns)
  7. parseDate(final String date, final String format)
  8. parseDate(final String dateStr)
  9. parseDate(final String dateString)