How can I set date and time formatting in Java that respects the user's OS settings

后端 未结 7 928
Happy的楠姐
Happy的楠姐 2020-11-28 11:25

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

7条回答
  •  悲&欢浪女
    2020-11-28 11:55

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

    A quick browse of the .NET libraries gives this page - 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.

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

提交回复
热议问题