Date and time formatting depending on locale

Use android.text.format.DateFormat.getTimeFormat()

ref: http://developer.android.com/reference/android/text/format/DateFormat.html


tl;dr

ZonedDateTime                                   // Represent a moment as seen in the wall-clock time used by the people of a particular region (a time zone). 
.now( ZoneId.of( "Asia/Kolkata" ) )             // Capture the current moment as seen in the specified time zone. Returns a `ZonedDateTime` object.
.format(                                        // Generate text representing the value of this `ZonedDateTime` object.
    DateTimeFormatter                           // Class controlling the generation of text representing the value of a date-time object.
    .ofLocalizedDateTime ( FormatStyle.FULL )   // Automatically localize the string representing this date-time value.
    .withLocale ( Locale.FRENCH )               // Specify the human language and cultural norms used in localizing.
)                                               // Return a `String` object.

java.time

The code in the Question uses troublesome old date-time classes, now legacy, supplanted by the java.time classes built into Java 8 and later.

Locale and time zone have nothing to do with each other. Locale determines the human language and cultural norms used when generating a String to represent a date-time value. The time zone determines the wall-clock time of a particular region used to represent a moment on the timeline.

The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).

Instant instant = Instant.now();

2016-10-12T07:21:00.264Z

Apply a time zone to get a ZonedDateTime. I arbitrarily choose to show this moment using India time zone. Same moment, same point on the timeline.

ZoneId z = ZoneId.of( "Asia/Kolkata" );
ZonedDateTime zdt = instant.atZone( z );

2016-10-12T12:51:00.264+05:30[Asia/Kolkata]

Generate a String using the locale of Québec Canada. Let java.time automatically localize the string.

Locale l = Locale.CANADA_FRENCH;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime ( FormatStyle.FULL ).withLocale ( l );
String output = zdt.format ( f );  // Indian time zone with Québécois presentation/translation.

mercredi 12 octobre 2016 12 h 51 IST


About java.time

The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date, .Calendar, & java.text.SimpleDateFormat.

The Joda-Time project, now in maintenance mode, advises migration to java.time.

To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.

Where to obtain the java.time classes?

  • Java SE 8 and SE 9 and later
  • Built-in.
  • Part of the standard Java API with a bundled implementation.
  • Java 9 adds some minor features and fixes.
  • Java SE 6 and SE 7
  • Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
  • Android
  • The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
  • See How to use….

The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval, YearWeek, YearQuarter, and more.

Diagram showing when to use java.time, ThreeTenABP, or Android desugaring API