How to set date and time formats in Java that respect the user’s operating system settings
I run my java application on a Windows 7 machine, and my locale is set to format the date as yyyy MM DD and the time as HH: mm: SS (for example, "2011-06-20 07:50:28") But when I use dateformat getDateTimeInstance(). Format to format my date, I can't see it, but get "7:50:28 a.m. on June 20, 2011" What does my customer need to do to format the date by setting the operating system to display the date?
This is my code as follows:
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)); }
Messages. The getformattedstring call uses messageformat to put the date into a sentence as follows:
However, my operating system sets the date according to the above format. I expect to see:
I search here and other Java programming websites and can't find the answer, but it seems to be an obvious thing. If I want to do it, I think I lack some obvious things
Solution
You cannot perform this operation in pure Java Sun / Oracle cannot make this system independent
. Net library gives this page – reference:
If you need this feature, I recommend that you do it in a way that your system depends on windows (for example, access the windows registry in the @laz recommended way)