How to merge java.sql.Date and java.sql.Time to java.util.Date?

后端 未结 3 607
深忆病人
深忆病人 2021-01-05 22:48

I have two objects: a java.sql.Date and a java.sql.Time.
What is the best way to merge them into single java.util.Date?

In

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-05 23:28

    You can create two Calendar instances. In the first you initialize the date and in the latter the time. You can the extract the time values from the "time" instance and set them to the "date".

      // Construct date and time objects
      Calendar dateCal = Calendar.getInstance();
      dateCal.setTime(date);
      Calendar timeCal = Calendar.getInstance();
      timeCal.setTime(time);
    
      // Extract the time of the "time" object to the "date"
      dateCal.set(Calendar.HOUR_OF_DAY, timeCal.get(Calendar.HOUR_OF_DAY));
      dateCal.set(Calendar.MINUTE, timeCal.get(Calendar.MINUTE));
      dateCal.set(Calendar.SECOND, timeCal.get(Calendar.SECOND));
    
      // Get the time value!
      date = dateCal.getTime();
    

提交回复
热议问题