Date object to Calendar [Java] -
i have class movie in have start date, duration , stop date. start , stop date date objects (private date startdate ...) (it's assignment cant change that) want automatically calculate stopdate adding duration (in min) startdate.
by knowledge working time manipulating functions of date deprecated hence bad practice on other side see no way convert date object calendar object in order manipulate time , reconvert date object. there way? , if there best practice
what creating instance of gregoriancalendar
, set date
start time:
date date; calendar mycal = new gregoriancalendar(); mycal.settime(date);
however, approach not use date
@ all. use approach this:
private calendar starttime; private long duration; private long startnanos; //nano-second precision, less precise ... this.starttime = calendar.getinstance(); this.duration = 0; this.startnanos = system.nanotime(); public void setendtime() { this.duration = system.nanotime() - this.startnanos; } public calendar getstarttime() { return this.starttime; } public long getduration() { return this.duration; }
in way can access both start time , duration start stop. precision of course.
Comments
Post a Comment