Concise way to handle the Calendar object in Java -


some of used methods in calendar object return void , not chainable, such add(). more so, class appears mutable if add, example, day calendar object, original calendar object modified new date.

i have written this:

calendar currenttime = calendar.getinstance(timezone.gettimezone("utc")); // report.time() returns calendar instance if(report.time().after(currenttime.gettime().add(calendar.date, 1)) || currenttime.after( report.time().add(calendar.month, 5) )){     validtime=false; } 

this not compile because add() methods being used in parameters of after() return void , after() expecting calendar instance, not void.

the other solution can think of ugly , bloated:

calendar currenttime = calendar.getinstance(timezone.gettimezone("utc")) calendar nextday = calendar.getinstance(timezone.gettimezone("utc")); nextday.add(calendar.date, 1)) calendar nextmonths = calendar.getinstance(timezone.gettimezone("utc")); nextmonths.add(calendar.month, 5)) if(report.time().after(nextday) || currenttime.after(nextmonths )){    validtime=false; } 

is there more concise way this?

i'm not sure business logic regarding valid range of date-times, code seems contradictory.

here's code in joda-time may going in right direction. let's target date-time valid if occurs between 3 days ago through tomorrow, 5-day span (3 days ago, today, tomorrow).

usually best practice in defining span of time "half-open" approach. in approach, beginning of span inclusive while ending exclusive. including tomorrow means want every moment not including first moment of day after tomorrow; add two days rather one in code below. search stackoverflow more discussion , examples of "half-open".

the method withtimeatstartofday adjusts first moment of day. moment not time 00:00:00 because of daylight saving time or other anomalies. in utc day start @ time, habit should avoid hard-coding particular time (let joda-time work you).

not sure wanted, seems want define "tomorrow" utc. here.

datetime = datetime.now( datetimezone.utc ); datetime momentaftertomorrow = now.plusdays( 2 ).withtimeatstartofday();  datetime threedaysago = now.minusdays( 3 ).withtimeatstartofday(); interval validinterval = new interval( threedaysago, momentaftertomorrow );  // ( inclusive, exclusive ) 

test if our target date-time occurs within interval.

datetime target = now.minusdays( 1 );  // arbitrary choice. boolean targetisvalid = validinterval.contains( target );  // true 

another test.

datetime target2 = now.minusdays( 111 );  // arbitrary choice. boolean target2isvalid = validinterval.contains( target2 );  // false 

by way, joda-time offers 3 classes defining various kinds of spans of time: period, interval, , duration.


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -