2

I am having problem trying to figure out how to convert a person's local time to a UK time with Joda. Say, 31-01-2015 12:00:01am Washington DC time(could be any country's time) to

31-01-2015 5:00:01am london time(London time should always be the output)

DateTimeZone zone = DateTimeZone.forID("Europe/London"); DateTime dt = new DateTime(zone);

Can't seem to format that into this format Day-Month-Year hour:min:sec:a

aloy
  • 53
  • 6

1 Answers1

3

The input format is in Washington DC time and output is in London time You can have multiple zone id as input and output. Get timezone ids from here or here

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
public class Test {

public static void main(String[] args) throws Exception {

    final DateTimeZone fromTimeZone = DateTimeZone.forID("EST");
    final DateTime dateTime = new DateTime("2019-01-03T01:25:00", fromTimeZone);

    final org.joda.time.format.DateTimeFormatter outputFormatter
            = DateTimeFormat.forPattern("yyyy-MM-dd hh:mm:ss a").withZone(DateTimeZone.forID("Europe/London"));
    System.out.println("London time:" + outputFormatter.print(dateTime));

}

output:

London time:2019-01-03 06:25:00 AM

Ref

dkb
  • 4,389
  • 4
  • 36
  • 54
  • Thanks, but I need to convert from any time zone to London time. Not just from Washington DC time. Like uhm, get the current time using DateTime() or Date() then to London time. Pls add more comments on where I can change from the code. Thanks. – aloy Jan 03 '19 at 18:14
  • 1
    You can put zone id in the place of "EST" in `fromTimeZone` variable, that's it. – dkb Jan 04 '19 at 05:33
  • in order to get country timezone goto https://www.joda.org/joda-time/timezones.html – Gilian Marques Jun 07 '20 at 23:00