0

I need that the returned date string would have the format:

"DD/MM/YYYY" 

i.e. 2 digits for day and month and 4 digits for year.

Example:

int day = 11;
int month = 2;
int year = 1997;

be returned as

"11/02/1997"

Any suggestions?

Cœur
  • 37,241
  • 25
  • 195
  • 267

2 Answers2

0

First of all your date format is wrong.change

"DD/MM/YYYY"  

to

"dd/MM/yyyy" 

Meaning of d and D is differ same as y and Y

Read about that.

Answer to the next part.

You can use Calender for this

 int day = 11;
 int month = 2; // 0th January. 1st February... so on
 int year = 1997;

 Calendar calendar=Calendar.getInstance();
 calendar.set(Calendar.YEAR,year);
 calendar.set(Calendar.MONTH,month-1);
 calendar.set(Calendar.DATE,day);

 DateFormat df=new SimpleDateFormat("dd/MM/yyyy");
 System.out.println(df.format(calendar.getTime()));

Out put:

 11/02/1997

You may need to read about Calender too.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0
Calendar  calendar = Calendar.getInstance();
calendar.set(1997, 11, 2);
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
System.out.println(dateFormat.format(calendar.getTime()));
dReAmEr
  • 6,986
  • 7
  • 36
  • 63