I have two log files which I feed with data, obviously.
Every entry in each log file should include date and time when an event occurred.
For instance:
06/05/2015 17:10:00 new connection established
How do I retrieve the date?
SimpleDateFormat sFormat = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
Calendar cal = Calendar.getInstance();
sFormat.format(cal.getTime());
Now currently two different classes (threads) need the current date and time.
Due to that I don't want to write in every class a method which returns sFormat.format(cal.getTime()) because it is unprofessional, I thought about a DateFactory.class :
public class DateFactory {
public static String datePattern = "MM/dd/yyyy HH:mm:ss";
public static String retrieveDate() {
Calendar cal = Calendar.getInstance();
SimpleDateFormat simpleFormat = new SimpleDateFormat(datePattern);
return simpleFormat.format(cal.getTime());
}
}
And now I am able to retrieve the current Date within my whole project no matter how much threads will join my project.
Do look like this:
out.write(DateFactory.retrieveDate() + " " + newConnection.getInetAddress() + " " + " new connection established" + TheServerSocket.newLine);
Where out is a instance of FileWriter.class if this is relevant for someone.
The Question:
Do I totally miss something and my idea is the worst on planet? Are there any disadvantages in using my DateFactory-solution compared to a totally different design pattern (if there is any) which I may be completely unaware of? Or are there general any disadvantages in using my DateFactory-solution?
Aucun commentaire:
Enregistrer un commentaire