Java Parse Time parseTimeString(String timeString)

Here you can find the source of parseTimeString(String timeString)

Description

Parse a date/time string and return its equivalent in milliseconds (using same time coordinate system as System.currentTimeMillis()).

License

Open Source License

Parameter

Parameter Description
timeString the date/time string to parse.

Return

the number of milliseconds since 00:00:00 1970 indicated by the date/time string, or null if the input string is null or the string is not a recognizable format.

Declaration

public static Long parseTimeString(String timeString) 

Method Source Code

//package com.java2s;

import java.text.*;
import java.util.regex.Pattern;

public class Main {
    /**//  w ww . j  a  v  a 2  s.c o  m
     * Parse a date/time string and return its equivalent in milliseconds (using same time coordinate system as
     * System.currentTimeMillis()). The following formats are recognized and conform to those defined in KML version
     * 2.2: "1997", "1997-07", "1997-07-16", "1997-07-16T07:30:15Z", "1997-07-16T07:30:15+03:00" and
     * "1997-07-16T07:30:15+0300".
     *
     * @param timeString the date/time string to parse.
     *
     * @return the number of milliseconds since 00:00:00 1970 indicated by the date/time string, or null if the input
     *         string is null or the string is not a recognizable format.
     */
    public static Long parseTimeString(String timeString) {
        if (timeString == null)
            return null;

        // KML allows a hybrid time zone offset that does not contain the leading "GMT", e.g. 1997-05-10T09:30:00+03:00.
        // If the time string has this pattern, we convert it to an RFC 822 time zone so that SimpleDateFormat can
        // parse it.
        if (isKMLTimeShift(timeString)) {
            // Remove the colon from the GMT offset portion of the time string.
            timeString = timeString.trim();
            int colonPosition = timeString.length() - 3;
            String newTimeString = timeString.substring(0, colonPosition);
            timeString = newTimeString
                    + timeString.substring(colonPosition + 1,
                            timeString.length());
        }

        try {
            DateFormat df = new SimpleDateFormat(
                    "yyyy-MM-dd'T'HH:mm:sszzzzz");
            return df.parse(timeString).getTime();
        } catch (ParseException ignored) {
        }

        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            return df.parse(timeString).getTime();
        } catch (ParseException ignored) {
        }

        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            return df.parse(timeString).getTime();
        } catch (ParseException ignored) {
        }

        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM");
            return df.parse(timeString).getTime();
        } catch (ParseException ignored) {
        }

        try {
            DateFormat df = new SimpleDateFormat("yyyy");
            return df.parse(timeString).getTime();
        } catch (ParseException ignored) {
        }

        return null;
    }

    protected static boolean isKMLTimeShift(String timeString) {
        return Pattern.matches(".*[+-]+\\d\\d:\\d\\d$", timeString.trim());
    }
}

Related

  1. parseTime(String token)
  2. parseTimeConfiguration(String time)
  3. parseTimeSpec(String[] spec)
  4. parseTimeStr(String timeStr)
  5. parseTimeString(String time)
  6. parseTimezoneOffset(String value)