Localized Date Format in Java

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

I know that I can specify this format:

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

But I would like the format to be localized in the user's locale. I also tried

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

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

+4
source share
6 answers

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

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

The first int is the style for the hour, the other is the style for the 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.

Exit for Locale.GERMANY: Date: 07/25/13 10:57

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

+12
source

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

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

DateFormat.getDateInstance(int, Locale) => Gets the date formatter with the specified formatting style for this language.

While

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

+2
source

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

  • Date field style
  • time field style
  • The locale by which the template is automatically 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)); 
+1
source

Try something like this

  SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yy HH:mm", Locale.getDefault()); String formatted = simpleDateFormat.format(900000); System.out.println(simpleDateFormat.parse(formatted)); 
+1
source

You can use something like this:

 SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yy HH:mm", Locale.getDefault()); String formatted = sdf .format(900000); System.out.println(simpleDateFormat.parse(formatted)); 
+1
source

Using Joda-Time , you can define system settings and use a 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()); 
0
source

Source: https://habr.com/ru/post/1493394/


All Articles