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!

7
  • 1
    Timestamp is a thin wrapper around java.util.Date. Date is a Long. "20220321211529042 " is a String Commented Jan 17, 2023 at 2: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. Use LocalDateTime and DateTimeFormatter from the modern API. Commented Jan 17, 2023 at 5: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? Commented Jan 17, 2023 at 5:52
  • 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. Check this answer and this answer to learn how to use java.time API with JDBC. Commented Jan 17, 2023 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. Commented Jan 17, 2023 at 17:47

2 Answers 2

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"));
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Have added that as an alternative
-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");
}

2 Comments

20220321211529042 is presumably 2022/03/21 21:15:29.042, not an epoch seconds/ms value.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.