0

Hi I noticed that Joda DateTime created using Zone is returning toDate as per Current System Date. I was expecting toDate to return as per Created DateTime. As per Joda API, it says toDate should return Date initialised with this datetime. I am using Joda 2.9.4 in my Maven. Any thoughts from Gurus out in SO

public class TestJodaError {

public static void main(String[] args) {
    DateTime ldt = new DateTime();
    System.out.println("Local dt: " + ldt + " toDate: " + ldt.toDate());

    String tzId = "Asia/Singapore";
    ZoneId zoneId = ZoneId.of(tzId);
    ZonedDateTime zdt = ZonedDateTime.now(zoneId);

    DateTimeZone dtZone = DateTimeZone.forID(zdt.getZone().getId());
    DateTime rdt = new DateTime(dtZone);
    System.out.println("Remote dt: " + rdt + " toDate: " + rdt.toDate());
}}

Results are

Local dt: 2019-05-17T11:33:30.333+05:30 toDate: Fri May 17 11:33:30 IST 2019
Remot dt: 2019-05-17T14:03:30.738+08:00 toDate: Fri May 17 11:33:30 IST 2019

Expected Results

Remot dt: 2019-05-17T14:03:30.738+08:00 toDate: Fri May 17 14:03:30 SGT 2019
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Anand
  • 1,845
  • 2
  • 20
  • 25
  • toDate is your custom function? I couldnot find it in Joda [DateTime](http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTime.html) – Bilbo Baggins May 17 '19 at 06:21
  • 1
    Without Zoda time, you can achieve using Java 8. – Sambit May 17 '19 at 06:30
  • @BilboBaggins perhaps it's actually a `LocalDateTime`? – Andy Turner May 17 '19 at 06:32
  • @AndyTurner Not sure, I been using Java 8, java.time and not Joda time. Was just trying to solve the question. :) – Bilbo Baggins May 17 '19 at 06:49
  • toDate Method is from Joda DateTime – Anand May 17 '19 at 07:03
  • You are losing information twice in your operation: (1) A Joda-Time `DateTime` has a UTC offset, but no time zone, so `rdt` doesn’t know that it is from Singapore, only that its offset it `+08:00`. (2) As others have said, an old-fashioned `Date` neither has got offset nor time zone, so just prints in your JVM’s default time zone regardless. – Ole V.V. May 17 '19 at 15:00

2 Answers2

3

As Andy mentioned, Date will not have any specific time zone info. If you want to use without Joda time and want to leverage Java 8, I provide below the code snippet.

LocalDateTime ldt = LocalDateTime.of(2019, Month.MAY, 17, 11, 30);
ZonedDateTime klDateTime = ldt.atZone(ZoneId.of("Asia/Kolkata"));
DateTimeFormatter indianFormat = DateTimeFormatter.ofPattern("dd MMM yyyy");

String output = klDateTime.format(indianFormat);
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Sambit
  • 7,625
  • 7
  • 34
  • 65
2

Printing a java.util.Date will always use the JVM's default time zone, because Date doesn't actually contain any time zone information.

As such, printing the result of rdt.toDate() will print in IST if ldt.toDate() also prints in IST.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • Thanks Andy, that shows why... Was under the assumption that toDate from Joda DateTime will follow accordingly. – Anand May 17 '19 at 07:02