Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Working out time difference between now and 18:00 1

Status
Not open for further replies.

bobrivers2003

Technical User
Oct 28, 2005
96
GB
I want to work out the time difference (in hours) between and and 18:00.

I have tried:

Date futDate = new Date("23/05/2006 18:00:00");
long currentTime = System.currentTimeMillis();
long futTime = futDate.getTime();

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
sdf.setTimeZone((TimeZone.getTimeZone("GMT")));
long left = futTime - currentTime;
System.out.println(sdf.format(new Date(left)));


Two things:

If I run it at 12:48:00 the time difference will be 06:11:00 instead of 05:11:00, how to I get the correct hour?

Once I have the correct hour diffence how would I excract just the hour and assign to a var?


Any help would be greatly appreciated

Many Thanks
 
Maybe this helps:

Code:
import java.util.Calendar;
class hour 
{
	public static void main(String[] args) 
	{
		Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int dif = hour -18;
		System.out.println(dif>=0?dif+" hours past from 18":(dif*(-1))+" hours left for 18");
	}
}

Cheers,
Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top