Handling a change in system date

J

james

Hi all,

I'm relatively new to java so I apologise if this is a simple
question. I am trying to detect when the system date changes so that I
can start a new log file for each day. I have setup the code to create
a new log file etc. however I can't seem to find any information on
how to detect changes in the system date. I am only interested in when
the day changes (not the time etc.) and I have looked into change
listeners however I'm not sure if this is the right approach or not?

Can anyone please point me in the right direction, or even better to a
good tutorial on how to do this!

Thanks in advance,
James
 
E

Eric Sosman

james said:
Hi all,

I'm relatively new to java so I apologise if this is a simple
question. I am trying to detect when the system date changes so that I
can start a new log file for each day. I have setup the code to create
a new log file etc. however I can't seem to find any information on
how to detect changes in the system date. I am only interested in when
the day changes (not the time etc.) and I have looked into change
listeners however I'm not sure if this is the right approach or not?

One way is to insert the date check in the logging code:
Each time you log a new message, first check for a date change
and possibly start a new log. This method is simple, but may
not be appropriate if the change-the-log code writes log messages
of its own, or if the logging code is mostly a "black box" whose
innards are hard to get at. (If you're using the java.util.logging
package, consider doing this by extending FileHandler.)

Another way is to use a separate thread to manage the change.
Each time you open a log file (at program start or when switching
log files), calculate the amount of time remaining until the next
midnight and set up a Timer to run the log-change activity then.
This separates the log-change from the rest of the logging, but
requires that you be careful about synchronizing things. Also,
Timer is not guaranteed to be 100% accurate, and you may switch
logs a few seconds early or late.

A variation on the second method: Use a repeating Timer to
wake up and check the date every minute or so, either changing
the log file or sticking with the old one.
 
C

Christian

james said:
Hi all,

I'm relatively new to java so I apologise if this is a simple
question. I am trying to detect when the system date changes so that I
can start a new log file for each day. I have setup the code to create
a new log file etc. however I can't seem to find any information on
how to detect changes in the system date. I am only interested in when
the day changes (not the time etc.) and I have looked into change
listeners however I'm not sure if this is the right approach or not?

Can anyone please point me in the right direction, or even better to a
good tutorial on how to do this!

Thanks in advance,
James

I would have a look at:
http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html

christian
 
R

Roedy Green

I'm relatively new to java so I apologise if this is a simple
question. I am trying to detect when the system date changes so that I
can start a new log file for each day. I have setup the code to create
a new log file etc. however I can't seem to find any information on
how to detect changes in the system date. I am only interested in when
the day changes (not the time etc.) and I have looked into change
listeners however I'm not sure if this is the right approach or not?

The easiest way is to use BigDate.localToday or BigDate.UTCToday.
see http://mindprod.com/products1.html#COMMON11

Otherwise you will have to fool around with GregorianCalendar to get
the day of month and see if it has changed.
see http://mindprod.com/jgloss/calendar.html
 
J

james

Thanks for all your help, I'm looking into all the options at the
moment however I think the log4j approach looks the most promising.

James
 
D

Dr J R Stockton

In comp.lang.java.programmer message <1414a7dc-6693-481e-8ce4-d57a95386b
(e-mail address removed)>, Sat, 26 Jan 2008 05:15:30, james
I am trying to detect when the system date changes so that I
can start a new log file for each day.

Another approach might be to write only to NewLogFile, and have a task
scheduled for, say, 01:01:01 each day which copied NewLogFile; to
YesterdayFile until the log entry date changed, and then removed the old
entries from NewLogFile. It would probably need exclusive access to
NewLogFile for the second half of that.
 
G

GArlington

Hi all,

I'm relatively new to java so I apologise if this is a simple
question. I am trying to detect when the system date changes so that I
can start a new log file for each day. I have setup the code to create
a new log file etc. however I can't seem to find any information on
how to detect changes in the system date. I am only interested in when
the day changes (not the time etc.) and I have looked into change
listeners however I'm not sure if this is the right approach or not?

Can anyone please point me in the right direction, or even better to a
good tutorial on how to do this!

Thanks in advance,
James

Why do not you use date string as part of the file name? This way the
file system itself is going to help you and create new file when the
date has changed...
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top