0

I would need to get a way to interpret epoch time in a Date but I am having a strange behavior surely due to TimeZone...

Here is the small piece of code I'm having:

Calendar origin = Calendar.getInstance();
origin.clear();
origin.setTimeInMillis(0);
Date date_origin = new Date(origin.getTimeInMillis());
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
System.out.println("Date origin "+sdf.format(date_origin));

and the output is "Date origin 01/01/1970 12:00" whereas I'm expecting 01/01/1970 00:00

Even if I'm trying to use Calendar instead of Date

Calendar origin_retrieved = Calendar.getInstance();
origin_retrieved.setTimeInMillis(origin.getTimeInMillis());
System.out.println("Date origin from calendar "+sdf.format(origin_retrieved.getTime()));

I'm still having 12:00.... Don't know what I'm doing wrong....

tiamat
  • 879
  • 2
  • 12
  • 35
  • 1
    Please note that `Date`, `Calendar` and `SimpleDateFormat` are legacy classes and have been superseeded by the `java.time` API for multiple reasons since Java 8 onwards. – Zabuzard Aug 19 '21 at 16:05
  • 3
    Did you [read the Javadoc of `SimpleDateFormat`](https://docs.oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html), particularly around what "h" means? – Andy Turner Aug 19 '21 at 16:13
  • you're right....better to use "H"....works better now...thks – tiamat Aug 20 '21 at 08:44

0 Answers0