Android JodaTime dateTimeFormatter doesn't always return UTC time

58 views Asked by At

My problem is that dateTimeFormatter doesn't always give the same outcome I expected. Sometimes it returns UTC and sometimes phone's timezone. The method below converts a timestamp to yyMMdd-HHmmss format.

static String convertTimestamp(double timestamp) {
    DateTime dateTime = new DateTime((long) timestamp*1000); // conversion to miliseconds
    DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyMMdd-HHmmss");
    return dateTimeFormatter.print(dateTime);
}

I want to get UTC dateTime consistently, how can I achieve this?

1

There are 1 answers

0
ucanyiit On

Changed return line to this, and now the method returns the proper timestamp everytime.

return dateTimeFormatter.withZoneUTC().print(dateTime);