我正在Windows 7机器上运行我的Java应用程序,其中我的区域设置设置为将date格式设置为YYYY-mm-dd,时间格式为HH:mm:ss(例如“2011-06-20 07:50:28”) 。 但是,当我使用DateFormat.getDateTimeInstance().format
来格式化我的date,我没有看到,而是我得到“20-Jun-2011 7:50:28 AM”。 我需要做什么来格式化date的方式,我的客户的操作系统设置显示date?
这是我的代码是这样的:
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
将date放入一个如下所示的句子中:
打出QB纳特vs伊恩008'(收到2011年6月20日7时50分28秒)
然而,我的操作系统设置设置为格式化date,如上所述,我期望看到这样的:
打出QB纳特vs伊恩008'(2011-06-20 07:50:28收到)
我在这里和其他Java编程站点search,找不到答案,但这似乎是这样一个明显的事情想要做,我觉得我失去了明显的东西。
你不能在纯Java中做到这一点。 Sun / Oracle不可能使这个系统独立。
.NET库的快速浏览给出了这个页面 – 引用:
用户可以选择通过“控制面板”的区域和语言选项部分覆盖与当前Windows文化相关的一些值。 例如,用户可能选择以不同格式显示日期,或者使用文化默认值以外的其他货币。 如果CultureInfo.UseUserOverride属性设置为true,则还会从用户设置中检索CultureInfo.DateTimeFormat对象,CultureInfo.NumberFormat对象和CultureInfo.TextInfo对象的属性。
如果你需要这个功能,我建议你以一种依赖于Windows的系统的方式来做到这一点(例如,如@laz建议的那样访问Windows注册表)。
首先,你必须告诉Java你的系统LOCALE是什么样的。
检查Java系统。
String locale = System.getProperty("user.language")
然后格式化日期(SimpleDateFormat)
SimpleDateFormat(String pattern, Locale locale)
参考一个实际的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
我看起来像你需要访问Windows注册表。 看到这个问题的各种解决方案,以读/写使用Java的Windows注册表 。
一旦您选择访问注册表的许多方法之一,您将需要从注册表中获取格式正确的密钥。 本文档指出使用的关键是HKEY_USERS\.Default\Control Panel\International
。
在Linux中使用GNOME时,可以使用与Windows注册表的reg
命令类似的gconftool
命令。 我在我的配置中看到了key /apps/panel/applets/clock_screen0/prefs/custom_format
,但是由于我使用的是默认值,所以它是空白的:
gconftool -g /apps/panel/applets/clock_screen0/prefs/custom_format
我不确定这是否是您想要用于您的目的的价值。
在Mac OS上,我不确定你会做什么。
也许我误解了你在这里得到的东西,但是你需要使用Locale。
DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
通过使用区域设置,您可以控制使用什么区域的格式。