Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the zero in front of the day 1

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I have code that works,

int month = cal.get(Calendar.MONTH);
int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DAY_OF_MONTH);
String today = (year + "" + (month + 1) + "" + day + "-");
System.out.println("Current date : " + today);

But, where the day of the month is 1 to 9 this will display the digit without the leading zero.

I have tried to use length() to check the size but I get an error that this is not allowed.

Is there a easy fix?
 
I think this will do what you want.

Code:
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyyMMdd");

System.out.println("Current date : " + sdf.format(cal.getTime()));

-----------------------------------------
I cannot be bought. Find leasing information at
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top