Why if I convert a date from milliseconds to days, and then back, from days to milliseconds, this date change?
for example:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2012-06-02");
System.out.println(date);
Long dateAsDays = TimeUnit.MILLISECONDS.toDays(date.getTime());
System.out.println(
new Date(
TimeUnit.DAYS.toMillis(dateAsDays)
) );
will be printed:
Sat Jun 02 00:00:00 GMT+03:00 2012
Fri Jun 01 03:00:00 GMT+03:00 2012
How I can save the day of the month in this conversion? And why this code is not working properly?
The date becomes less accurate when you get it in days. You are in GMT+3, so 12:00 GMT is 3:00 for you. From the TimeUnit class reference: