1

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.

kofhearts
  • 3,607
  • 8
  • 46
  • 79
  • 1
    For Java 7 I suggest using java.time, the modern Java date and time API, through the backport, [ThreeTen Backport](https://www.threeten.org/threetenbp/). `DateTimeUtils.toInstant(startDate).atZone(ZoneId.systemDefault()).toLocalDate().plusDays(day - 1).atTime(DateTimeUtils.toLocalTime(startTime))` (I trust you to break it up into smaller statements). – Ole V.V. Sep 06 '20 at 07:24
  • kofhearts, I wrote [a new and modern answser for you here](https://stackoverflow.com/a/63761970/5772882). It includes a section on Java 6 and 7. It doesn’t include how to find the right day, but you can probably take that from my comment above and/or search for how. – Ole V.V. Sep 06 '20 at 08:02

1 Answers1

1

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);
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Smile
  • 3,832
  • 3
  • 25
  • 39
  • thanks for the solution but i am using java 7 and i dont think i have access to java.time package. can you suggest java 7 approach? – kofhearts Sep 06 '20 at 06:31
  • Updated my answer – Smile Sep 06 '20 at 06:33
  • 1
    @Smile You may well let your fine Java 8 code stand as a supplement for other readers that are using Java 8 or later (and for kofhearts once s/he upgrades). Also feel free to include my code for Java 7 and the backport from my comment if you like. – Ole V.V. Sep 06 '20 at 07:27