简体   繁体   中英

How to localize timestamps in an Android app using Java?

I'm working on an app where users can timestamp themselves IN or OUT from their workplace. At the moment I'm trying to get the localization of the timestamps done. For example when I make a timestamp in UTC +02:00 at 08:00 02.01.2020, it works correctly and shows the time as 08:00 and right date as well. But when I change to UTC +01:00 in my phone settings, and do the same timestamp, the time becomes 07:00 and date becomes 01.01.2020.

The code I have so far for "parsing" the time looks like this:

String formattedTime = "";

String datetime2 = "1970-01-01T" + returntime;
Log.v("DATE", datetime2);
OffsetDateTime odt2 = OffsetDateTime.parse(datetime2);
Date date2 = Date.from(odt2.toInstant());

SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm",Locale.getDefault());
formattedTime = sdf2.format(date2);
Log.v("FORMTIME", formattedTime);

I'm using a similar code snippet for "parsing" the date as well. The output for the two logs (when in UTC +01:00):

V/DATE: 1970-01-01T15:00:00+02:00
V/FORMTIME: 14:00 //SHOULD BE 15:00
V/DATE: 1970-01-01T08:00:00+02:00
V/FORMTIME: 07:00  //SHOULD BE 08:00
V/DATE: 1970-01-01T08:00:00+02:00
V/FORMTIME: 07:00  //SHOULD BE 08:00

It seems like the change in UTC from +02:00 to +01:00 reduce the time and date also with 1... So is it wrong to use the OffsetDateTime class and "toInstant" (Instant class) for what I'm trying to achieve? What would be the right solution?

OffsetTime

I don't understand what that offset of +02:00 in your string signifies. In particular it confuses me what you want to do when the offset changes. In any case java.time, the modern Java date and time API, parses and formats your time pretty easily. Let's first define the formatter that describes your desired output format:

private static final DateTimeFormatter timeFormatter
        = DateTimeFormatter.ofPattern("HH:mm");

With this in place you may do:

    String returntime = "15:00:00+02:00";
    OffsetTime time = OffsetTime.parse(returntime);
    String formattedTime = time.format(timeFormatter);
    System.out.println(formattedTime);

Output:

15:00

The offset is parsed, but is not used for anything. The output time will always be the same as the time in the string.

I take it that the date 1970-01-01 that you used in your code is arbitrary and without significance. The OffsetTime that I am using hasn't got a date, so saves us from choosing a date for processing the time.

Word use: There isn't any localization going on here. Localization is when for an American audience you print 3:00 PM instead of 15:00, for example.

EDIT:

If your string contains a date too, OffsetDateTime is the right class to use, and again we need no explicit formatter for parsing (only for formatting). Your code in the comment is fine (except that you had accidentally reversed the order of day, month and year in the string).

    String returnDate1 = "2020-12-05T00:00+02:00";
    DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
    OffsetDateTime dateTime = OffsetDateTime.parse(returnDate1);
    String formattedDate = dateTime.format(dateFormatter);
    
    System.out.println(formattedDate);

05-12-2020

What went wrong in your code?

It seems you were over-complicating things. In particular you were mixing old and modern date-time classes. The old ones, Date and SimpleDateFormat , are poorly and confusingly designed, which no doubt contributed to your unexpected results. And when mixing, you are going to need conversions that are not really needed for your job, again just making your code more complicated than needed.

Your sdf2 was using your default time zone for printing the time. You had got offset +02:00 in the string, so when you set the phone to UTC+01:00, a conversion takes place. When the time is 08:00 at offset +02:00, it is only 07:00 at offset +01:00. So this was the result you got. This in turn means that if the user's time zone was at offset +01:00 on 1970-01-01, then you were getting the correct times for that time zone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM