-4

I have two Strings of the format "hh:mm:ss".I want to obtain only the hour part of both strings and find the difference between them.How should i do it.I tried Dateformat and parse function but when i subtract, it tells bad operand types. Code:

 String ta="";
 String tb="";
 int diff;
 Scanner s=new Scanner(System.in);
 ta=s.nextLine();
 tb=s.nextLine();
 DateFormat sdf = new SimpleDateFormat("hh");
 Date date1 = sdf.parse(ta);
 Date date2 = sdf.parse(tb);
 diff=sdf.format(date1)-sdf.format(date2);
 if(diff==20)
     System.out.println("Twenty");
sai
  • 3
  • 2
  • 2
    If you want help fixing your code, you need to [edit] your question to include the code that's giving the error so we can see what you did. Describing your code ("I tried Dateformat") isn't helpful. – azurefrog Nov 30 '17 at 15:16
  • You should search for an answer before posting a question... Please see this question: https://stackoverflow.com/questions/1555262/calculating-the-difference-between-two-java-date-instances – Gomes Nov 30 '17 at 15:18
  • Is the input a duration (that implies possible hour values greater than 24) or a clock time? – Meno Hochschild Nov 30 '17 at 15:29
  • No,the strings will vary between "24:59:59" to "00:00:00" – sai Nov 30 '17 at 15:38
  • Well, if "24:59:59" is allowed then this excludes any answers based on either `SimpleDateFormat` or `java.time.DateTimeFormatter`. You need a duration parser, either home-made like in the answer of @rememberthetitans (but better with extra hour adjustment as sketched in my comment below) or via a 3rd-party library. Standard Java has no duration parser. – Meno Hochschild Nov 30 '17 at 15:46

2 Answers2

3

You probably don't need to convert it to Date at all:

String time1 = "07:00:55";
String time2 = "05:00:33";
String time1Sub = time1.substring(0, time1.indexOf(':'));
String time2Sub = time2.substring(0, time2.indexOf(':'));
System.out.println(Integer.parseInt(time1Sub) - Integer.parseInt(time2Sub));

Edit:

As per comment below, if you want full hours, you can check the difference in the hour slots (feel free to edit if there are better ways to do this).

String hour1 = time1.substring(time1.lastIndexOf(':') - 2, time1.lastIndexOf(':'));
String hour2 = time2.substring(time1.lastIndexOf(':') - 2, time2.lastIndexOf(':'));
int result = Integer.parseInt(time1Sub) - Integer.parseInt(time2Sub);
System.out.println(Integer.parseInt(hour1) < Integer.parseInt(hour2) ? result - 1 : result);
  • I'm. Not sure why this is down voted. It seems reasonable. – Glen Pierce Nov 30 '17 at 15:29
  • Yes, it is simple and straightforward and even better than the suggested alternative to let `LocalTime` parse the input because this answer is capable to process hour values >= 24 (for the case, the input is meant as duration and not clock time). However, the hour delta should probably be reduced by one for the case if the subhour-part of first input is bigger than the subhour-part of second input to be compared. – Meno Hochschild Nov 30 '17 at 15:31
  • @MenoHochschild added optional edit for *full* hours. – rememberthetitans Nov 30 '17 at 16:25
0

Using java.time:

public static void main(String[] args) {

    java.time.LocalTime a = java.time.LocalTime.parse("12:00:00");
    System.out.println(a.getHour());

}
Merch0
  • 594
  • 5
  • 17