Hello. I'm trying to convert Calendar to dd/MM/yyyy format.

My problem was it couldn't convert to exactly what i wanted. I'm Turkish so when we write a date, we write the day first then month then year...

My code is here

public static String toFormattedDateString(Calendar input){
		String year = null;
		String month = null;
		String day = null;
			int monthInt = input.get(Calendar.MONTH);
			month = "0";
			month += (new Integer(monthInt)).toString();
			month = (new Integer(input.get(Calendar.MONTH)+1)).toString();
			day = "0"+ (new Integer(input.get(Calendar.DAY_OF_MONTH))).toString();
			day = (new Integer(input.get(Calendar.DAY_OF_MONTH))).toString();
		year = new Integer(input.get(Calendar.YEAR)).toString();
		return day+"/"+month+"/"+year;

And it can't convert to what i want exactly. Here are some results.

01.02.2009---> It couldn't convert it correctly. Added dots and 01 is the month and 02 is the day.
17/01/2009 ---> This is the correct result. dd/MM/yyyy format
01.02.2009----> Wrong again.

Have a look at SimpleDateFormat, it allows you to format dates according to your locale and your own format specification.
It works with Date objects, rather than Calendars, so use the getTime() Calendar method to get the correponding Date.

public static String toFormattedDateString(Calendar input){
SimpleDateFormat df = new SimpleDateFormat();
return df.format(input.getTime());

The same results again. I don't know what's wrong with this.

What is the exact input data that you are using? and what is the exact output?
Maybe the roblem is in how the Calendar object is beung set up before this method is called?

Edited 7 Years Ago by JamesCherrill: n/a

I have an HTML table and i get this table as a result.

My result is correct.

It's like this.


Then in the servlet i do this;

response.setHeader("Content-Disposition", "attachment; filename = Report.XLS");
response.getWriter().write(result);//--->this is my HTML table above

The result in excel is this

02.03.2009 02.03.2009

The table data which are like 22/10/2009 or 13/01/2009 are correct in excel. I can see them in the correct format.

It writes correct in the console. But the problem is while converting the table to the excel file.

Edited 7 Years Ago by Xessa: n/a

I used . instead of / and this fixed the problem...

This article has been dead for over six months. Start a new discussion instead.