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

时间:2023-01-14 16:10:20

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?

我在一台Windows 7机器上运行我的Java应用程序,在那里我的区域设置被设置为YYYY-mm-dd格式,时间设置为HH:mm:ss。“2011-06-20 07:50:28”)。但是当我使用DateFormat.getDateTimeInstance()时。格式化我的日期,我没有看到相反,我得到“20- 6月-2011年7:50:28 AM”。我需要做什么来格式化日期,就像我的客户有他们的操作系统设置显示日期一样?

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:

这些消息。getFormattedString调用使用MessageFormat将日期放入一个类似以下的句子:

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

玩QB Nat vs Ian 008(收到2011.06 - 2011.07:50:28)

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)

(收到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.

我在这里和其他Java编程网站上搜索,但没有找到答案,但这似乎是一个显而易见的事情,我觉得我漏掉了一些明显的东西。

5 个解决方案

#1


4  

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

纯Java是做不到的。Sun/Oracle不可能使这个系统独立。

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

快速浏览一下。net库,就会得到这个页面- to - 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.

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

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).

如果您需要这个功能,我建议您以一种系统依赖于Windows的方式来完成(例如,按照@laz的建议访问Windows注册表)。

#2


7  

First you have to tell Java what your system LOCALE looks like.

首先,您必须告诉Java您的系统语言环境是什么样子。

Check Java System.
String locale = System.getProperty("user.language")

检查Java系统。字符串区域= System.getProperty(“user.language”)

And then format the date accordinly (SimpleDateFormat)
SimpleDateFormat(String pattern, Locale locale)

然后对日期进行相应的格式化(SimpleDateFormat) SimpleDateFormat(字符串模式,语言环境)

Refer to the practical Java code for a working example...

请参考实用的Java代码以了解一个工作示例……

String systemLocale = System.getProperty("user.language");
String s;
Locale locale; 

locale = new Locale(systemLocale );
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// system locale is PT outputs 16/Jul/2011

locale = new Locale("us");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs Jul 16, 2011

locale = new Locale("fr");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs 16 juil. 2011  

#3


3  

I looks like you will need to access the Windows registry for this. See this question for various solutions to that read/write to Windows Registry using Java.

看起来你需要访问Windows注册表。有关使用Java对Windows Registry进行读写的各种解决方案,请参阅这个问题。

Once you choose one of the many methods of accessing the registry you will need to get the correct key from the registry for the format. This document indicates the key to use is HKEY_USERS\.Default\Control Panel\International.

一旦您选择了许多访问注册表的方法中的一种,您将需要从注册表中为格式获取正确的键。此文档指示要使用的键是HKEY_USERS\。控制面板\ \国际违约。

When using GNOME in Linux, you can use the gconftool command similar to the reg command for Windows as mentioned in the prior links about the Windows registry. I see the key /apps/panel/applets/clock_screen0/prefs/custom_format in my configuration, though it is blank since I am using the default:

在Linux中使用GNOME时,您可以使用gconftool命令,类似于Windows的reg命令,如前面关于Windows注册表的链接所提到的。我在配置中看到了key /apps/panel/applet /clock_screen0/prefs/custom_format,但是由于我使用的是默认值,所以它是空的:

gconftool -g /apps/panel/applets/clock_screen0/prefs/custom_format

I'm not sure if that is the value you'd want to use for your purposes or not.

我不确定这是否是你想要用于你的目的的值。

On Mac OS, I'm not sure what you would do.

在Mac OS上,我不知道你会做什么。

#4


2  

I found this Java utility class by JetBrains that retrieves all the custom locale settings from the OS (both from Windows and Mac) and does the correct formatting for you:

我在JetBrains网站上找到了这个Java实用类,它从操作系统(包括Windows和Mac)中检索所有自定义语言环境设置,并为您做正确的格式化:

https://github.com/JetBrains/intellij-community/blob/master/platform/util/src/com/intellij/util/text/DateFormatUtil.java

https://github.com/JetBrains/intellij-community/blob/master/platform/util/src/com/intellij/util/text/DateFormatUtil.java

It's under the Apache 2.0 license so you can probably use it in your project.

它在Apache 2.0许可下,所以您可以在项目中使用它。

#5


1  

Perhaps I'm misunderstanding what you're getting at here but you need to use the Locale.

也许我误解了你的意思,但是你需要使用语言环境。

 DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

By using Locale you can control what format for what region you're using.

通过使用Locale,您可以控制所使用的区域的格式。

#1


4  

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

纯Java是做不到的。Sun/Oracle不可能使这个系统独立。

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

快速浏览一下。net库,就会得到这个页面- to - 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.

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

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).

如果您需要这个功能,我建议您以一种系统依赖于Windows的方式来完成(例如,按照@laz的建议访问Windows注册表)。

#2


7  

First you have to tell Java what your system LOCALE looks like.

首先,您必须告诉Java您的系统语言环境是什么样子。

Check Java System.
String locale = System.getProperty("user.language")

检查Java系统。字符串区域= System.getProperty(“user.language”)

And then format the date accordinly (SimpleDateFormat)
SimpleDateFormat(String pattern, Locale locale)

然后对日期进行相应的格式化(SimpleDateFormat) SimpleDateFormat(字符串模式,语言环境)

Refer to the practical Java code for a working example...

请参考实用的Java代码以了解一个工作示例……

String systemLocale = System.getProperty("user.language");
String s;
Locale locale; 

locale = new Locale(systemLocale );
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// system locale is PT outputs 16/Jul/2011

locale = new Locale("us");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs Jul 16, 2011

locale = new Locale("fr");
s = DateFormat.getDateInstance(DateFormat.MEDIUM, locale).format(new Date());
System.out.println(s);
// outputs 16 juil. 2011  

#3


3  

I looks like you will need to access the Windows registry for this. See this question for various solutions to that read/write to Windows Registry using Java.

看起来你需要访问Windows注册表。有关使用Java对Windows Registry进行读写的各种解决方案,请参阅这个问题。

Once you choose one of the many methods of accessing the registry you will need to get the correct key from the registry for the format. This document indicates the key to use is HKEY_USERS\.Default\Control Panel\International.

一旦您选择了许多访问注册表的方法中的一种,您将需要从注册表中为格式获取正确的键。此文档指示要使用的键是HKEY_USERS\。控制面板\ \国际违约。

When using GNOME in Linux, you can use the gconftool command similar to the reg command for Windows as mentioned in the prior links about the Windows registry. I see the key /apps/panel/applets/clock_screen0/prefs/custom_format in my configuration, though it is blank since I am using the default:

在Linux中使用GNOME时,您可以使用gconftool命令,类似于Windows的reg命令,如前面关于Windows注册表的链接所提到的。我在配置中看到了key /apps/panel/applet /clock_screen0/prefs/custom_format,但是由于我使用的是默认值,所以它是空的:

gconftool -g /apps/panel/applets/clock_screen0/prefs/custom_format

I'm not sure if that is the value you'd want to use for your purposes or not.

我不确定这是否是你想要用于你的目的的值。

On Mac OS, I'm not sure what you would do.

在Mac OS上,我不知道你会做什么。

#4


2  

I found this Java utility class by JetBrains that retrieves all the custom locale settings from the OS (both from Windows and Mac) and does the correct formatting for you:

我在JetBrains网站上找到了这个Java实用类,它从操作系统(包括Windows和Mac)中检索所有自定义语言环境设置,并为您做正确的格式化:

https://github.com/JetBrains/intellij-community/blob/master/platform/util/src/com/intellij/util/text/DateFormatUtil.java

https://github.com/JetBrains/intellij-community/blob/master/platform/util/src/com/intellij/util/text/DateFormatUtil.java

It's under the Apache 2.0 license so you can probably use it in your project.

它在Apache 2.0许可下,所以您可以在项目中使用它。

#5


1  

Perhaps I'm misunderstanding what you're getting at here but you need to use the Locale.

也许我误解了你的意思,但是你需要使用语言环境。

 DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

By using Locale you can control what format for what region you're using.

通过使用Locale,您可以控制所使用的区域的格式。