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

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

问题描述

我正在Windows 7计算机上运行Java应用程序,在该计算机上,我的区域设置已设置为将日期格式为YYYY-mm-dd,将时间格式为HH:mm:ss(例如"2011-06-20 07:50:28).但是,当我使用 DateFormat.getDateTimeInstance().format 格式化我的日期时,却没有看到"2011年6月20日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?

这是我所讨论的代码如下:

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));
}

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天全站免登陆