How do I find out whether a locale uses 12 or 24 hour time in Java?

后端 未结 3 2013
清酒与你
清酒与你 2021-01-05 07:04

In Java, I want to print just the time of day in hours and minutes and want it to correctly switch between, e.g., \"13:00\" and \"1:00 PM\" according to the locale. How do I

3条回答
  •  滥情空心
    2021-01-05 07:36

    Use the java.text.DateFormat class to create the correct output of the given time.

    As from the API:

    To format a date for the current Locale, use one of the static factory methods:

    myString = DateFormat.getDateInstance().format(myDate);
    

    You just use the getTimeInstance() method and call the format() method on the returned DateFormat object

提交回复
热议问题