Hi, I am trying to create a log file using the RandomAccessFile class and use the current date in part of the file name. I am trying this using the Date class to get the current date but the file is not getting created, probably because I am not using the correct format. This is what my code looks like.
I think the problem is the way that the date is formatted with the Date class. It looks like this. Mon Feb 26 09:42:21 CST 2001 I would like to use a format that would create a file that looks something like "Server02262001.log" I have looked for a method that would convert the date format to this, but I haven't found one. Maybe I'm not looking in the right place. Surely there is something already out there to do this. Any suggestion? Thanks, Brian
Thanks for quick reply Frank. Your solution works. This is what I wrote.
I have one question about this line of code.
Why was the DateFormat class introduced here? I was able to compile the code using this line too.
Is it necessary to intruduce another class, or am I missing something? Thanks again, Brian
Joined: Jan 07, 1999
This is subtle piece of good Java style. For really maintainable code, you should always create an object using the most specific class you can find, but refer to it as the least specific. This allows you to change the SimpleDateFormat constructor to any other DateFormat class (say a custom DateFormat which is not based on SimpleDateFormat)with no changes to any other code. The type of each variable in your system should be decided solely on the operations you require of it. In this case we only require operations provided by the base DateFormat class, so the variable "doesn't need to know" which child class was used to create it. In this case the code is very short, so the benefits are small (if you changed to a new type of DateFormat in your version, you would need to change the name SimpleDateFormat twice, rather than just once for mine), but as software gets larger this approach becomes very important in ensuring maximum flexibility and reuse, with minimum maintenance overhead.
Joined: Dec 06, 2000
I see your point. That makes sence to me now. Thanks for your explanation.