0

Hey guys I have the date "01.01.1000 AD"(SimpleDate) as String and dd.MM.yyyy G(SimpleFormat) and need to parse it into a Standard ISO-Date in the form 1995-12-31T23:59:59Z (yyyy-MM-dd'T'hh:mm:ss'Z')

my actual code is:

public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException {
    Date date;
    if (simpleFormat.equals("long")) {
        date = new Date(Long.parseLong(simpleDate));
    } else {
        SimpleDateFormat df = new SimpleDateFormat(simpleFormat);
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        // or else testcase
        // "1964-02-24" would
        // result "1964-02-23"
        date = df.parse(simpleDate);
    }
    return getISODate(date, isoFormat);
}

Does anyone have an idea how do I do that?

Sebastian Röher
  • 417
  • 7
  • 20

4 Answers4

2

Try this:

    String string = "01.01.1000 AD";
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy GG");
    Date date = dateFormat.parse(string);

The G in the date format stands for era.

See http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

Pieter
  • 895
  • 11
  • 22
  • I still get there an exeption `java.text.ParseException: Unparseable date: "01.01.1000 AD"` – Sebastian Röher Dec 18 '13 at 12:42
  • I can reproduce your error if I set the locale like so: `SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy GG", Locale.GERMANY);`, so it looks like a locale issue. – Pieter Dec 18 '13 at 13:08
1

This will hopefully help [tricky with standard jdk, but at least possible - and JSR 310 doesn't support this feature :-( ]:

DateFormat df = new SimpleDateFormat("dd.MM.yyyy GG", Locale.US);
DateFormat iso = new SimpleDateFormat("yyyy-MM-dd");
try {
    Date d = df.parse("01.01.1000 AD");
    System.out.println(iso.format(d)); // year-of-era => 1000-01-01 (not iso!!!)

    // now let us configure gregorian/julian date change right for ISO-8601
    GregorianCalendar isoCalendar = new GregorianCalendar();
    isoCalendar.setGregorianChange(new Date(Long.MIN_VALUE));
    iso.setCalendar(isoCalendar);
    System.out.println(iso.format(d)); // proleptic iso year: 1000-01-06
} catch (ParseException ex) {
    ex.printStackTrace();
}
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • And by the way, related to historic dates there is no sense to format in ISO including hours or even milliseconds. Therefore I have intentionally left out the time part. – Meno Hochschild Dec 18 '13 at 13:24
  • And in order to avoid DST effects it is wise to set in both format objects df and iso the timezone to UTC. Good luck. – Meno Hochschild Dec 18 '13 at 13:25
  • the problem is i need it as iso-date for my Solr-Index, and Solr can only work with ISO-date if u want to search facet 8thats what i need to do) – Sebastian Röher Dec 18 '13 at 13:48
  • Now that works for me, the Problem was the wrong locale 'public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException { Date date; if (simpleFormat.equals("long")) { date = new Date(Long.parseLong(simpleDate)); } else { SimpleDateFormat df = new SimpleDateFormat(simpleFormat, Locale.US); df.setTimeZone(TimeZone.getTimeZone("UTC")); date = df.parse(simpleDate); } return getISODate(date, isoFormat); }` – Sebastian Röher Dec 18 '13 at 13:57
  • First thing to note: With Locale.US the code will successfully parse. Second: 01.01.1000 AD is correctly translated to 1000-01-06 (pure ISO). Third: Because of lack of support in java I leave out the historical detail that in most countries of Europe at that time the year did not start at first of January, but in March or April. Fourth: If you really need time part for solr-interface just set it to midnight. – Meno Hochschild Dec 18 '13 at 13:57
0

Something like this?

String date = "01.01.1000 AD";   
SimpleDateFormat parserSDF = new SimpleDateFormat("dd.mm.yyyy GG");   
System.out.println(parserSDF.parse(date));   
Eugene Kisly
  • 129
  • 12
0

Try it may be help:

public static String getISODate(String simpleDate, String simpleFormat, String isoFormat) throws ParseException {
    Date date;
    if (simpleFormat.equals("long")) {
        date = new Date(Long.parseLong(simpleDate));
    } else {
        SimpleDateFormat df = new SimpleDateFormat(simpleFormat);
        df.setTimeZone(TimeZone.getTimeZone("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
        // or else testcase
        // "1964-02-24" would
        // result "1964-02-23"
        date = df.parse(simpleDate);
    }
    return getISODate(date, isoFormat);
}
Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • yyyy-MM-dd'T'HH:mm:ss.SSSZ is not a valid timezone identifier, but a pattern string for SimpleDateFormat. – Meno Hochschild Dec 18 '13 at 12:53
  • also still `java.text.ParseException: Unparseable date: "01.01.1000 AD"` – Sebastian Röher Dec 18 '13 at 12:54
  • That is because for the timezone you entered, the moment 00:00:00 01-01-1000 AD does not exist. You will see the same error for a 1 hour period every year, if you pick a date/time instant in the hour that is skipped at the start of daylight saving time. – Mzzl Dec 19 '13 at 13:58