-1

I have to calculate the difference bettween two dates where is one from the user`s input and the other is the current time. Here is what I have so far:

long time2 = System.currentTimeMillis();
System.out.print("Enter Time-in(hh:mm)");
String start = input.next();
String newTime[] = start.split(":");
String h = newTime[0];
String m = newTime[1];
String s = newTime[2];

int newH = Integer.parseInt(h);
int newM = Integer.parseInt(m);

LocalTime time1 = LocalTime.now();
long hr=ChronoUnit.HOURS.between(time1,time2);
System.out.println("Total number of hours: " + hr);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
khavirna
  • 21
  • 5
  • 1
    Start by creating a new `LocalTime` (or `LocalDateTime`) from the time values the user enters – MadProgrammer Jul 19 '17 at 23:39
  • `LocalTime().of(newH, newM)` for example, then you have something to which you can compare to – MadProgrammer Jul 19 '17 at 23:41
  • Can you please remove the `javascript` tag? – Vivick Jul 19 '17 at 23:43
  • `LocalTime.of(newH, newM)` or `LocalTime.parse(start)` – shmosel Jul 19 '17 at 23:55
  • @MadProgrammer im sorry im not sure what to compare it with what.... – khavirna Jul 19 '17 at 23:57
  • 1
    @shmosel (like this?) LocalTime time1 = LocalTime.now(); LocalTime.parse(start); long hr=ChronoUnit.HOURS.between(time1,time2); System.out.println("Total number of hours: "+hr); – khavirna Jul 19 '17 at 23:58
  • You forgot about `time2`. – shmosel Jul 20 '17 at 00:00
  • `long hr=ChronoUnit.HOURS.between(LocalTime().of(newH, newM),LocalTime.now());` ... – MadProgrammer Jul 20 '17 at 00:03
  • Possible duplicate of [Calculating the difference between two Java date instances](https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances) – Zabuzard Jul 20 '17 at 00:17
  • 1
    A tip for your next questions on Stack Overflow, it’s easier to help you if you explain clearly and precisely how your code fails to solve your problem; that is, the difference between desired behaviour and your program’s observed behaviour. – Ole V.V. Jul 21 '17 at 05:53

1 Answers1

2

Try This :

    System.out.print("Enter Time-in(hh:mm)");
    String start=input.next(); //make sure it have "hh:mm" format
    LocalTime userTime = LocalTime.parse(start);
    LocalTime currentTime = LocalTime.now();


    long diff = ChronoUnit.HOURS.between(currentTime, userTime);
Gusti Arya
  • 1,281
  • 3
  • 15
  • 32
  • 2
    Should solve OPs question, good! However you could improve the quality of your answer by also explaining what the code does. For example how `LocalTime` works and which pattern it expects at `LocalTime#parse`. It would also be helpful to provide a link from the Java-API like: https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html#parse-java.lang.CharSequence- – Zabuzard Jul 20 '17 at 00:20
  • i tried the code but after entering the time in (hh:mm) it shows error.... – khavirna Jul 20 '17 at 00:39
  • so when entering the number do we have to split?? – khavirna Jul 20 '17 at 00:46
  • System.out.print("Enter Time-in hh:mm"); String start=input.next(); LocalTime userTime = LocalTime.parse(start); LocalTime currentTime=LocalTime.now(); long hr=ChronoUnit.HOURS.between(currentTime,userTime); System.out.println("Total number of hours: "+hr); – khavirna Jul 20 '17 at 00:48
  • no you dont have to split anything, you only have to enter time with hh:mm format e.g : "10:30" – Gusti Arya Jul 20 '17 at 00:48