-1

I'm working on a program that will return the difference in time (how many hours, mins) that has elapsed between two points in time. I need to be able to calculate the time, also, if the times are, say, 11:00 pm to 3:00 am. How would I go about doing this, as well as calculating the time from say 8:00 am to 5:00 pm?

Thanks!

2 Answers2

1
public static void main(String[] av) {
    /** The date at the end of the last century */
    Date d1 = new GregorianCalendar(2000, 11, 31, 23, 59).getTime();

    /** Today's date */
    Date today = new Date();

    // Get msec from each, and subtract.
    long diff = today.getTime() - d1.getTime();

    System.out.println("The 21st century (up to " + today + ") is "
        + (diff / (1000 * 60 * 60 * 24)) + " days old.");
}
tawheed
  • 5,565
  • 9
  • 36
  • 63
CAA
  • 968
  • 10
  • 27
1

If someone is looking for a solution, you can do like this answer

String time = "22:00-01:05";
String[] parts = time.split("-");

LocalTime start = LocalTime.parse(parts[0]);
LocalTime end = LocalTime.parse(parts[1]);
if (start.isBefore(end)) { // normal case
    System.out.println(Duration.between(start, end));
} else { // 24 - duration between end and start, note how end and start switched places
    System.out.println(Duration.ofHours(24).minus(Duration.between(end, start)));
}
pcsantana
  • 599
  • 5
  • 12