Thiago Moraes
Thiago Moraes

Reputation: 617

Get LocalDateTime from a DateTimeObject

I'm having problems with getting the local time using Joda Time library. Here's a simple example of what I'm trying to do.

package simple;

import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class TestJodaLocalTime {

    public static void main(String[] args) {
        //time in UTC standard
        DateTime processingDate = DateTime.now();
        //local time
        LocalDateTime localDateTime = processingDate.
                withZone(DateTimeZone.getDefault()).toLocalDateTime();

        DateTimeFormatter fmtDate = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTimeFormatter fmtHour = DateTimeFormat.forPattern("HH:mm:ss");

        System.out.println("Date: " + fmtDate.print(processingDate));
        System.out.println("Time: " + fmtHour.withZone(DateTimeZone.UTC).
                print(processingDate));
        System.out.println("Local date: " + fmtDate.print(localDateTime));
        System.out.println("Local time: " + fmtHour.print(localDateTime));

    }

}

I was expecting that, when printing the local time, I would get the time on the timezone from where my application is running, but I got an identical time to the UTC DateTime.

Date: 06/12/2012
Time: 12:55:49
Local date: 06/12/2012
Local time: 12:55:49

What did I missed here?

Upvotes: 2

Views: 8807

Answers (2)

Andrii Polunin
Andrii Polunin

Reputation: 1306

You can try the following (I'm in GMT+2:00 time zone):

public class TestJodaLocalTime {

    public static void main(String[] args) {
        DateTime utcNow = DateTime.now(DateTimeZone.UTC);
        DateTime localNow = utcNow.withZone(DateTimeZone.getDefault());

        DateTimeFormatter fmt = ISODateTimeFormat.dateHourMinuteSecond();

        System.out.println("UTC   now: " + fmt.print(utcNow));
        System.out.println("Local now: " + fmt.print(localNow));
    }

}

Output:

UTC   now: 2012-12-07T17:43:43
Local now: 2012-12-07T19:43:43

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1499770

This comment may be part of the problem:

//time in UTC standard
DateTime processingDate = DateTime.now();

No, DateTime.now() returns the current time in the default time zone. So when you later use withZone(DateTimeZone.getDefault()) that's a no-op.

However, I'd still have expected fmtHour.withZone(DateTimeZone.UTC).print(processingDate) to convert to UTC.

Indeed, that's what I get when I manually set the default time zone to something other than UTC:

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        DateTimeZone pacific = DateTimeZone.forID("America/Los_Angeles");
        DateTimeZone.setDefault(pacific);

        DateTime processingDate = DateTime.now();
        //local time
        LocalDateTime localDateTime = processingDate.
            withZone(DateTimeZone.getDefault()).toLocalDateTime();

        DateTimeFormatter fmtDate = DateTimeFormat.forPattern("dd/MM/yyyy");
        DateTimeFormatter fmtHour = DateTimeFormat.forPattern("HH:mm:ss");

        System.out.println("Date: " + fmtDate.print(processingDate));
        System.out.println("Time: " + fmtHour.withZone(DateTimeZone.UTC).
                           print(processingDate));
        System.out.println("Local date: " + fmtDate.print(localDateTime));
        System.out.println("Local time: " + fmtHour.print(localDateTime));
    }
}

Output:

Date: 06/12/2012
Time: 13:19:35
Local date: 06/12/2012
Local time: 05:19:35

Are you sure the problem isn't just that your default time zone is UTC?

Upvotes: 1

Related Questions