This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes How to use DateFormat Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to use DateFormat" Watch "How to use DateFormat" New topic
Author

How to use DateFormat

Kay Crocker
Greenhorn

Joined: Nov 01, 2001
Posts: 17
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

Joined: Aug 26, 2001
Posts: 7610
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Kay Crocker
Greenhorn

Joined: Nov 01, 2001
Posts: 17
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

Joined: May 17, 2001
Posts: 2823

You just need to get your string to a date. Play around with the SimpleDateFormat options and see what they do.
Kay Crocker
Greenhorn

Joined: Nov 01, 2001
Posts: 17
Thanks, I will try and if I get it working I will post my code.
Kay Crocker
Greenhorn

Joined: Nov 01, 2001
Posts: 17
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

Joined: May 17, 2001
Posts: 2823
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).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to use DateFormat
 
Similar Threads
How to check if date/time was entered?
How to Manipulate Dates?
Timezone Conversion
date or calendar object creation
IllegalArgumentException