Rithik_Star
Rithik_Star

Reputation: 661

Date format Issue in Java

I am using below simple date format in backend java to format the date which was given by user through user interface.

SimpleDateFormat sdf = new SimpleDateFormat("MMM");
SimpleDateFormat sdfMonthYear = new SimpleDateFormat("MMM-yyyy");
SimpleDateFormat sdfMonthYearDisplay = new SimpleDateFormat("MMM yyyy");

When It's deployed in my local machine It populates correct month value as Jan , Feb, Mar,Apr etc.But The proj now deployed in other server ex in Swedan's system .So now I am getting the month value as Maj instead of May and Okt instead of Oct.I have verifed that this month format is swedan based.I want a generic way of month format even Its deployed in any country like Jan ,Feb,may,Oct etc.

Please help me on this issue.

Upvotes: 0

Views: 161

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1503439

You just need to specify the locale when you create the SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.US);

Note that using one specific Locale for all users is typically appropriate for machine-to-machine communication - when you're parsing an ISO-8601 format, for example. To display a date to a user, you would normally use their locale and fetch an appropriate format, e.g.

DateFormat format = DateFormat.getDateInstance(DateFormat.LONG, userLocale);

Upvotes: 1

SparkOn
SparkOn

Reputation: 8956

You basically need this constructor SimpleDateFormat(String pattern, Locale locale) of java.text.SimpleDateFormat

Upvotes: 0

Arijit Mukherjee
Arijit Mukherjee

Reputation: 3885

SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.YourCountryCode);

Will change it to your local date time

For Codes Follow :

Locale Code

Upvotes: 1

earthmover
earthmover

Reputation: 4525

Use another constructor of SimpleDateFormat which includes Locale :

SimpleDateFormat(String pattern, Locale locale)

here locale is the locale whose date format symbols should be used

like :

SimpleDateFormat sdf = new SimpleDateFormat("MMM", Locale.US);

Upvotes: 0

Related Questions