Question

I'm developing application for mobile phone. So, after publish application will be ready for all users around the world. I need to adjust server time with local time of user. I thought maybe Joda (the library that i'm using) is able to do that. My question is how can I change server time to users local time.

Server returns "2013-04-17T07:43:45Z" as date/time. After searching Internet I could parse it to "17 Apr 2013, 07:43 AM" successfully. This is my method:

private String formatFanciedDate(String date) {
        date = date.substring(0 , date.length()-1); // Removing Z otherwise crash happens

        try {
            LocalDateTime dt1 = LocalDateTime.parse(date); // Like 2013-03-30T09:04:56.123
            return dt1.toString("dd MMM yyyy, HH:mm a");
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }

        return "";
    }

However, this is not my local time (correct time should be around 15:00 PM). How can I change this time to my local time?

Was it helpful?

Solution

You strip the 'Z' from your input but that's the bit that specifies the time zone! Z means it is UTC. Without this information, Joda cannot make sense of the input.

What I think you are looking for is:

LocalDateTime local = new DateTime("2013-04-17T07:43:45Z").toLocalDateTime();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top