This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SimpleDateFormat and locale

 
Ronan Dowd
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I'm using java 1.5 SimpleDateFormat and was wondering if passing a Locale into the constructor
for SimpleDateFormat is needed at all, i.e. will it always bring in way you specify in the pattern ?, see below code:

String PATTERN = "dd/MM/yyyy HH:mm:ss";
Date now = new Date();

SimpleDateFormat sdf1 = new SimpleDateFormat(PATTERN);
System.out.println("Date (default locale) = [" + sdf1.format(now));

SimpleDateFormat sdf2 = new SimpleDateFormat(PATTERN, new Locale("en", "US"));
System.out.println("Date (US locale) = [" + sdf2.format(now));

both of the above give the same value of [15 Apr 2010 11:37:05]
 
Amit Vinod Dali
Ranch Hand
Posts: 42
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't specify a locale, then it will use the default locale of your system. If that also happens to be US English (or some other language where the abbreviation for April is "Apr"), then ofcourse the results will be the same.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic