i get time field from a database table. But may i know how to convert it into 24 hour minutes and second. simpledateformat("hhmmss") is not working. Is there any other simple java method doing it?

Dani AI

Generated

Quick practical summary and recommended approach.

This thread shows the usual culprits: the formatter token (24‑hour vs 12‑hour) and the JDBC column type. As pointed out, use the 24‑hour hour symbol rather than the 12‑hour one. As warned, calling a date-only getter will yield 00:00:00 for the time part. 's suggestion to format in SQL is valid for some workflows, but database formatting functions differ by vendor and reduce portability.

A concise, robust Java-side approach (Java 8+) is to read the timestamp/time from the ResultSet, convert to the java.time API, and format with a thread-safe formatter. Example:

Timestamp ts = rs.getTimestamp("time_col");
if (ts != null) {
    LocalTime lt = ts.toLocalDateTime().toLocalTime();
    String formatted = lt.format(DateTimeFormatter.BASIC_ISO_TIME);
}

Notes and troubleshooting:

  • DateTimeFormatter and the java.time types are immutable and thread-safe; prefer them over SimpleDateFormat in multi-threaded servers. For pre-Java 8 environments, create a new SimpleDateFormat per use or use a ThreadLocal wrapper.
  • Use the JDBC getter that matches the column type: getTimestamp/getTime/getObject(...) for time-bearing columns. Calling a date-only getter will drop the time.
  • Handle nulls from the ResultSet and check driver/JDBC compatibility for java.time support (toLocalDateTime/toLocalTime).
  • Formatting in the database (TO_CHAR or vendor equivalent) is an option for quick display values, but application-side formatting avoids DB-specific patterns and locale surprises.

Ignore 's conversion snippet — it is not valid Java.

Recommended Answers

All 10 Replies

Show your code (and use code tags). Also, is it a Date, DateTime, or TimeStamp field in the Database.

Be aware though, that if your calling getDate on the field, that it will return only a Date, the time will be 00:00:00.

Check the doc for SimpleDateFormat. You want to use "HHmmss" instead of "hhmmss".

Show your code (and use code tags). Also, is it a Date, DateTime, or TimeStamp field in the Database.

Be aware though, that if your calling getDate on the field, that it will return only a Date, the time will be 00:00:00.

if i do it this way
String time = x.getTime(1).toString();

will it 24 hour minutes and second in the format 001236 ?

Check the doc for SimpleDateFormat. You want to use "HHmmss" instead of "hhmmss".

just tested....it works !! thanks
still need to try it in DB2

What is the sql type of the column in your database?
Perhaps you can alter the query in order to format the column and return it as String:

select TO_CHAR(date_col,'DD/MM/YYYY HH24 : MI : SS') from table tbl;

The date_col would be type DATE at the table and when you get the column from the ResultSet you can use: rs.getString();

I think that if it is type TIME then use only: HH24 : MI : SS in the TO_CHAR method

... i'd say to have a method that reads the time from thr base then get the HOURs convert it IF needed ex:

public Date convert(Date time){
int mask 12;
if (time <= mask)
time=time+12

... i'd say to have a method that reads the time from thr base then get the HOURs convert it IF needed ex:

public Date convert(Date time){
int mask 12;
if (time <= mask)
time=time+12

You declare time as Date and mask as int ?????? Can you please explain what do you mean, because your code doesn't make any sense.

Thanks javaAddict.....got your idea........your method works.....

select TO_CHAR(date_col,'DD/MM/YYYY HH24 : MI : SS') from table tbl;

... i'd say to have a method that reads the time from thr base then get the HOURs convert it IF needed ex:

public Date convert(Date time){
int mask 12;
if (time <= mask)
time=time+12

This code is not valid and appropriate solutions have already been provided.

Don't worry too much about this guy. He has proven on another forum that he doesn't know what he is doing and either just cannot understand what you try to tell him, or simply ignores it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.