sascha@2357: package de.intevation.flys.utils; sascha@2357: sascha@2357: /** sascha@2357: * Copyright (c) 2006 by Intevation GmbH sascha@2357: * sascha@2357: * @author Sascha L. Teichmann (teichmann@intevation.de) sascha@2357: * sascha@2357: * This program is free software under the LGPL (>=v2.1) sascha@2357: * Read the file LGPL coming with FLYS for details. sascha@2357: */ sascha@2357: sascha@2357: import java.util.Date; sascha@2357: import java.util.Calendar; sascha@2357: sascha@2357: import java.util.regex.Pattern; sascha@2357: import java.util.regex.Matcher; sascha@2357: sascha@2357: public final class DateGuesser { sascha@2357: public static final String [] MONTH = { sascha@2357: "jan", "feb", "mrz", "apr", "mai", "jun", sascha@2357: "jul", "aug", "sep", "okt", "nov", "dez" sascha@2357: }; sascha@2357: sascha@2357: public static final int guessMonth(String s) { sascha@2357: s = s.toLowerCase(); sascha@2357: for (int i = 0; i < MONTH.length; ++i) sascha@2357: if (MONTH[i].equals(s)) { sascha@2357: return i; sascha@2357: } sascha@2357: return -1; sascha@2357: } sascha@2357: sascha@2357: public static final Pattern YYYY_MM_DD = sascha@2357: Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})$"); sascha@2357: sascha@2357: public static final Pattern DD_MM_YYYY = sascha@2357: Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})$"); sascha@2357: sascha@2357: public static final Pattern MMM_YYYY = sascha@2357: Pattern.compile("^(\\d{0,2})\\.?(\\w{3})\\.?(\\d{2,4})$"); sascha@2357: sascha@2357: public static final Pattern GARBAGE_YYYY = sascha@2357: Pattern.compile("^\\D*(\\d{2,4})$"); sascha@2357: sascha@2357: public static final Pattern YYYY_MM_DDThh_mm = sascha@2357: Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2})$"); sascha@2357: sascha@2357: public static final Pattern YYYY_MM_DDThh_mm_ss = sascha@2357: Pattern.compile("^(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})$"); sascha@2357: sascha@2357: public static final Pattern DD_MM_YYYYThh_mm = sascha@2357: Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2})$"); sascha@2357: sascha@2357: public static final Pattern DD_MM_YYYYThh_mm_ss = sascha@2357: Pattern.compile("^(\\d{1,2})\\.(\\d{1,2})\\.(\\d{2,4})T(\\d{1,2}):(\\d{2}):(\\d{2})$"); sascha@2357: sascha@2357: private DateGuesser() { sascha@2357: } sascha@2357: sascha@2357: public static final int calendarMonth(String month) { sascha@2357: return calendarMonth(Integer.parseInt(month)); sascha@2357: } sascha@2357: sascha@2357: public static final int calendarMonth(int month) { sascha@2357: return Math.max(Math.min(month-1, 11), 0); sascha@2357: } sascha@2357: sascha@2357: public static Date guessDate(String s) { sascha@2357: if (s == null || (s = s.trim()).length() == 0) { sascha@2357: throw new IllegalArgumentException(); sascha@2357: } sascha@2357: sascha@2357: Matcher m; sascha@2357: sascha@2357: m = YYYY_MM_DD.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(1); sascha@2357: String month = m.group(2); sascha@2357: String day = m.group(3); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year), sascha@2357: calendarMonth(month), sascha@2357: Integer.parseInt(day), sascha@2357: 12, 0, 0); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: m = DD_MM_YYYY.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(3); sascha@2357: String month = m.group(2); sascha@2357: String day = m.group(1); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), sascha@2357: calendarMonth(month), sascha@2357: Integer.parseInt(m.group(1)), sascha@2357: 12, 0, 0); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: m = MMM_YYYY.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: int month = guessMonth(m.group(2)); sascha@2357: if (month >= 0) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(3); sascha@2357: String day = m.group(1); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), sascha@2357: month, sascha@2357: day.length() == 0 ? 15 : Integer.parseInt(day), sascha@2357: 12, 0, 0); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: } sascha@2357: sascha@2357: m = YYYY_MM_DDThh_mm.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(1); sascha@2357: String month = m.group(2); sascha@2357: String day = m.group(3); sascha@2357: String hour = m.group(4); sascha@2357: String minute = m.group(5); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year), sascha@2357: calendarMonth(month), sascha@2357: Integer.parseInt(day), sascha@2357: Integer.parseInt(hour), sascha@2357: Integer.parseInt(minute), sascha@2357: 0 sascha@2357: ); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: m = YYYY_MM_DDThh_mm_ss.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(1); sascha@2357: String month = m.group(2); sascha@2357: String day = m.group(3); sascha@2357: String hour = m.group(4); sascha@2357: String minute = m.group(5); sascha@2357: String second = m.group(6); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year), sascha@2357: calendarMonth(month), sascha@2357: Integer.parseInt(day), sascha@2357: Integer.parseInt(hour), sascha@2357: Integer.parseInt(minute), sascha@2357: Integer.parseInt(second) sascha@2357: ); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: m = DD_MM_YYYYThh_mm.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(3); sascha@2357: String month = m.group(2); sascha@2357: String day = m.group(1); sascha@2357: String hour = m.group(4); sascha@2357: String minute = m.group(5); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), sascha@2357: calendarMonth(month), sascha@2357: Integer.parseInt(day), sascha@2357: Integer.parseInt(hour), sascha@2357: Integer.parseInt(minute), sascha@2357: 0 sascha@2357: ); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: m = DD_MM_YYYYThh_mm_ss.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(3); sascha@2357: String month = m.group(2); sascha@2357: String day = m.group(1); sascha@2357: String hour = m.group(4); sascha@2357: String minute = m.group(5); sascha@2357: String second = m.group(6); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), sascha@2357: calendarMonth(month), sascha@2357: Integer.parseInt(day), sascha@2357: Integer.parseInt(hour), sascha@2357: Integer.parseInt(minute), sascha@2357: Integer.parseInt(second) sascha@2357: ); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: m = GARBAGE_YYYY.matcher(s); sascha@2357: sascha@2357: if (m.matches()) { sascha@2357: Calendar cal = Calendar.getInstance(); sascha@2357: String year = m.group(1); sascha@2357: cal.set( sascha@2357: Integer.parseInt(year) + (year.length() == 2 ? 1900 : 0), sascha@2357: 5, // month sascha@2357: 15, // day sascha@2357: 12, 0, 0); sascha@2357: return cal.getTime(); sascha@2357: } sascha@2357: sascha@2357: throw new IllegalArgumentException(); sascha@2357: } sascha@2357: sascha@2357: public static void main(String [] args) { sascha@2357: for (int i = 0; i < args.length; ++i) { sascha@2357: System.out.println(args[i] + ": " + guessDate(args[i])); sascha@2357: } sascha@2357: } sascha@2357: } sascha@2357: // end of file