如何在Java中设置尊重用户操作系统设置的日期和时间格式 [英] How can I set date and time formatting in Java that respects the user's OS settings
问题描述
DateFormat.getDateTimeInstance()。format
格式化我的日期时,我看不到,而是得到2011年6月20日上午7:50:28。我需要做什么来按照我的客户的操作系统设置来显示日期格式化日期? 这是我的代码如下: p>
文件selGameLastTurnFile = selectedGame.getLastTurn().getTurnFile();
日期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.getFormattedString
调用使用 MessageFormat
将日期放在一个如下所示的句子中:
但是,我的操作系统设置设置为按照上述格式化日期,我预计会看到:
玩转QB Nat vs Ian 008'(收到2011-06-20 07:50:28)
我在这里搜索和其他Java编程网站,无法找到答案,但这似乎是一件很明显的事情,想要做,我觉得我缺少一些明显的东西。
您不能在纯Java中执行此操作。 Sun / Oracle无法使此系统独立。
快速浏览.NET库提供了此页面 - 引用:
用户可以选择通过控制面板的区域和语言选项部分覆盖与当前Windows文化相关联的某些值。例如,用户可以选择以不同的格式显示日期,或者使用文化以外的其他货币。如果CultureInfo.UseUserOverride属性设置为true,则还将从用户设置中检索CultureInfo.DateTimeFormat对象,CultureInfo.NumberFormat对象和CultureInfo.TextInfo对象的属性。
如果您需要此功能,我建议您以系统依赖于Windows的方式执行此操作(例如,以@laz建议的方式访问Windows注册表)。
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.
You can't do this in pure Java. There is no way Sun/Oracle could make this system independent.
A quick browse of the .NET libraries gives this page - to quote:
The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If the CultureInfo.UseUserOverride property is set to true, the properties of the CultureInfo.DateTimeFormat object, the CultureInfo.NumberFormat object, and the CultureInfo.TextInfo object are also retrieved from the user settings.
I would suggest that you do this in a way that is system dependent upon Windows if you need this functionality (e.g. access the Windows registry as @laz suggested).
这篇关于如何在Java中设置尊重用户操作系统设置的日期和时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!