Question

I have two Jdatechooser(named as firstdate and lastdate) and Jspinner(named as starttime and endtime) in a gui.

The scenario is,

1.if i open gui i will get the current time and set it in endtime and currenttime-1 in starttime(the code is below),

Calendar cal = Calendar.getInstance();
cal.add(Calendar.HOUR, -1);
Date oneHourBack = cal.getTime();
String timeStamp = new SimpleDateFormat("HH:mm:ss").format(oneHourBack);
Date date = new SimpleDateFormat("HH:mm:ss").parse(timeStamp);
starttime.setValue(date);

2.For both the Jdatechooser i set the current date.

3.If current time is 00:44:36 (HH:mm:ss), in starttime(Jspinner) i have to set 23:44:36, with this i have to set the firstdate(Jdatechooser) value to previous day date instead of current date.

for this am trying the following way,

Calendar currentTime = Calendar.getInstance();
Date curHr = currentTime.getTime();
String curtime = new SimpleDateFormat("HH").format(curHr);
int timeCheck = Integer.parseInt(curtime);

if(timeCheck > 00 && timeCheck < 01){
 //code to set previous day's  
 date                                       
}

is this the way to do it? or is there any better way available? Please help

Was it helpful?

Solution

You should be able to use the oneHourBack Date value as the value for the lastdate JDateChooser, as not only has the time been rolled back, but so has the date value, for example...

Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 44);
cal.set(Calendar.SECOND, 36);

Date startTime = cal.getTime();

cal.add(Calendar.HOUR, -1);
Date endTime = cal.getTime();

System.out.println("startTime = " + startTime);
System.out.println("endTime = " + endTime);

Outputs...

startTime = Thu Feb 06 00:44:36 EST 2014
endTime = Wed Feb 05 23:44:36 EST 2014

This is the nice thing about Calendar

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top