4

I have a timestamp in millis and want to format it indicating day, month, year and the hour with minutes precission.

I know I can specify the format like this:

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy HH:mm");
String formatted = simpleDateFormat.format(900000)

But I'd like the format to be localized with the user's locale. I've also tried

DateFormat DATE_FORMAT = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
DATE_FORMAT.format(new Date());

But it does not show the hour. How can I do it?

theadam
  • 3,961
  • 4
  • 25
  • 41
Addev
  • 31,819
  • 51
  • 183
  • 302
  • As a note, it seems that `DateFormat.getDateTimeInstance(int, int)` calls `DateFormat.getDateTimeInstance(int, int, Locale)` using `Locale.getDefault()` anyway, at least with the current version of Android. – JAB Jan 16 '14 at 15:44

6 Answers6

13

Is using joda time (http://joda-time.sourceforge.net/) out of the question? If not, then I would wholeheartedly recommend using this wonderful library instead of the cumbersome Java API.

If not, you could use DateFormat.getDateTimeInstance(int, int, Locale)

The first int is the style for hour, the other is the style for time, so try using:

DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
String formattedDate = f.format(new Date());
System.out.println("Date: " + formattedDate);

See if this suits you.

Output for Locale.GERMANY: Date: 25.07.13 10:57

Output for Locale.US: Date: 7/25/13 10:57 AM

theadam
  • 3,961
  • 4
  • 25
  • 41
2

But it does not show the hour. How can I do it?

You have to call DateFormat.getDateTimeInstance(int, int, Locale)

DateFormat.getDateInstance(int, Locale) => Gets the date formatter with the given formatting style for the given locale.

While

DateFormat.getDateTimeInstance(int, int, Locale) => Gets the date/time formatter with the given formatting styles for the given locale.

M. Abbas
  • 6,409
  • 4
  • 33
  • 46
2

java.time

The legacy date-time API (java.util date-time types and their formatting API, SimpleDateFormat) is outdated and error-prone. It is recommended to stop using it completely and switch to java.time, the modern date-time API*.

You can use DateTimeFormatter.ofLocalizedDateTime to obtain a locale-specific date format for the ISO chronology.

Demo:

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        DateTimeFormatter dtfLocalized = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT);
        // Test
        LocalDateTime date = LocalDateTime.now();

        System.out.println(dtfLocalized.withLocale(Locale.US).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.UK).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.CHINESE).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.GERMAN).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.forLanguageTag("tr")).format(date));
        System.out.println(dtfLocalized.withLocale(Locale.getDefault()).format(date));
    }
}

Output:

5/8/21, 10:49 PM
08/05/2021, 22:49
2021/5/8 δΈ‹εˆ10:49
08.05.21, 22:49
8.05.2021 22:49
08/05/2021, 22:49

Learn more about the the modern date-time API* from Trail: Date Time.


* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

You can use method getDateTimeInstance, of DateFormat. Here the getDateTimeInstance method takes 3 arguments

  1. the style of Date field
  2. the style of time field
  3. the Locale using which pattern is auto extracted

    DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
    System.out.println(DATE_FORMAT.format(d));
    DATE_FORMAT = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.FRENCH);
    System.out.println(DATE_FORMAT.format(d));
    
sanbhat
  • 17,522
  • 6
  • 48
  • 64
1

Try some thing like this

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy HH:mm", Locale.getDefault());
    String formatted = simpleDateFormat.format(900000);
    System.out.println(simpleDateFormat.parse(formatted));
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Using Joda-Time you could detect the system setting and use different time format:

String format;

if (DateFormat.is24HourFormat(context)) {
  format = "MM/dd/yy, hh:mm";
}
else {
  format = "MM/dd/yy, h:mm aa";
}

DateTimeFormatter formatter = DateTimeFormat.forPattern(format);
formatter.print(new DateTime());
Rafal Enden
  • 3,028
  • 1
  • 21
  • 16