1

I have a time in milliseconds: 1618274313.

When I convert it to time using this website: https://www.epochconverter.com/, I am getting 6:08:33 AM.

But when I use SimpleDateFormat, I am getting something different:

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
System.out.println(sdf.format(new Date(1618274313)));

I am getting output as 23:01:14.

What is the issue in my code?

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
ADITYA DIXIT
  • 141
  • 1
  • 11
  • 4
    Please don't use `Date` and `SimpleDateFormat`, they're outdated and troublesome. [Read here why](https://stackoverflow.com/q/1969442/507738). Use classes from the `java.time` package instead. – MC Emperor Apr 13 '21 at 11:37
  • Possibly related: [Java: Date from unix timestamp](https://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp) – Ole V.V. Apr 13 '21 at 15:59

4 Answers4

4

In your example, you are using time 1618274313 and you are assuming that it is in milliseconds. However, when I entered the same time on https://www.epochconverter.com/, I got below results:

Please notice the site mentions: Assuming that this timestamp is in seconds.


Now if we use that number multiplied by 1000 (1618274313000) as the input so that the site considers it in milliseconds, we get below results:

Please notice the site now mentions: Assuming that this timestamp is in milliseconds.


Now, when you will use 1618274313000 (correct time in milliseconds) in Java with SimpleDateFormat, you should get your expected result (instead of 23:01:14):

SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss", Locale.getDefault());
System.out.println(sdf.format(new Date(1618274313000)));
Viral Lalakia
  • 331
  • 2
  • 4
  • 14
2

use Instant.ofEpochSecond

long test_timestamp = 1618274313L;
        LocalDateTime triggerTime =
                LocalDateTime.ofInstant(Instant.ofEpochSecond(test_timestamp), 
                                        TimeZone.getDefault().toZoneId());  

        System.out.println(triggerTime);

it prints output as 2021-04-13T06:08:33

sanjeevRm
  • 1,541
  • 2
  • 13
  • 24
0

Assuming it is in milliseconds as you say, all you know for certain is that you have a specific duration.

Duration d = Duration.ofMillis(1618274313);
System.out.println(d);

Prints

PT449H31M14.313S

Which says it is 449 hours, 31 minutes and 14.313 seconds of duration. Without knowing the epoch of this duration and any applicable zone offsets, it is not really possible to ascertain the specific date/time it represents. I could make lots of assumptions and provide results based on that, but more information from you would be helpful.

WJS
  • 36,363
  • 4
  • 24
  • 39
0

java.time

As Viral Lalakia already spotted, the epoch converter that you linked to, explicitly said that it assumed that the number was seconds (not milliseconds) since the epoch. The following makes the same assumption in Java. I recommend that you use java.time, the modern Java date and time API.

    ZoneId zone = ZoneId.of("Asia/Kolkata");
    
    long unixTimestamp = 1_618_274_313;
    
    Instant when = Instant.ofEpochSecond(unixTimestamp);
    ZonedDateTime dateTime = when.atZone(zone);
    
    System.out.println(dateTime);
    System.out.println(dateTime.format(DateTimeFormatter.ISO_LOCAL_TIME));

Output is:

2021-04-13T06:08:33+05:30[Asia/Kolkata]
06:08:33

This agrees with the 6:08:33 AM that you got from the converter. And the date is today’s date. A coincidence?

If the number is indeed milliseconds (which I honestly doubt), just use Instant.ofEpochMill() instead of Instant.ofEpochSecond().

    Instant when = Instant.ofEpochMilli(unixTimestamp);
1970-01-19T23:01:14.313+05:30[Asia/Kolkata]
23:01:14.313

This in turn agrees with the result you got in Java (except that the milliseconds are also printed).

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161