Java String to Time toTime(final String dateString)

Here you can find the source of toTime(final String dateString)

Description

Tries to interpret a given string as a timestamp This routines assumes that all date parts of a day are present, including time components.

License

Apache License

Parameter

Parameter Description
dateString a formatted date string

Return

null if the date couldn't be parsed, otherwise the date with time details

Declaration

public static Date toTime(final String dateString) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /** day and time formats to be tested */
    private static final List<String> TIME_FORMATS = new ArrayList<String>();
    private static final Map<Locale, SimpleDateFormat[]> TIME_PATTERNS = new HashMap<Locale, SimpleDateFormat[]>();

    /**/*from  w w w .j  a  va 2s. co m*/
     * Tries to interpret a given string as a timestamp
     *
     * This routines assumes that all date parts of a day are present, including time components. It then tries to match
     * the given string according to the default format, and then tries various international formats with a preference
     * for European date orders.
     *
     * The tested formats are presented below, missing the time format HH:mm:[ss]:
     *
     * <code>
     *   y-M-d  y-MMM-d  d-MMM-y  d.M.y  MMM d, y  MMM d,y  d/M/y  d-M-y
     * </code>
     *
     * These formats will be first tested against the default locale, and then against an English locale (Great Britain,
     * not USA, because of the formats).
     *
     * @param dateString a formatted date string
     * @return null if the date couldn't be parsed, otherwise the date with time details
     */
    public static Date toTime(final String dateString) {
        if (dateString == null || dateString.trim().length() < 6)
            return null;

        // locales to be tried
        Locale[] locales = { Locale.getDefault(), Locale.UK, Locale.GERMANY };

        // attempt to convert the given date
        for (Locale locale : locales) {
            if (!TIME_PATTERNS.containsKey(locale)) {
                // create appropriate date patterns/formatters for the given locale
                List<SimpleDateFormat> patterns = new ArrayList<SimpleDateFormat>();
                for (String pattern : TIME_FORMATS) {
                    patterns.add(new SimpleDateFormat(pattern, locale));
                }

                TIME_PATTERNS.put(locale, patterns.toArray(new SimpleDateFormat[0]));
            }

            for (SimpleDateFormat pattern : TIME_PATTERNS.get(locale)) {
                // try to convert the date string
                try {
                    Date date = toCurrentCentury(pattern.parse(dateString));
                    if (date != null)
                        return date;
                } catch (ParseException pex) {
                    // well, didn't work, so why complain
                }
            }
        }
        return null;
    }

    /**
     * Moves the date into this century using the sliding window technique if the century is amiss.
     *
     * The date will remain unmodified if a century was given, it will only be moved if the century seems to be amiss.
     * Okay, all dates in the first century of the first millenium will be modified, but we regard that as no longer
     * applicable nowadays.
     *
     * @param date the date to move into this century if the century was not given
     * @return <code>null</code> if the date was null, otherwise the date with century
     */
    public static Date toCurrentCentury(final Date date) {
        Date currentDate = date;

        if (currentDate != null) {
            // apply sliding window technique around current year
            Calendar today = Calendar.getInstance();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            int year = calendar.get(Calendar.YEAR);
            if (year < 100) {
                // no century specified, window it
                int currentYear = today.get(Calendar.YEAR) % 100;
                int century = today.get(Calendar.YEAR) - currentYear;
                year += century - ((year - currentYear > 25) ? 100 : 0);
                calendar.set(Calendar.YEAR, year);
                currentDate = calendar.getTime();
            }
        }

        return currentDate;
    }
}

Related

  1. string2DateTime(String stringDate)
  2. string2DateTimeByAutoZero(String stringDate)
  3. stringToCalendar(String stringTime)
  4. stringToCalendar(String timeStr)
  5. stringToTimeDate(String timeStr)
  6. toTime(String display)
  7. toTime(String str)