如何在 Java 中设置尊重用户操作系统设置的日期和时间格式 [英] How can I set date and time formatting in Java that respects the user's OS settings

查看:28
本文介绍了如何在 Java 中设置尊重用户操作系统设置的日期和时间格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 7 机器上运行我的 Java 应用程序,我的区域设置设置为将日期格式化为 YYYY-mm-dd 并将时间格式化为 HH:mm:ss(例如2011-06-20 07:50:28 英寸).但是当我使用 DateFormat.getDateTimeInstance().format 来格式化我的日期时,我没有看到20-Jun-2011 7:50:28 AM".我需要怎么做才能按照我的客户设置操作系统来显示日期的方式来格式化日期?

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?

我的代码如下所示:

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 将日期放入如下所示的句子中:

The Messages.getFormattedString calls are using MessageFormat to put the date into a sentence that will look like this:

播放QB Nat vs Ian 008"(2011 年 6 月 20 日上午 7:50:28 收到)

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:

播放QB Nat vs Ian 008"(收到 2011-06-20 07:50:28)

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

我搜索了这里和其他 Java 编程网站,但找不到答案,但这似乎是一件显而易见的事情,我觉得我错过了一些明显的东西.

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 中做到这一点.Sun/Oracle 无法让这个系统独立.

You can't do this in pure Java. There is no way Sun/Oracle could make this system independent.

快速浏览 .NET 库提供 此页面 - 引用:

A quick browse of the .NET libraries gives this page - to quote:

用户可以选择通过控制面板的区域和语言选项部分覆盖与当前 Windows 文化相关的一些值.例如,用户可能会选择以不同的格式显示日期或使用不同于默认区域性的货币.如果 CultureInfo.UseUserOverride 属性设置为 true,则还会从用户设置中检索 CultureInfo.DateTimeFormat 对象、CultureInfo.NumberFormat 对象和 CultureInfo.TextInfo 对象的属性.

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 注册表).

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆