File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes SimpleDateFormat and locale Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "SimpleDateFormat and locale" Watch "SimpleDateFormat and locale" New topic
Author

SimpleDateFormat and locale

Ronan Dowd
Ranch Hand

Joined: Jan 21, 2006
Posts: 84
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]

SCJP 1.4 | OCWCD JEE 5
Amit Vinod Dali
Ranch Hand

Joined: Apr 14, 2010
Posts: 42
http://java.sun.com/docs/books/tutorial/i18n/format/dateFormatSymbols.html
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13875
    
  10

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SimpleDateFormat and locale
 
Similar Threads
Parsing date..?
how to convert date to a string
Covert String to Date
date format conversion to MM/dd/yyyy hh:mm a
Timezone offset to Timezone ID conversion