-3

I have Long value 1282680754000 where if I check this value in https://www.epochconverter.com/ it gives me Tuesday, August 24, 2010 8:12:34 PM

But if I use new DateTime(1282680754000).toDate() I get Wed Aug 25 01:42:34 IST 2010 (It is adding +5.30 hour)

How to get Tuesday, August 24, 2010 8:12:34 PM for 1282680754000 in java

Ganesh
  • 167
  • 1
  • 7
  • 21
  • 1
    U have to use conversion to utc – h__ Sep 27 '18 at 19:30
  • even with DateTime(timeInstance).withZone(DateTimeZone.UTC).toDate()); it givves same value – Ganesh Sep 27 '18 at 19:38
  • no it does not `System.out.println(DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.of("UTC")) .format(Instant.ofEpochMilli(1282680754000L)));` for example. This is just about playing with `DateTimeFormatterBuilder` I guess – Eugene Sep 27 '18 at 19:40
  • 3
    It's not adding 5.5 hours. It's just displaying the same moment in time differently. – Dawood ibn Kareem Sep 27 '18 at 19:41
  • 1
    On the original of this duplicate, see specifically the modern solution using *java.time* classes in the [Answer by Przemek](https://stackoverflow.com/a/34444188/642706). – Basil Bourque Sep 27 '18 at 21:49
  • 1
    Always **search Stack Overflow** before posting. You can assume any basic question about date-time has already been asked and answered. – Basil Bourque Sep 27 '18 at 21:49

3 Answers3

1

Just use

Instant.ofEpochMilli(1_282_680_754_000L)

or

Instant.ofEpochMilli(1_282_680_754_000L).atOffset(ZoneOffset.UTC)

(using java.time, the modern Java date and time API; you may consider it the successor of Joda-Time).

The latter will give you an OffsetDateTime, which you can then format into youe desired format.

What went wrong in your code?

Your code is correct. You got the correct Date. The only things are:

  • For most purposes you shouldn’t want a java.util.Date. That class is long outdated and has design problems, which was the major background for development of Joda-Time and later java.time.
  • Your Date was printed in your local time (IST, probably India Standard Time or Asia/Kolkata) where you expected UTC. A Date has got neither time zone nor offset in it. When you print it, its toString method grabs your JVM’s time zone setting and renders the time in this time zone — in your case in IST. This behaviour surprises many.

Link: All about java.util.Date on Jon Skeet’s coding blog

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
0
 String result = DateTimeFormatter.ofPattern("EEEE, MMMM dd, yyyy h:mm:ss a")
            .withZone(ZoneId.of("UTC"))
            .toFormat()
            .format(Instant.ofEpochMilli(1282680754000L));

 System.out.println(result); // Tuesday, August 24, 2010 8:12:34 PM
Eugene
  • 117,005
  • 15
  • 201
  • 306
0

Omit the toDate, you are converting it to a Date object, which its toString method uses your operating system default time zone to print its value (you can see the IST in your question).

Just for reference examine the follows:

public static void main(String[] args) {
  DateTime dateTime = new DateTime(1282680754000L, DateTimeZone.forID("GMT"));
  System.out.println(dateTime.toDate().toGMTString());
}

24 Aug 2010 20:12:34 GMT

against (my default time zone is IDT):

public static void main(String[] args) {
  DateTime dateTime = new DateTime(1282680754000L,DateTimeZone.forID("GMT"));
  System.out.println(dateTime.toDate());
}

Tue Aug 24 23:12:34 IDT 2010

Shmulik Klein
  • 3,754
  • 19
  • 34
  • FYI, the [*Joda-Time*](http://www.joda.org/joda-time/) project is now in [maintenance mode](https://en.wikipedia.org/wiki/Maintenance_mode), with the team advising migration to the [*java.time*](http://docs.oracle.com/javase/10/docs/api/java/time/package-summary.html) classes. See [Tutorial by Oracle](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Sep 27 '18 at 21:50