Day boundary comparisons

G

GaryM

Is there an easy way to perform day boundary comparisons?

I have a large directory of archived files and I want to zip them up
when they are older than 7 days. I can make it work using
System.currentTimeMillis() - (2 days in millis) versus the
file.lastModified() date. However, to get it at the day level I am
creating a GregorianCalendar and setting the time to 1ms after
midnight.

Was wondering if there was a simpler way to do this such that < and
<= would work at the day level? Do I need to make Date objects and
simply compare d/m/y elements?

Here is the code I have been using to get the base date for
comparison:

// Set the time to 00:00:00 + 1ms and -7 days
Calendar c = new GregorianCalendar();
c.setTimeInMillis(System.currentTimeMillis());
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 1);
c.add(Calendar.DAY_OF_MONTH, -7);
long compareDate = c.getTimeInMillis();

Thanks in advance,

Gary
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,738
Latest member
JinaMacvit

Latest Threads

Top