java - How to get the time of the day in milliseconds? -
i want time of day in milliseconds, not day have specific date, time. made something, thought worked, went debugging , concluded doesn't work how want to.
i want use check if current time between both specified starttime
, endtime
.
long starttime = settings.getlong("starttime", 0); long endtime = settings.getlong("endtime", 0); if ((currenttime.getmillis() >= starttime) && (currenttime.getmillis() <= endtime)) { //do stuff here }
how setting time of propeties starttime
, endtime
:
calendar starttime = calendar.getinstance(); starttime.set(calendar.hour_of_day, 16); starttime.set(calendar.minute, 00); editor.putlong("starttime", starttime.gettimeinmillis()); calendar endtime = calendar.getinstance(); endtime.set(calendar.hour_of_day, 16); endtime.set(calendar.minute, 00); endtime.add(calendar.hour_of_day, 11); editor.putlong("endtime", endtime.gettimeinmillis()); editor.commit();
however mean both starttime
and endtime
have specific date attached it.
i hope explained well, appreciated!
avoid milliseconds
no need mess milliseconds purpose. using milliseconds date-time confusing , error-prone.
what need decent date-time library rather notoriously troublesome bundled java.util.date & .calendar classes.
joda-time
if want ignore dates , ignore time zones, here's example code using localtime
class offered third-party free-of-cost joda-time library.
localtime start = new localtime( 10, 0, 0 ); localtime stop = new localtime( 14, 30, 0 ); localtime target = localtime.now(); boolean isnowinspan = !( ( target.isbefore( target ) ) | ( target.isafter( stop ) ) );
adjust last line according business logic needs. might want:
- the beginning , ending inclusive
- the beginning , ending exclusive
- "half-open" beginning inclusive , ending exclusive
(usually best date-time work)
dump console…
system.out.println( "start: " + start ); system.out.println( "stop: " + stop ); system.out.println( "target: " + target ); system.out.println( "isnowinspan: " + isnowinspan );
when run…
start: 10:00:00.000 stop: 14:30:00.000 target: 23:49:37.779 isnowinspan: false
another example
time-of-day-only not right way go. when new date-time work, naïve programmer may @ first think time-only simplifies things. on contrary, example shows how spinning around clock creates complications. using date+time+timezone best approach in long run.
localtime = localtime.now(); localtime start = new localtime( 13, 0, 0, 0 ); localtime stop = start.plushours( 11 ); system.out.println( "now: " + ); system.out.println( "start: " + start ); system.out.println( "stop: " + stop ); if ( now.isafter( start ) ) { system.out.println( "after start" ); } if ( now.isbefore( stop ) ) { system.out.println( "before stop" ); }
when run…
now: 14:00:32.496 start: 13:00:00.000 stop: 00:00:00.000 after start
java.time
java 8 brings new java.time package, inspired joda-time, defined jsr 310.
in java.time, find localtime class similar 1 in joda-time.
Comments
Post a Comment