aloy
aloy

Reputation: 53

Convert a local time to a specific country time with JodaTime

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

Upvotes: 2

Views: 723

Answers (1)

dkb
dkb

Reputation: 4596

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

Upvotes: 3

Related Questions