• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Date format question

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need a DateFormat. Try:
;
 
Brian Tomlin
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see your point. That makes sence to me now. Thanks for your explanation.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic