2013年7月4日木曜日

InternetDateFormat for RFC3339

Java isn't supported RFC3339.

Because I made InternetDateFormat class.

But it hasn't tested very much.

If you use this class, you need a few test.


FYI:Japanese Edition

2013/07/31: Update detail.
2013/12/16: Bug fix "parseFractionalSeconds".
2017/09/18: Bug fix "parse" (Thanka comment!)

2 件のコメント:

  1. Hi, thanks for sharing your code.
    I found a little tricky situation where "magic" milliseconds are not correct when parsing a date formatted in String:

    Code
    --------
    String date = "2016-05-27T12:01:02+02:00"; // no milliseconds
    InternetDateFormat dateFormat = new InternetDateFormat(true, 3); // have 3 digits for milliseconds
    Date d = dateFormat.parse(date);
    System.out.println("before: " + date + " (" + d.getTime() + " ms)");
    System.out.println("after: " + dateFormat.format(d));

    Output (where 885 milliseconds appeared)
    -------
    before: 2016-05-27T12:01:02+02:00 (1464339662885 ms)
    after: 2016-05-27T11:01:02.885+01:00

    How to fix
    ----------
    In method "parse(String source, ParsePosition pos)"

    if (source.substring(pos.getIndex()).startsWith(".")) {
    pos.setIndex(pos.getIndex() + 1);
    parseCalendar.set(Calendar.MILLISECOND, parseFractionalSeconds(source, pos));
    }
    else { // <----------------------------------------------------------- ADD THIS
    parseCalendar.set(Calendar.MILLISECOND, 0); // <---------------- ADD THIS
    } // <---------------------------------------------------------------- ADD THIS

    Loïc, loic DOT monney @@@ hefr DOC ch

    返信削除