I am running my Java app on a Windows 7 machine where my regional settings are set up to format dates as YYYY-mm-dd and time as HH:mm:ss (e.g. "2011-06-20 07:50:28"). But when I use DateFormat.getDateTimeInstance().format
to format my date I do not see that instead I get "20-Jun-2011 7:50:28 AM". What do I need to do to format dates in the way that my customers have their OS setup to display dates?
Here is what my code in question looks like:
File selGameLastTurnFile = selectedGame.getLastTurn ().getTurnFile ();
Date selGameModifiedDate = new Date (selGameLastTurnFile.lastModified());
if (selectedGame.isYourTurn ()) {
gameInfo = Messages.getFormattedString ("WhoseTurnIsIt.Prompt.PlayTurn", //$NON-NLS-1$
FileHelper.getFileName (selGameLastTurnFile),
DateFormat.getDateTimeInstance().format(selGameModifiedDate));
} else {
gameInfo = Messages.getFormattedString ("WhoseTurnIsIt.Prompt.SentTurn", //$NON-NLS-1$
selGameLastTurnFile.getName (),
DateFormat.getDateTimeInstance().format(selGameModifiedDate));
}
The Messages.getFormattedString
calls are using MessageFormat
to put the date into a sentence that will look like this:
Play the turn 'QB Nat vs Ian 008' (received 20-Jun-2011 7:50:28 AM)
However my OS settings are setup to format the date as I described above and I expected to see this:
Play the turn 'QB Nat vs Ian 008' (received 2011-06-20 07:50:28)
I searched here and other Java programming sites and could not find the answer but this seems like such an obvious thing to want to do that I feel like I am missing something obvious.
Java SimpleDateFormat with Locale String pattern = "EEEEE MMMMM yyyy HH:mm:ss. SSSZ"; SimpleDateFormat simpleDateFormat =new SimpleDateFormat(pattern, new Locale("fr", "FR")); String date = simpleDateFormat. format(new Date()); System.
Formatting Dates String pattern = "yyyy-MM-dd"; SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern); String date = simpleDateFormat. format(new Date()); System. out. println(date);
First you have to tell Java what your system LOCALE looks like.
Check Java System.String locale = System.getProperty("user.language")
And then format the date accordinly (SimpleDateFormat)SimpleDateFormat(String pattern, Locale locale)
Refer to the practical Java code for a working example...
String systemLocale = System.getProperty("user.language");
String s;
Locale locale;
locale = new Locale(systemLocale );
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// system locale is PT outputs 16/Jul/2011
locale = new Locale("us");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs Jul 16, 2011
locale = new Locale("fr");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs 16 juil. 2011
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With