example

i will enter
timein: 1:30
timeout: 5:00

how to know their time difference?in timeformat


or how to accept a timeformat input from the user

Recommended Answers

All 11 Replies

You can use the SimpleDateFormat class to parse a time in format "hh:mm" and convert that to a Date object. You can convert two date objects to millisecs and subtract one from the other to get the time difference. You can finally use that to create a new Date object for the difference, and format it with the same SimpleDateFormat

Details, as always, are in the API JavaDoc

how to get the time diff? i cant seem to figure it out.. example pls

You can use the getTime() method to convert your Data object to milliseconds. Do that for both dates and subtract one answer from the other. That gives you the diff in mSecs.

example code sir ..

ok

String time1 = "1:00",time2="1:30";


	  DateFormat sdf = new SimpleDateFormat("H:mm");
	 
	  Date date = sdf.parse(time1);
	  Date date1 = sdf.parse(time2);
	  long diff = date1.getTime() - date.getTime();

	  
	      System.out.println((diff / (1000*60*60)));

That's a good start. Does it give you the result you wanted?

the result is 0

Yes it is. Difference is 30 mins, ie 30*60*1000 mSec. You display the difference in hours, not minutes, and in integer arithmetic this is truncated to 0 hours.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.