0

I want to know what time format has the user chosen in his computer say in his Windows time setting like 12 hr or 24 hour format. I want to detect that and use that as the time format in my Java application.

Note: I am aware of the SimpleDateFormat API but that is something I will use only after I can detect what user has selected in Windows time setting.

Any help is appreciated. Krishna

1 Answers1

0

For SimpleDateFormat, You call toLocalizedPattern()

For Java 8 users:

The Java 8 Date Time API is similar to Joda-time. To gain a localized pattern we can use class DateTimeFormatter

DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);

Note that when you call toString() on LocalDate, you will get date in format ISO-8601

Note that Date Time API in Java 8 is inspired by Joda Time and most solution can be based on questions related to time.

Mohith P
  • 585
  • 5
  • 14
  • The solution you provided gives only the format used in the Locale. Here is what I did that worked for me - `ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "time"); builder.redirectErrorStream(true); Process p = builder.start(); InputStream inputStream = p.getInputStream(); BufferedReader r = new BufferedReader( new InputStreamReader(inputStream)); String line = null; while ( (line = r.readLine()) != null) { System.out.println(line); p.destroy(); }` This prints The current time is: 19:16:07.43 Enter the new time: For 24H format – Krishna Murthy Mirajkar Feb 29 '16 at 13:46
  • The [https://en.wikipedia.org/wiki/TIME_(command)](https://en.wikipedia.org/wiki/TIME_(command)) has all the details which can be used to detect the time format in different OS and looked up to feed our Java Application logic – Krishna Murthy Mirajkar Feb 29 '16 at 14:45