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 Beginning Java and the fly likes How to format a date. 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 » Beginning Java
Bookmark "How to format a date." Watch "How to format a date." New topic
Author

How to format a date.

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I have a Date object.

I want to print the date like this:

20050808

I use:

Calendar c = Calendar.getInstance();
c.setTime(shipDate1); //shipDate is my Date Objecy

String date = String.valueOf(c.get(Calendar.YEAR)) + String.valueOf(c.get(Calendar.DAY_OF_MONTH)) +
String.valueOf((Calendar.DAY_OF_MONTH));

The 0's from the day and month keep getting trimmed. There has to be a clean way to do this.
Richard Anderson
Ranch Hand

Joined: May 20, 2005
Posts: 61
There is a clean way to do this.

Take a look at SimpleDateFormat


-Rich, SCJP 1.4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to format a date.
 
Similar Threads
Time refreshing
Calendar returns wrong date?
How to read a file
Date Function - HOLIDAY
Calender class and its components