1

I would like to get the current date for a given user language/country. E.g.:

  • 'Friday, 10 March 2017' (for an English user in UK ; perhaps it is 'Friday, March 10th 2017' in some other english-speaking countries)
  • 'Vendredi 10 mars 2017' (for a French user in France)
  • 'Freitag, 10. März 2017' for a German user

etc...

Perhaps I did not search correctly on SO and the web but I did not find convenient answers...

Thanks !

toto_tata
  • 14,526
  • 27
  • 108
  • 198

3 Answers3

0

You can pass default locale with SimpleDateFormat class like this

SimpleDateFormat dateformat = new SimpleDateFormat(timeFormat, Locale.ENGLISH);
Wasim K. Memon
  • 5,979
  • 4
  • 40
  • 55
  • 1
    FYI, the troublesome old date-time classes such as [`java.util.Date`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), [`java.util.Calendar`](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html), and `java.text.SimpleTextFormat` are now [legacy](https://en.wikipedia.org/wiki/Legacy_system), supplanted by the [java.time](https://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) classes. – Basil Bourque Mar 11 '17 at 03:32
0

tl;dr

LocalDate.now( ZoneId.of( "America/Montreal" ) )                    // Capture the current date, “today”.
         .format(                                                   // Generate a string.
             DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL )  // Automatically localize.
                              .withLocale( Locale.CANADA_FRENCH )   // Specify human language for translation and cultural norms for formatting.
         )

Using java.time

Add the ThreeTenABP library to your project. That library is an adaptation for Android of the ThreeTen-Backport project that back-ports to Java 6 & 7 most of the functionality of the java.time classes. The java.time classes are built into Java 8 and later. The java.time classes supplant the notoriously troublesome legacy date-time classes such as Date and Calendar.

Get the current date in the desired/expected time zone. I recommend making a habit of always specifying the time zone explicitly rather than implicitly relying on the JVM’s current default time zone.

The LocalDate class represents a date-only value without time-of-day and without time zone.

A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.

Specify a proper time zone name in the format of continent/region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 3-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).

ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate localDate = LocalDate.now( z );

You can get the JVM’s current default time zone with a call to ZoneId.systemDefault. Keep in mind that the current default can be changed at any moment by any code in any thread of any app within that JVM. So, if time zone is critical, confirm with the user.

Generate a string to represent that value with a DateTimeFormatter object. You can specify a formatting pattern. But you want a localized value, so let DateTimeFormatter do that work.

To localize, specify:

  • FormatStyle to determine how long or abbreviated should the string be.
  • Locale to determine (a) the human language for translation of name of day, name of month, and such, and (b) the cultural norms deciding issues of abbreviation, capitalization, punctuation, separators, and such.

    Locale locale = Locale.CANADA_FRENCH ; DateTimeFormatter f = DateTimeFormatter.ofLocalizedDate( FormatStyle.FULL ).withLocale( locale );

Generate your string.

String output = localDate.format( f );

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, & SimpleDateFormat.

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

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?

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks. How to get the user's ZoneId ? – toto_tata Mar 10 '17 at 19:13
  • @Regis_AG Call [`ZoneId.systemDefault`](https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html#systemDefault--). Keep in mind that the current default can be changed at any moment by any code in any thread of any app within that JVM. So, if time zone is critical, confirm with the user. – Basil Bourque Mar 10 '17 at 19:24
0

If you look for a built-in way on Android without using an external library then the old-style class java.text.DateFormat is the best starting point where you can get a formatter specifying the locale without knowing the exact format pattern. See the javadoc.

DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
String formattedCurrentDate = df.format(new Date());
Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126