简体   繁体   中英

How can I handle ISO 8601 dates / Times and format them to the user's locale?

Hi sorry for asking question 1941521315# on ISO 8601 dates but I'm not sure how to proceed.

I got the following format

SimpleDateFormat input = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss:ssZ");

Which I then convert to

SimpleDateFormat output = new SimpleDateFormat("yyyy/MM/dd HH:mm");

This works but I want to present this using the user's date and time format currently used in their device settings. In the past, I used this to convert date and time formats to user's locale but it was always in milliseconds.

userTimeFormat = android.text.format.DateFormat.getTimeFormat(context.getApplicationContext());
userDateFormat = android.text.format.DateFormat.getDateFormat(context.getApplicationContext());

Which I returned as a string.

userTimeFormat.format(new Date(dateAndTimeInMillis)) + ", " + userDateFormat.format(new Date(dateAndTimeInMillis));

So im not sure how to handle this format as well as account for the different time zones. I am aware this ISO 8601 format deals with UTC but wondering how I can implement it along with the user's current date/time format.

I am targeting Android API 11 by the way.

For formatting the date for local display, you should be able to use either SimpleDateFormat's no-arg constructor which will use the default Locale, or the constructor that takes a Locale, if you wish to specify the Locale for the output.

SimpleDateFormat will take care of the time zone conversion for you.

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