I have two strings of the form "dd-MM-YYYY HH:mm:ss". How can I find the difference in hours between the two Strings ?
            Asked
            
        
        
            Active
            
        
            Viewed 129 times
        
    -2
            
            
        - 
                    2[I downvoted because research must be done to ask a good question](http://idownvotedbecau.se/noresearch/). – Ole V.V. Aug 19 '20 at 06:08
- 
                    Do you know which programming language you want to use? I understand that many are used on Android. – Ole V.V. Aug 19 '20 at 06:10
- 
                    Also see [Android difference between Two Dates](https://stackoverflow.com/questions/21285161/android-difference-between-two-dates). I’m immodest enough to recommend [my own answer there](https://stackoverflow.com/a/45169929/5772882). And also see [my answer here](https://stackoverflow.com/a/48529971/5772882). – Ole V.V. Aug 19 '20 at 06:24
2 Answers
1
            
            
        You can converted the String into Date objects, then you may compare them.
Something like below
 SimpleDateFormat sdformat = new SimpleDateFormat("dd-MM-YYYY HH:mm:ss");
  Date d1 = sdformat.parse("04-05-2020 02:33:12");
  Date d2 = sdformat.parse("05-05-2020 02:33:12");
  System.out.println("The date 1 is: " + sdformat.format(d1));
  System.out.println("The date 2 is: " + sdformat.format(d2));
  if(d1.compareTo(d2) > 0) {
     System.out.println("Date 1 occurs after Date 2");
  } else if(d1.compareTo(d2) < 0) {
     System.out.println("Date 1 occurs before Date 2");
  } else if(d1.compareTo(d2) == 0) {
     System.out.println("Both dates are equal");
  }
 
    
    
        Qben
        
- 2,617
- 2
- 24
- 36
 
    
    
        vikas kumar
        
- 10,447
- 2
- 46
- 52
- 
                    Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Aug 19 '20 at 06:07
- 
                    1thanks @OleV.V. for the suggestion, yeah i guess now we should move on to java 8 date and time. api's will use from now going forward. – vikas kumar Aug 19 '20 at 06:28
0
            You can do it using SimpleDateFormat and Date:
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
public static long date_hour_diff(String start_date, String end_date) { 
  SimpleDateFormat simple_date_format = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); 
  try {  
    Date parsed_start_date = simple_date_format.parse(start_date); 
    Date parsed_end_date = simple_date_format.parse(end_date); 
  
    long time_diff = d2.getTime() - d1.getTime(); 
    long diff_hours = (time_diff / (1000 * 60 * 60)) % 24; 
    return diff_hours;
  } catch (ParseException e) { 
      Log.e(TAG, "Date is not valid.");
  } 
}
 
    
    
        Fede
        
- 321
- 1
- 9
- 
                    Consider throwing away the long outmoded and notoriously troublesome `SimpleDateFormat` and friends. See if you either can use [desugaring](https://developer.android.com/studio/write/java8-support-table) or add [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) to your Android project, in order to use java.time, the modern Java date and time API. It is so much nicer to work with. – Ole V.V. Aug 19 '20 at 06:07
 
    