加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如何在Java中设置尊重用户操作系统设置的日期和时间格式

发布时间:2020-12-14 16:39:34 所属栏目:Java 来源:网络整理
导读:我在 Windows 7机器上运行我的 Java应用程序,我的区域设置设置为格式化日期为YYYY-mm-dd,时间为HH:mm:ss(例如“2011-06-20 07:50:28”) .但是当我使用DateFormat.getDateTimeInstance().格式来格式化我的日期时,我看不到,而是得到“2011年6月20日上午7:50:
我在 Windows 7机器上运行我的 Java应用程序,我的区域设置设置为格式化日期为YYYY-mm-dd,时间为HH:mm:ss(例如“2011-06-20 07:50:28”) .但是当我使用DateFormat.getDateTimeInstance().格式来格式化我的日期时,我看不到,而是得到“2011年6月20日上午7:50:28”.我的客户将操作系统设置为显示日期的方式来格式化日期需要做些什么?

这是我的代码如下:

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.getFormattedString调用使用MessageFormat将日期放入一个如下所示的句子:

Play the turn ‘QB Nat vs Ian 008’ (received 20-Jun-2011 7:50:28 AM)

但是,我的操作系统设置是按照上述格式设置日期,我预计会看到:

Play the turn ‘QB Nat vs Ian 008’ (received 2011-06-20 07:50:28)

我在这里搜索和其他Java编程网站,找不到答案,但这似乎是一件很明显的事情,想要做,我觉得我缺少一些明显的东西.

解决方法

您不能在纯Java中执行此操作. Sun / Oracle无法使此系统独立.

.NET库的快速浏览给出了this page – 引用:

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.

如果您需要此功能,我建议您以系统依赖于Windows的方式执行此操作(例如,以@laz建议的方式访问Windows注册表).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读