-8

I've created this program to calculate the time between startWork and finishWork but I cant seem to figure out how to calculate time... This is my Interface. Image of GUI

Just wanting to know a way of approaching this calculation.

Thanks

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • see this : https://stackoverflow.com/questions/18686575/difference-between-two-times-in-minutes – Amol Raje Jan 27 '18 at 12:51
  • Please include your code in the question instead of a picture it. – ASutherland Jan 27 '18 at 12:55
  • I haven't got the code for the calculation as I'm not sure where to start... – Trent Wright Jan 27 '18 at 12:56
  • 2
    Possible duplicate of [Java 8: Calculate difference between two LocalDateTime](https://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime) – Joe C Jan 27 '18 at 13:09
  • This has been asked and answered before. What did your search bring up? – Ole V.V. Jan 27 '18 at 13:36
  • Really rather a duplicate of [Calculating the time difference between two times in Java](https://stackoverflow.com/questions/46876974/calculating-the-time-difference-between-two-times-in-java). – Ole V.V. Jan 27 '18 at 13:42
  • 1
    You should post a sample code for code review - avoid asking solution. – Nirmal Jan 27 '18 at 13:57
  • @JoeC No, not a duplicate of [that Question](https://stackoverflow.com/questions/25747499/java-8-calculate-difference-between-two-localdatetime) which is about much larger scale of time. And the Answer should be using `ZonedDateTime` rather than `LocalDateTime` to account for Daylight Saving Time (DST) and other anomalies. – Basil Bourque Jan 27 '18 at 16:19

2 Answers2

2

Use java.time as suggested by Ole V.V.:

String time1 = "07:00:00";
String time2 = "15:30:12";
LocalTime t1 = LocalTime.parse(time1);
LocalTime t2 = LocalTime.parse(time2);
Duration diff = Duration.between(t1, t2);
System.out.println(diff.toString());

Prints:

PT8H30M12S

Amol Raje
  • 928
  • 3
  • 9
  • 16
  • So with that code from 07:00:00 to 15:00:00 it prints 8 which is correct If I change it to 07:00:00 to 15:30:00 it prints 8 which is incorrect How would I got about changing the output to print 8.5? – Trent Wright Jan 27 '18 at 13:05
  • 2
    Please note that `java.util.Date` is legacy. – Joe C Jan 27 '18 at 13:07
  • 1
    Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. Today we have so much better in [`java.time`, the modern Java date and time API,](https://docs.oracle.com/javase/tutorial/datetime/) and its `DateTimeFormatter`. Not to mention its `LocalTime`, which matches the needs much better than the outmoded `Date`. – Ole V.V. Jan 27 '18 at 13:26
  • @OleV.V. Thank you i have updated answer.. – Amol Raje Jan 27 '18 at 14:04
  • Using `LocalTime` fails to account for anomalies such as Daylight Saving Time(DST). – Basil Bourque Jan 27 '18 at 16:21
2

Use the Duration class from java.time to represent your working time. Let Duration.between() do the calculation for you, passing two LocalTime or two ZonedDateTime objects to it as appropriate. The latter will take transitions to and from summer time (DST) into the calculation if such a transition happens during the working hours.

If the time is entered as for example 1530 or 3:30pm, define a DateTimeFormatter to parse it into LocalTime.

Duration objects can be summed using its plus method, so you can calculate the hourly and monthly working time and so on.

To format the working time into for example 8.5 (for 8 hours 30 minutes), use the toMinutes method, then convert to double before you divide by 60 (I would declare the constant 60 as final double minutesPerHour = TimeUnit.HOURS.toMinutes(1);).

java.time

java.time is the modern Java date and time API. It came out nearly 4 years ago to replace the outdated and poorly designed date and time classes from Java 1.0 and 1.1 from the last years of the previous millennium.

Link: Oracle Tutorial trail Date Time

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161