3

What happens to my time/date using this sample code??

package date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateFormatTest
{
    public static void main(String args[]) throws ParseException
    {
        final String pattern = "dd/MM/YYYY HH:mm";
        final Locale locale = Locale.FRENCH;
        final SimpleDateFormat formatter = new SimpleDateFormat(pattern, locale);

        Date d = new Date();
        System.out.println("Today: " + d);

        String parsedDate = formatter.format(d);
        System.out.println("Today as string:" + parsedDate);

        Date d2 = formatter.parse(parsedDate);
        System.out.println("Today parsed back:" + d2);

    }
}

Output:

Today: Fri Jun 28 16:28:04 CEST 2013
Today as string:28/06/2013 16:28
Today parsed back:Mon Dec 31 16:28:00 CET 2012    >>> ????
remi
  • 3,914
  • 1
  • 19
  • 37
  • Had forgotten to put the output. Just edited the question! – remi Jun 28 '13 at 14:32
  • No riddle: I get current date, parse it as a String with the formatter, then transform this string back to a Date with the same formatter, and the date returned is different than the original one – remi Jun 28 '13 at 14:34
  • 2
    The `YYYY` should be `yyyy` – fge Jun 28 '13 at 14:34

2 Answers2

10
pattern = "dd/MM/YYYY HH:mm";

should be

pattern = "dd/MM/yyyy HH:mm";

See JavaDoc.

But note that this code as you posted does not even run on my Eclipse:

java.lang.IllegalArgumentException: Illegal pattern character 'Y'

Ah, Y is added in Java 7. But it is weekyear.

zw324
  • 26,764
  • 16
  • 85
  • 118
  • By OP's comment: *I get current date, parse it as a String with the formatter, then transform this string back to a Date with the same formatter, and the date returned is different than the original one* the `YYYY` looks like a typo from OP while writing the question and that wasn't the real problem =\ – Luiggi Mendoza Jun 28 '13 at 14:36
  • That was fast! And indded the good answer, need to wait a couple of minutes to accept it though. Thanks! – remi Jun 28 '13 at 14:36
  • @remi Glad I could help, but honestly I don't really grab the week year concept yet, either. – zw324 Jun 28 '13 at 14:42
  • I still don't get why the parsed back date is different from the original. – WilQu Jun 28 '13 at 14:45
  • @WilQu Me neither. I thought it was because `format(Date)` method is inherited from `DateFormat`, and is not overriden in `SimpleDateFormat`, but the `format(Date, StringBuffer, FieldPosition)` method which is defined in the `SimpleDateFormat` class is giving the same behavior. Further explanation might need to dig into the JDK source. – zw324 Jun 28 '13 at 14:58
  • @WilQu I'm not entirely confident I've wrapped my head around it, but from what's said in the link provided the first week of 2013 actually starts on the 31st of December, and that week and all subsequent weeks until the first week of 2014 (starting on the 30th December 2013) have the same "week year" value (2013). I guess attempting to parse a string containing a week year value ignores everything else (because there's no way to resolve both), so the resulting date is simply the first day of the first week of that year. – Anthony Grist Jun 28 '13 at 15:03
1

Little explanation, but is only a guessing, correct me if I'm wrong.

As the explanation of week year I guess that parsing the week year of 2013 (due to the wrong pattern 2013 -> YYYY ) is somehow setting the whole date to the first week year of the 2013, that is Monday 31/12/2012.

Enrichman
  • 11,157
  • 11
  • 67
  • 101