-2

I would like to obtain the following date format 25/11/1990 14:35 based on a Timestamp of format '0001-01-01-00.00.00.000000'.

It needs to be done either via Angular 6 or Java 8.

Please provide any relevant solutions.

My Approach:

Timestamp timestamp = Timestamp.valueOf("1990-11-25 01:02:03.123456789")//Value from Db.
String str = timestamp.tostring();
str.substring();

This is helping me for displaying to the user but as I am converting it to a string I am unable to store it in DB Since DB will only store Timestamp format.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • Please provide any relevant attempts that you have made. This is not code for hire, what have you tried? what issues did you encounter? – Ashley Medway Apr 06 '20 at 12:31
  • 1
    I answered a similar question today https://stackoverflow.com/questions/61058640/how-to-get-date-and-time-in-below-format-in-java/61058713#61058713 – PaianganuOm Apr 06 '20 at 12:32
  • @AshleyMedway I am trying to convert the Timestamp into a String using tostring() method and format it Using substring, "timestamp.toString()". While it displays to the user via this method. When I try to edit the record and send it to Db it no longer works as in the DB the column is specified as Timestamp and I am sending a string value. – Vivek Kumar Jha Apr 06 '20 at 12:46
  • When you say Timestamp, did you mean `java.sql.Timestamp`? I recommend you don’t use that class. It is poorly designed and long outdated. Instead retrieve your timestamp as either an `OffsetDateTime` (for timestamp with time zone) or a `LocalDateTime` (for without time zone) from your database. Both classes are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). See [my answer here](https://stackoverflow.com/a/54907501/5772882). – Ole V.V. Apr 06 '20 at 13:04

1 Answers1

0

java.time and JDBC 4.2

I warmly recommend that you use java.time, the modern Java date and time API, for your date and time work, not the old Timestamp class. This example snippet includes how you retrieve the timestamp from your database and format it for the user:

    DateTimeFormatter formatter = DateTimeFormatter
            .ofLocalizedDateTime(FormatStyle.SHORT)
            .withLocale(Locale.forLanguageTag("pt"));

    PreparedStatement query = yourDatabaseConnection.prepareStatement(
            "select your_timestamp_column from your_table where id = 4;");
    ResultSet rs = query.executeQuery();
    if (rs.next()) {
        OffsetDateTime dateTime
                = rs.getObject("your_timestamp_column", OffsetDateTime.class);
        String str = dateTime.format(formatter);
        System.out.println(str);
    }

Example output would be

25/11/1990 14:35

Please insert the language tag of your user where I put pt (for Portuguese). Or leave out the withLocale call to rely on the default locale of your JVM. And note the use of getObject() rather than getTimestamp().

I have used OffsetDateTime in the code based on the assumption that the datatype in your database is timestamp with time zone, which is what is recommended for timestamps. In this case you may also want to convert the timestamp to the user’s time zone before formatting it.

If the datatype is timestamp without time zone, instead use LocalDateTime in exactly the same manner.

Link: Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161