This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to use DateFormat

 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a string = "2001-11-27".
Can't I use the Date and DateFormat Classes to change this to a string that looks like "Novemeber 27, 2001" ?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java API gives good info about that
http://java.sun.com/j2se/1.3/docs/api/java/text/DateFormat.html
HIH
------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have read that and Date and SimpleDateFormat Class info in the Api. My problem is that I don't now how to use it in conjunction with one another to get the result I want.
A code example would be useful.
Please help.
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You just need to get your string to a date. Play around with the SimpleDateFormat options and see what they do.
 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, I will try and if I get it working I will post my code.
 
Kay Crocker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done it!!
code:
Date sampleDate = null;
/* convert string to date */
SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd hh:mm:ss" );
String sampleDateString = "2001-11-27 09:53:18";
try {
sampleDate = sdf.parse(sampleDateString);
} catch (ParseException pe) {
System.out.println(pe.toString());
}
/* converts date to text */
SimpleDateFormat formatter = new SimpleDateFormat ("MMMMMMMMM dd, yyyy 'at' h:mm a zzz");
String dateString = formatter.format(sampleDate);
System.out.println(dateString);
This prints out:
November 27, 2001 at 9:53 AM EST
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See that wasn't so bad was it.
ps to use the code tags it takes more than code: use the [ and ] around code and /code.
[This message has been edited by Paul Stevens (edited November 28, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic