I am creating a Java GUI on a clock which asks the user to change the time from current time. I have the system time and the user time. What should be the code so that the user time gets displayed. The foolowing code i am using :
Date curDate = new Date();
//changeTime i have got from the user
double time=0;
if(curDate.getTime()- changeTime.getTime() > 0){
time = curDate.getTime() - changeTime.getTime();
time = time/(1000);
}
else{
time = changeTime.getTime()-curDate.getTime();
time = time/(1000);
}
if(changeTime.getHours()==0 && changeTime.getMinutes()==0){
time=0;
}
curDate.setHours(curDate.getHours()+(int)time/(3600));
curDate.setMinutes(curDate.getMinutes()+((int)time/60)%60);
A
new Date()is “intended to reflect coordinated universal time (UTC).” What the user sees on the clock depends on theTimeZone, which can be specified in theDateFormatused to render the date. The example below prints the same instant in GMT and a selection of time zones from New York to Berlin.Addendum: I find it helpful to think of
Dateas a model andDateFormatas a view of that model.package date;