0

I have a question. I need to convert and display the data from database which it's a timestamp to date format only. *the format is dd/MM/yyyy

Currently I writng the code like this but the error says "Cannot format given Object as a Date"

xi.setItem("Dte",db.getDataAt(i,
           new SimpleDateFormat("dd/MM/yyyy").format("date_column_from_db")));

this is the example of the date column in the database: 20220321211529042 (basically it is a timestamp)

and I want to convert and display the data to date format only, like this: 21/03/2022

Hope to get solution. Thank you!

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • 1
    Timestamp is a thin wrapper around java.util.Date. Date is a Long. "20220321211529042 " is a String – Scary Wombat Jan 17 '23 at 02:34
  • I strongly recommend you neither use `SimpleDateFormat` nor `Date`. The latter is poorly designed, the former a notorious troublemaker of a class, you don’t want to struggle with them. Fortunately both are long outdated and replaced by [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). Use `LocalDateTime` and `DateTimeFormatter` from the modern API. – Ole V.V. Jan 17 '23 at 05:50
  • `"20220321211529042"` (2022 Match 21 at 21:15:29.042) is in which time zone? In which time zone do you want the date format? – Ole V.V. Jan 17 '23 at 05:52
  • Please give a concrete example of the value and type you are passing to `format()` in your code? Passing a string, whether `"date_column_from_db"` or `"20220321211529042"`, is expected to generate the exception you mention. Passing a number, for example `20220321211529042L`, will not (but will also in this case not give you the expected result). – Ole V.V. Jan 17 '23 at 05:55
  • You likely want `LocalDateTime.parse("20220321211529042", DateTimeFormatter.ofPattern("uuuuMMddHHmmssSSS")).format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))`. It yields `21/03/2022`. – Ole V.V. Jan 17 '23 at 06:01
  • 1
    The `java.util` Date-Time API and their formatting API, `SimpleDateFormat` are outdated and error-prone. It is recommended to stop using them completely and switch to the [modern Date-Time API](https://www.oracle.com/technical-resources/articles/java/jf14-Date-Time.html). Check [this answer](https://stackoverflow.com/a/67752047/10819573) and [this answer](https://stackoverflow.com/a/67505173/10819573) to learn how to use `java.time` API with JDBC. – Arvind Kumar Avinash Jan 17 '23 at 16:03
  • 1
    @ArvindKumarAvinash Thanks. I suspect that the OP is keeping the timestamp in a `char` or `varchar` column in their database. Your suggestion would require them to change that to `timestamp` (with or without time zone). Which they should anyway, so it is a very good suggestion. – Ole V.V. Jan 17 '23 at 17:47

2 Answers2

3

Your use of the term 'timestamp' is misleading - it is really an encoded string.

You will need to decode the DB string and then recode it in the new format you want. Something like:

var timestring = "20220321211529042";
var parsedTimestamp = DateTimeFormatter.ofPattern("uuuuMMddHHmmssSSS").parse(timestring);
var output = DateTimeFormatter.ofPattern("dd/MM/yyyy").format(parsedTimestamp);

As an alternative, you can use an intermediate LocalDateTime variable:

var timestring = "20220321211529042";
var dateTime = LocalDateTime.parse(timestring, DateTimeFormatter.ofPattern("uuuuMMddHHmmssSSS"));
var output = dateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
sprinter
  • 27,148
  • 6
  • 47
  • 78
  • 1
    It’s a good step forward compared to the OP’s code and gives the output that the OP says they want. Thanks. Which type has your `parsedTimestamp` got? It’s a `TemporalAccessor`, I type that should not be widely used in application code. I would rather parse into a `LocalDateTime` like this: `LocalDateTime.parse(timestring, DateTimeFormatter.ofPattern("uuuuMMddHHmmssSSS"))`. And then format using for example `parsedTimestamp.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"))`. This is the more conventional way of using java.time. – Ole V.V. Jan 17 '23 at 06:10
  • Have added that as an alternative – sprinter Jan 17 '23 at 06:35
-1

Just test the below code, format() method only allows Date or Number as an argument, String is invalid.

String column = new SimpleDateFormat("dd/MM/yyyy").format(20220321211529042L);
System.out.println("column = " + column);

DateFormat source may be more detailed https://developer.classpath.org/doc/java/text/DateFormat-source.html

public final StringBuffer format(Object obj, StringBuffer toAppendTo,
                                 FieldPosition fieldPosition)
{
    if (obj instanceof Date)
        return format( (Date)obj, toAppendTo, fieldPosition );
    else if (obj instanceof Number)
        return format( new Date(((Number)obj).longValue()),
                      toAppendTo, fieldPosition );
    else
        throw new IllegalArgumentException("Cannot format given Object as a Date");
}
TYZRPVX
  • 90
  • 1
  • 7
  • 2
    20220321211529042 is presumably 2022/03/21 21:15:29.042, not an epoch seconds/ms value. – tgdavies Jan 17 '23 at 02:38
  • 2
    I get `column = 22/06/642726`. It’s clearly wrong. Also neither the OP nor you nor anyone else should use the notoriously troublesome `SimpleDateFormat` class. Use java.time. See the other answer. But thanks for demonstrating how hard it is to use `SimpleDateFormat` correctly and how easy to get it wrong. Even when reading the source code. I think it supports my point. – Ole V.V. Jan 17 '23 at 06:00