Home > front end >  Show a pretty timestamp with month name
Show a pretty timestamp with month name

Time:12-28

I have a Timestamp object, and I want to show it.

Actually, I'm using new Timestamp(System.currentTimeMillis()).toLocaleString(), and I have this result:

27 déc. 2021 17:54:35

But :

  • the toLocaleString() method is deprecated
  • It return the value according to computer's lang, so it can show this :

Dec 27, 2021 17:54:35 PM

Which is NOT what I want. In my personnal computer, in french, it's fine. But, on VPS it's not fine.

Also, all finded thread are only talking about formatting but without month name, which is not what I want.

How can I easily show the timestamp, in French language?

CodePudding user response:

tl;dr

Use new conversion methods added to the old class Timestamp to convert to modern java.time classes.

Timestamp myTimestamp = Timestamp.from( Instant.now() );  // Simulate receiving a `Timestamp` object from old code not yet updated to *java.time*. 
System.out.println(
        myTimestamp
                .toInstant()                              // Convert from legacy class to modern class.
                .atZone(                                  // Adjust into a time zone.
                        ZoneId.of( "Asia/Tokyo" )
                )                                         // Returns a `ZonedDateTime` object. 
                .format(
                        DateTimeFormatter
                                .ofLocalizedDateTime(
                                        FormatStyle.FULL
                                )
                                .withLocale(
                                        Locale.FRANCE
                                )
                )                                          // Returns a `String` object, generated text.
);

See this code run live at IdeOne.com.

mardi 28 décembre 2021 à 05:39:58 heure normale du Japon

Details

Timestamp is one of the terrible legacy date-time classes. These classes were years ago supplanted by the modern java.time classes. Never use Timestamp, Date, Calendar, SimpleDateFormat, etc.

Capture the current moment as seen in UTC, with an offset of zero hours-minutes-seconds.

Instant instant = Instant.now() ;

Capture the current moment as seen in a particular time zone.

ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;

Generate text in an automatically localized format. Specify a locale to determine the human language used in translation and the cultural norms used in abbreviation, capitalization, order of elements, and such.

Locale locale = Locale.CANADA_FRENCH ;
DateTimeFormatter f = DateTimeFormatter.ofLocalizedDateTime( FormatStyle.MEDIUM ).withLocale( locale ) ;
String output = zdt.format( f ) ;

If receiving a moment from a database column of a data type akin to the SQL standard type TIMESTAMP WITH TIME ZONE, use OffsetDateTime class in Java with a JDBC driver compliant with JDBC 4.2 or later.

OffsetDateTime odt = myResultSet.getObject( … , OffsetDateTime.class ) ;

Adjust into your desired time zone to yield a ZonedDateTime object.

ZonedDateTime zdt = odt.atZoneSameInstant( z ) ;

All of this has been handled many times already on Stack Overflow. Search to learn more.

  • Related