Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Format

 
Eric Martin
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I need to create a Date in a certain format and leave it as a Date object. How do I do this? I have looked at SimpleDateFormat, but all of its returns seem to be of type String.

TIA
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Date object represents the actual date -- i.e., the time in UTC. It's only in rendering the Date as a String that the concept of a format is introduced.

Beginners often make the same mistake with floating-point numbers. A float or double has some arbitrary number of decimal places -- i.e., 3.99999999654. It's only in rendering it as text that we choose to limit ourselves to two decimal places and get 4.00 .
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Eric Martin:
Hello, I need to create a Date in a certain format and leave it as a Date object. How do I do this? I have looked at SimpleDateFormat, but all of its returns seem to be of type String.

TIA


Do you have the String that you need to use to create a Date object? Or similarly, do you have a Date object that you need to output as a String? As EFH mentions above, these are the only situations where the format has any meaning. (Internally, the Date class has its own representation of the date and time, which probably doesn't include a String.) To convert a String into a Date, use DateFormat.parse(). To convert a Date into a String, use DateFormat.format(). Notice that SimpleDateFormat extends DateFormat. This means that the documentation for SimpleDateFormat only contains information for the methods it overrides or adds. You should also look at the docs for the DateFormat class in order to see any methods that SimpleDateFormat inherits.

Layne
[ July 30, 2005: Message edited by: Layne Lund ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic