how can you combine date and sql time into a single date time in java?

176 views Asked by At

Using java Calendar how can you combine the start date, day and starttime?

For example:

If the start date is 9/8/2020. The day is 2 and the start time is 8:00 AM then how can we obtain a java date that is 9/9/2020 8:00 AM. Here is my unsuccessful attempt.

def startDateTime(Date startDate, int day, java.sql.Time startTime){    

        def date
        date = Calendar.getInstance()
        date.setTime(startDate)

        //adding day. since day 1 is the first day we need to add day - 1
        date.add(Calendar.DATE, day - 1)   
        // setting the time from startTime
        date.setTimeInMillis(startTime.getTime())
    
        return date.getTime()

    }

Thanks for the help.

1

There are 1 answers

3
Smile On BEST ANSWER

You are calling date.setTime(startDate) and date.setTimeInMillis(startTime.getTime()). 2nd method is overriding the time set in 1st method. You should create 2 separate instances of Calendar.

Here is how you can achieve this

  1. Create separate Calendar instances for startDay and startTime
  2. Construct a new Calendar object from separate Calendar objects created in #1 & add day as per requirement

Here is the complete code:

import java.sql.Time;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

public class ProofOfConcept {

    public static void main(String[] args) {
        int day = 2;

        Time startTime = new Time(1, 1, 1);
        Calendar timeCalendar = Calendar.getInstance();
        timeCalendar.setTime(startTime);

        Date startDate = new Date();
        Calendar startDateCalendar = Calendar.getInstance();
        startDateCalendar.setTime(startDate);

        /* Only java 8 and above
        LocalDateTime localDateTime = LocalDateTime.of(startDateCalendar.get(Calendar.YEAR), startDateCalendar.get(Calendar.MONTH) + 1, startDateCalendar.get(Calendar.DAY_OF_MONTH),
                timeCalendar.get(Calendar.HOUR), timeCalendar.get(Calendar.MINUTE), timeCalendar.get(Calendar.SECOND));
        localDateTime = localDateTime.plusDays(day);
        System.out.println("localDateTime : " + localDateTime);
        Date dateFromLocalDateTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        System.out.println("dateFromLocalDateTime : " + dateFromLocalDateTime);*/

        Calendar result = Calendar.getInstance();
        result.set(startDateCalendar.get(Calendar.YEAR), startDateCalendar.get(Calendar.MONTH), startDateCalendar.get(Calendar.DAY_OF_MONTH) + 2,
                timeCalendar.get(Calendar.HOUR), timeCalendar.get(Calendar.MINUTE), timeCalendar.get(Calendar.SECOND));

        Date date = result.getTime();
        System.out.println("date : " + date);
    }

}

Output:

date : Tue Sep 08 01:01:01 IST 2020

Note : I suggest using java.time.* packages over java.util.*. Why? Check this. But this is only available in java 8. (Though, you can use joda time in versions below 8).


Edit : Moving Ole V.V. 's comment to answer.

For Java 7, I suggest using java.time, the modern Java date and time API, through the backport, ThreeTen Backport.

static LocalDateTime startDateTime(Date startDate, int day, java.sql.Time startTime) {
    LocalTime startLocalTime = DateTimeUtils.toLocalTime(startTime);
    return DateTimeUtils.toInstant(startDate)
            .atZone(ZoneId.systemDefault())
            .toLocalDate()
            .plusDays(day - 1)
            .atTime(startLocalTime);
}