Re: Comparing time
mike wrote:
Hi,
How can I compare time without using any date information?
I have the following.
public boolean myMethod (Date date){
boolean state = false;
String time1 = "08:00";
String time2 = "17:00";
//Determine day
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
int hours = calendar.get(Calendar.HOUR_OF_DAY);//0-23
int minutes = calendar.get(Calendar.MINUTE);//0-59
//I need to check that my minutes and hours are within time 1 and 2.
........
return state;
}
I had a similar problem. I was getting information tagged with a 4
digit time that I needed to compare with now. In that case I just added
the month day and year to the time string, parsed it with a simple date
format, got the time and compared them.
// converts a string time HH:mm to a time today
private Date StringToDate(String hourStr) throws ParseException {
SimpleDateFormat sdf1 = new SimpleDateFormat("MMddyy");
SimpleDateFormat sdf2 = new SimpleDateFormat("MMddyyHH:mm");
String dateStr = sdf1.format(new Date());
Date then = sdf2.parse(dateStr + hourStr);
return then;
}
Date now = new Date();
now.getTime() ?? StringToDate("12:00");
Of course if you have to cross day boundaries you'll have to make some
changes.
--
Knute Johnson
email s/nospam/knute2008/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access