-2
public class Testdate_time {

public void testdate() throws ParseException {

    String in_time = "2018 May 07 09:20:01 AM";
    String out_time = "2018 May 07 10:08:29 AM";
    int hours = 0;
    int mintues = 0;
    org.joda.time.format.DateTimeFormatter format = DateTimeFormat.forPattern("yyyy MMM dd hh:mm:ss a");
    DateTime date1 = format.parseDateTime(in_time);
    DateTime date2 = format.parseDateTime(out_time);
    hours = Hours.hoursBetween(date1, date2).getHours();

    String strhours = String.valueOf(hours).replaceAll("-", "");
    mintues = date1.getMinuteOfHour() - date2.getMinuteOfHour();
    String Strminutes = String.valueOf(mintues).replaceAll("-", "");
    System.out.println("hours" + strhours);
    System.out.println("minutes" + Strminutes);

}

public static void main(String[] args) throws ParseException {

    new Testdate_time().testdate();

}

i have used jodatime also and simpledateformat http://www.joda.org/joda-time/userguide.html

Output:

hours0
minutes12
il_raffa
  • 5,090
  • 129
  • 31
  • 36
  • 1
    Because you’re getting minutes separately, so your minutes output is 20 - 8, hence 12. – achAmháin May 08 '18 at 06:08
  • #but why not hours calculating() – Vivek Srivastava May 08 '18 at 06:11
  • 2
    There is less than 1 hour between `09:20` and `10:08`, so `hoursBetween()` returns `0`. There is 12 minutes between `08` and `20`, so `minutes = 12`. What's so hard about understanding that? – Andreas May 08 '18 at 06:12
  • 1
    Because there isn’t one full hour between your dates. – achAmháin May 08 '18 at 06:12
  • @notyou hours = Hours.hoursBetween(date1, date2).getHours() and not calculating in hours – Vivek Srivastava May 08 '18 at 06:13
  • @VivekSrivastava It *is* calculating hours between, and less than 1 is **0**. – Andreas May 08 '18 at 06:14
  • Re read my comment above, and also @Andreas ‘ – achAmháin May 08 '18 at 06:15
  • 1
    BTW: You don't say what you expected. You should be expecting a good calculation to say `0 hours 48 minutes`, but for that you should calculate `diff = minutesBetween()`, not discard the sign, and calculate `hours = diff / 60` and `minutes = diff % 60`. – Andreas May 08 '18 at 06:18
  • I have downvoted because you don’t specify expected result and because you don’t show any search and research effort (I believe you should have been able to find a number of similar questions and answers with a moderate effort). – Ole V.V. May 08 '18 at 08:49

1 Answers1

1

To correctly calculate hours and minutes between two dates, using Java 8 Time API and ignoring Daylight Savings Time, you'd do it like this:

String in_time = "2018 May 07 09:20:01 AM";
String out_time = "2018 May 07 10:08:29 AM";
DateTimeFormatter format = DateTimeFormatter.ofPattern("uuuu MMM dd hh:mm:ss a", Locale.US);
LocalDateTime date1 = LocalDateTime.parse(in_time, format);
LocalDateTime date2 = LocalDateTime.parse(out_time, format);

long diff = ChronoUnit.MINUTES.between(date1, date2);
System.out.printf("%d hours %d minutes%n", diff / 60, diff % 60);

Or, if you don't want to do the math yourself, use Duration:

Duration duration = Duration.between(date1, date2);
System.out.printf("%d hours %d minutes%n", duration.toHours(), duration.toMinutesPart());

Output (from both)

0 hours 48 minutes
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • 1
    Maybe I just hate any kind of arithmetic applied to date/time values, but you could do something more [like this](https://stackoverflow.com/questions/50096083/convert-number-of-seconds-into-hhmm-without-seconds-in-java/50096554#50096554) which relies entirely on the API – MadProgrammer May 08 '18 at 06:27
  • 1
    @MadProgrammer Added `Duration` solution too. – Andreas May 08 '18 at 06:33