The incredible java time machine

date, java, simpledateformat

Solution

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.

Problem

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 >>> ???? ```

Original source