Here you can find the source of toDay(final String dateString)
Parameter | Description |
---|---|
dateString | a formatted date string |
public static Date toDay(final String dateString)
//package com.java2s; //License from project: Apache License import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; public class Main { /** day formats to be tested */ private static final List<String> DAY_FORMATS = new ArrayList<String>(); private static final Map<Locale, SimpleDateFormat[]> DAY_PATTERNS = new HashMap<Locale, SimpleDateFormat[]>(); /**// w w w. j a va 2s . com * Tries to interpret a given string as a day * * This routines assumes that all date parts of a day are present, time components are ignored. 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: * * <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 */ public static Date toDay(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 (!DAY_PATTERNS.containsKey(locale)) { // create appropriate date patterns/formatters for the given locale List<SimpleDateFormat> patterns = new ArrayList<SimpleDateFormat>(); for (String pattern : DAY_FORMATS) { patterns.add(new SimpleDateFormat(pattern, locale)); } DAY_PATTERNS.put(locale, patterns.toArray(new SimpleDateFormat[0])); } for (SimpleDateFormat pattern : DAY_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; } }