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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

formatting a date 1

Status
Not open for further replies.

jcarrott

Programmer
May 28, 2009
130
US
I am trying to format a date as part of a string field, I am sure that I am doing this the wrong way and I was hoping that there was an easy way to do this.

Here is the code I have,

Calendar cal = new GregorianCalendar();
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 + "-");
// this will not work for Jan thru June
int length = today.length();
if (length < 9){
today = (year + "" + (month + 1) + "0" + day + "-");
}
System.out.println("Current date : " + today);

Is there an easy way to do this?
 
First of all, you should post this in the Java forum, this one is intended only for J2EE questions.

Anyway, would a SimpleDateFormate do the trick for you?

Cheers,
Dian
 
Sorry about using the wrong forum, but your answer was exactly what I needed - thank you very much
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top