1

I am working on a Java project in which I have a Time object in another column. But for some query purposes, I need the time as a part of Timestamp. For this reason, I decided to call the setTime method of java.SQL.Timestamp, but it resets the date to 1970.

How can I only change the time part in java.SQL.Timestamp?

COde :

            object.getTimestamp().setTime(object.getTTime().getTime());

Thank you.

4
  • 1
    Hi, can you explain a bit more what are you trying to do, maybe post your query, or more code? Commented Nov 3, 2016 at 10:23
  • @karelss : Sure. I have two objects one is java.SQL.Timestamp and other is Time. I want to set only the time part in Timestamp while keeping the date in it constant. THe source of the time is Time object. Commented Nov 3, 2016 at 10:26
  • You have to work with long representation of the time, and extract the "time" part and add it to your other object, i think Commented Nov 3, 2016 at 10:30
  • @karelss : Yes, extract time from Time object, and set time ONLY in TImestamp object. Commented Nov 3, 2016 at 10:31

1 Answer 1

2

Work as with a normal java.util.Date, update the time part using Calendar, then update the timestamp with

  setTime(long time) 

Example

            Calendar c=Calendar.getInstance();
    c.setTimeInMillis(time.getTime());
    c.set(Calendar.HOUR_OF_DAY, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);

    ts.setTime(c.getTimeInMillis());
    System.out.println(ts.getTime()); 

EDIT

Example 2

    Timestamp ts; 
    //give some value to ts
    Time time ;
    //give some value to time

    //Calendar based on ts          
    Calendar cTs=Calendar.getInstance();
    cTs.setTimeInMillis(ts.getTime());

    //Calendar based on time
    Calendar cTime=Calendar.getInstance();
    cTime.setTimeInMillis(time.getTime());

    cTs.set(Calendar.HOUR_OF_DAY, cTime.get(Calendar.HOUR_OF_DAY));
    cTs.set(Calendar.MINUTE, cTime.get(Calendar.MINUTE));
    cTs.set(Calendar.SECOND, cTime.get(Calendar.SECOND));
    cTs.set(Calendar.MILLISECOND, cTime.get(Calendar.MILLISECOND));

    //set value of ts based on the modified cTs
    ts.setTime(cTs.getTimeInMillis());
    System.out.println(ts.getTime()); 
Sign up to request clarification or add additional context in comments.

7 Comments

What do you mean by works as normal with Date? Can you describe what you want to say, I cannot understand.
see the example in my answer
And in your above example where are you setting hours and minutes from Time object?
there's a small error, in second line of my code, now is correct. The Time class extends Date, so you can use getTime and manipulate it with Calendar. And of course you can manipulate also the date part, if you need
This way if I do, then the the TImestamp is set to current time, not to the one I have defined. So, There is a timestamp object like 04.11.2016 22.00 .. I only want to change the 22.00 part of it from a Time object..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.