0

Given I have such String date

val date =  "2019-01-07T13:54:00+0000"

How to parse this date to other timezone e.g "Asia/Kolkata"?

I was trying:

val zone = DateTimeZone.forID("Asia/Kolkata")

val resultMillis = ISODateTimeFormat
    .dateTimeParser()
    .withZone(zone)
    .parseDateTime(date)

But it did not worked

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
K.Os
  • 5,123
  • 8
  • 40
  • 95
  • Are you using Joda-Time? If so, please tag the question accordingly to attract the right experts. – Ole V.V. Jan 08 '19 at 08:16
  • Related: [Java: Date parsing, why do I get an error](https://stackoverflow.com/questions/48666263/java-date-parsing-why-do-i-get-an-error) – Ole V.V. Jan 08 '19 at 08:17

2 Answers2

1

It’s a two-step process: You parse into the offset or time zone that is in the string, then you convert to your desired time zone. I can write only Java code, I trust you to translate:

    DateTimeZone zone = DateTimeZone.forID("Asia/Kolkata");
    String date = "2019-01-07T13:54:00+0000";
    DateTime dt = ISODateTimeFormat.dateTimeParser()
            .parseDateTime(date)
            .withZone(zone);
    System.out.println(dt);

Output is:

2019-01-07T19:24:00.000+05:30

You were almost there. I have textually just swapped the calls to withZone and parseDateTime.

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

Out of my head comes the TimeZone class:

val date =  "2019-01-07T13:54:00+0000"
val zone = TimeZone.getTimeZone("Asia/Kolkata")

Which Calendar understands:

val calendar = Calendar.getInstance(zone)

Then a SimpleDateFormat should do:

val format = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
val simpleDateFormat = new SimpleDateFormat(format, Locale.ENGLISH);
calendar.setTime(sdf.parse(date));
shkschneider
  • 17,833
  • 13
  • 59
  • 112