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!
SimpleDateFormatnorDate. 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. UseLocalDateTimeandDateTimeFormatterfrom the modern API."20220321211529042"(2022 Match 21 at 21:15:29.042) is in which time zone? In which time zone do you want the date format?java.utilDate-Time API and their formatting API,SimpleDateFormatare 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 usejava.timeAPI with JDBC.charorvarcharcolumn in their database. Your suggestion would require them to change that totimestamp(with or without time zone). Which they should anyway, so it is a very good suggestion.