Sunday, November 10, 2019

java - 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.


Answer



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.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...