Question

I am trying to format a date with time including the milliseconds in Java specific to the current Locale, so for example in US/English Locale it should look like this: 10/7/13 05:24:12.121 PM. And in Germany/German Locale it should look like this: 07.10.13 17:24:12.121. I am aware of the DateFormat class in Java as well as the SimpleDateFormat class. Using the DateFormat, I can get a DateFormat object that lets me format the time specific to a certain Locale, but even with DateFormat.FULL as the formating paramater for the format method, it only displays the time up to the second. Using SimpleDateFormat, I can define my own date and time format including milliseconds, but then I would have to take care of the Locales myself, which would be a mess.

I also had a look at the Joda Time API, but I couldn't find the solution there either.

So the question is, how can I format a date with time including milliseconds in Java specific to a certain Locale?

Was it helpful?

Solution

Using SimpleDateFormat, I can define my own date and time format including milliseconds, but then I would have to take care of the Locales myself, which would be a mess.

Why mess? Just define e.g. a HashMap from Locale to SimpleDateFormat for those several locales you want to support. Write the custom SimpleDateFormats as you like. Then just use the right one based on the specific Locale passed in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top