Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Date pattern from Timezone of Locale in Java

I have a timezone, and Locale of the user. Now I want to get the date pattern.

For example: User's timezone PST and Locale US and the pattern I expect is "MM/dd/yyyy" and if the user's timezone is IST and Locale India, then pattern I expect is "dd/MM/yyyy"

How to get this?

Note: I want to get the pattern not the actual date so that I can use this in some other place.

like image 887
Kumar D Avatar asked Oct 05 '11 07:10

Kumar D


2 Answers

The logic translating Locale to date/time formats is burried in java.text.SimpleDateFormat#SimpleDateFormat constructor, precisely in sun.util.resources.LocaleData#getDateFormatData. This method provides ResourceBundle which is then queried for particular pattern depending on which style was chosen.

In other words - unfortunately JDK doesn't seems to provide an API/SPI to access raw formats. My advice is to use the Locale all along and pass it to formatting/parsing methods.

like image 91
Tomasz Nurkiewicz Avatar answered Sep 28 '22 22:09

Tomasz Nurkiewicz


Really do you need the TZ for date pattern? The usual way is having the data pattern in the localized properties file for a locale (or locale_country). I think it is enough.

like image 38
yoprogramo Avatar answered Sep 28 '22 20:09

yoprogramo