• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to format a date.

 
Anthony Smith
Ranch Hand
Posts: 285
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a clean way to do this.

Take a look at SimpleDateFormat
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic