-3

I need to get difference between below 2 dates. Both dates are in String format.

String sDate1 = Thu Jun 26 13:45:32 IST 2018;
String sDate2 = Thu Jul 16 03:25:37 IST 2018;

Date date1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(sDate1);
Date date2 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(sDate2)
long dateDiff2 = date2.getTime() - date1.getTime();
  • 2
    And your question is? – Jens Jul 30 '18 at 06:19
  • Possible duplicate [Difference between two DateTime objects in minutes](https://stackoverflow.com/questions/50884294/difference-between-two-datetime-objects-in-minutes/50884318#50884318); [Java SimpleDateFormat format issue with yyyy](https://stackoverflow.com/questions/25776787/java-simpledateformat-format-issue-with-yyyy/25777559#25777559); [Java finding difference between times](https://stackoverflow.com/questions/32961391/java-finding-difference-between-times/32961667#32961667) – MadProgrammer Jul 30 '18 at 06:19
  • I don't see the reasons for the downvotes, but what is your question? Your code actually looks fine to me. There is also a Java 8 way of doing this now. – Tim Biegeleisen Jul 30 '18 at 06:20
  • -2 down vote favorite I need to get difference between below 2 dates. Both dates are in String format. –  Jul 30 '18 at 06:20
  • 2
    And what is your problem? It looks like you already have a difference in milliseconds. – LuCio Jul 30 '18 at 06:20
  • but it is not working –  Jul 30 '18 at 06:21
  • @Samankumara in what way is it not working? – Andy Turner Jul 30 '18 at 06:22
  • 1
    @Samankumara *is not working* is not an error description. Add the result you get and the expected result – Jens Jul 30 '18 at 06:23
  • 2
    @TimBiegeleisen The primary reasons for the downvotes come down to the fact that the question is common enough to have a number of available answers AND you should NEVER do simply mathematical calculations on date/time values, there are too many rules governing how these calculations should be done not to use an appropriate API or library – MadProgrammer Jul 30 '18 at 06:23
  • @MadProgrammer I'll agree with you on the grounds that the question shows zero research on SO. But, maybe the OP wrote that code himself. Oh well, water under the bridge :-) – Tim Biegeleisen Jul 30 '18 at 06:24
  • 1
    @Samankumara So, you have two questions. 1- How to convert a string value to a date value and 2- How to calculate the difference between those date values. Both questions are common and have been asked before – MadProgrammer Jul 30 '18 at 06:24
  • It's working now, thank you –  Jul 30 '18 at 06:26
  • 1
    Possible duplicate of [Difference between two DateTime objects in minutes](https://stackoverflow.com/questions/50884294/difference-between-two-datetime-objects-in-minutes) – Moia Jul 30 '18 at 06:28
  • You should migrate to the newer date and time classes available in the `java.time` package. [Read more about it here.](https://docs.oracle.com/javase/tutorial/datetime/iso/legacy.html) – MC Emperor Jul 30 '18 at 07:14
  • @Moia Not a duplicate of that. This Question is about moments, that Question is about `LocalDateTime` values (not moments). – Basil Bourque Jul 30 '18 at 07:43

2 Answers2

1

Quote marks

You forgot the double-quote marks around your input strings.

java.time

You are using terrible old classes that were outmoded years ago by the java.time classes.

Parsing

Parse your input strings using DateTimeFormatter to get ZonedDateTime objects. Search Stack Overflow as this has been covered many times.

Elapsed

To calculate elapsed hours, minutes, and seconds, pass those ZonedDateTime objects to the Duration.between method. For years-months-days, use Period. Search Stack Overflow as this has been covered many times.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

Try this:-

String sDate1 = Thu Jun 26 13:45:32 IST 2018;
String sDate2 = Thu Jul 16 03:25:37 IST 2018;

Date d1= new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(sDate1);
Date d2= new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(sDate2); 
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000);
System.out.println("Time in seconds: " + diffSeconds + " seconds.");
System.out.println("Time in minutes: " + diffMinutes + " minutes.");
System.out.println("Time in hours: " + diffHours + " hours.");

Or you can use TimeUnit:-

long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(diff );
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(diff );
long diffInHours = TimeUnit.MILLISECONDS.toHours(diff );
Ravindra Kumar
  • 1,842
  • 14
  • 23
  • These terrible classes were supplanted years ago by the *java.time* classes. Poor advice in 2018. – Basil Bourque Jul 30 '18 at 07:53
  • I am agree with you but for bigners it is right way to do other wise he can use jodaTime also. – Ravindra Kumar Jul 30 '18 at 07:55
  • No, those classes are a *terrible* thing to show beginners, with a wretched design and awful hacks that will confound anyone trying to learn programming and OOP. And the *Joda-Time* project is now in maintenance mode, with its inventor having moved on to create *java.time*. – Basil Bourque Jul 30 '18 at 08:07