Reputation: 1336
I have the following code
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
And the alert box shows the date
09/04/2014
instead of
21/04/2013
Why?
Upvotes: 1
Views: 1315
Reputation: 64541
As others already say, it's because of your pattern. What they don't say is why it behaves that way.
When parsing 21/04/2013
as MM/dd/yyyy
, DateTimeFormat
will decompose the date as:
Month Day of month Year 21 4 2013
and it'll then adjust things to make a valid date. To do that, the Month part is truncated at 12
(so that temporary date is Dec 4th, 2013) and the remainder (21 - 12 = 9) is then added, leading to Sept. 4th 2014, which according to your format displays as 09/04/2014
.
Upvotes: 5
Reputation: 8323
You're reversing day and month.
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
Upvotes: 2
Reputation: 1751
You wanted to show 21/04/2013
but the format was MM/dd/yyyy
.
It should be dd/MM/yyyy
So change it like this:
String test = "21/04/2013";
fmt = DateTimeFormat.getFormat("dd/MM/yyyy");
Date dateTest = fmt.parse(test);
Window.alert(fmt.format(dateTest));
Upvotes: 2