File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Date format question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date format question" Watch "Date format question" New topic
Author

Date format question

Brian Tomlin
Ranch Hand

Joined: Dec 06, 2000
Posts: 31
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You need a DateFormat. Try:
;


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Brian Tomlin
Ranch Hand

Joined: Dec 06, 2000
Posts: 31
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
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
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.
Brian Tomlin
Ranch Hand

Joined: Dec 06, 2000
Posts: 31
I see your point. That makes sence to me now. Thanks for your explanation.
 
 
subject: Date format question
 
Similar Threads
dates in sql to dates in java
Date class
Converting from GMT to Local Timezone using two seperate fields (date & time).
DailyRollingFileAppender
How do you display what's read from a file in a JTextArea properly?