Home » Java » Java: convert a time from today to a timestamp

Java: convert a time from today to a timestamp

Posted by: admin December 28, 2021 Leave a comment

Questions:

I am using Java 6, and I have a time from the current date as a string, like this: 14:21:16, and I need to convert this to a Timestamp object to store in a database.

However there seems to be no good way to get a Timestamp from this. Timestamp.valueOf(String) is quite close, but requires a date. Is there a good way to make a Timestamp object from such a string?

Answers:

How about this:

final String str = "14:21:16";
final Timestamp timestamp =
    Timestamp.valueOf(
        new SimpleDateFormat("yyyy-MM-dd ")
        .format(new Date()) // get the current date as String
        .concat(str)        // and append the time
    );
System.out.println(timestamp);

Output:

2011-03-02 14:21:16.0

###

Personally, I’d use Joda Time to parse the time to a LocalTime, and add that to today’s LocalDate to get a LocalDateTime, then convert that into an Instant using whatever time zone you’re interested in. (Or use LocalTime.toDateTimeToday(DateTimeZone).)

Then just create a time stamp using the Timestamp(long) constructor.

There are plenty of other approaches (e.g. using SimpleDateFormat instead of parsing with Joda Time, if you really want…) but ultimately you’re likely to want the Timestamp(long) constructor in the end. (The benefit of using Joda Time here is that it’s obvious what’s being represented at each stage – you’re not trying to treat a “time only” as a “date and time” or vice versa.)

###

Best I can come up with using standard API is not that pretty:

    // Get today's date and time.
    Calendar c1 = Calendar.getInstance();
    c1.setTime(new Date()); 

    // Get the required time of day, copy year, month, day.
    Calendar c2 = Calendar.getInstance();
    c2.setTime(java.sql.Time.valueOf("14:21:16"));
    c2.set(Calendar.YEAR, c1.get(Calendar.YEAR));
    c2.set(Calendar.MONTH, c1.get(Calendar.MONTH));
    c2.set(Calendar.DAY_OF_MONTH, c1.get(Calendar.DAY_OF_MONTH));

    // Construct required java.sql.Timestamp object.
    Timestamp time = new Timestamp(c2.getTimeInMillis());

    Let's see what we've done.
    System.out.println(time);

Note that java.sql.Time.valueOf accepts a string of the form “HH:MM:SS” as you require. Other formats would require use of SimpleDateFormat.

###

Use org.apache.commons.lang.time.DateUtils:

Date today = DateUtils.truncate(new Date(), Calendar.DAY_OF_MONTH);    
DateFormat df = new SimpleDateFormat("HH:mm:ss");
Date time = df.parse("14:21:16");
Timestamp time = new Timestamp(today.getTime() + time.getTime());

###

Have a given day (say, unix epoch?) to serve as the day. When you use it, only use the time parameters that you care about, ignoring the day.

Another option would be java.sql.Time

http://download.oracle.com/javase/1.4.2/docs/api/java/sql/Time.htm

###

String str = "14:21:16";
DateFormat formatter = new SimpleDateFormat("HH:mm:ss"); 
Date date = formatter.parse(str); 
Timestamp timestamp = new Timestamp(date.getTime());