Reputation: 599
I've got something strange happening in a server running Linux, while a windows machine performing the same code behaves normally.
It is happening with the following code:
public static final SimpleDateFormat sqlDateFormat = new SimpleDateFormat("Y-M-d");
Calendar cal = Calendar.getInstance();
String now = sqlDateFormat.format(cal.getTime());
System.out.println(now);
cal.add(Calendar.DAY_OF_MONTH, -4);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
String trsh = sqlDateFormat.format(cal.getTime());
System.out.println(trsh);
The output on the windows machine running:
java version "1.7.0_07"
Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)
Output:
2014-01-02
2013-12-29
The above is matching the expected result.
The output on the Linux machine running:
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b108)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b50, mixed mode)
Output:
2014-01-02
2014-12-29
This is odd isn't it? Any nice workarounds?
Upvotes: 5
Views: 670
Reputation: 10717
You have a wrong format for a year in a SimpleDateFormat. You should be using y
instead of Y
.
Upvotes: 5
Reputation: 44071
Maybe you have constructed your format object with pattern symbol Y instead of y. Y stands for year of weekdate, not the normal iso calendar year. It is locale-dependent, especially dependent on when the week starts. So the locale settings on your windows machine and the linux server might be different. Remember that java.util.Calendar IS dependent on locale, too.
Try new SimpleDateFormat("yyyy-MM-dd");
Upvotes: 7