1

I have this code

String date = "18/06/2014";

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");

Calendar c = Calendar.getInstance();

c.setTime(sdf.parse(date));

c.add(Calendar.DATE, +2); 

I'm trying to get formatted date object from calendar object but without any success. Do you have any suggestion?

eng_mazzy
  • 1,049
  • 4
  • 23
  • 39

2 Answers2

2

Date is an object, it doesn't have an inherent textual representation. The SimpleDateFormat class can be used to provide the textual representation you're looking for,

String date = "30/06/2014";
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(date));
c.add(Calendar.DATE, +2);
System.out.println(sdf.format(c.getTime()));

Will output

02/07/2014
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

Are you looking for this?

Date date = sdf.parse(date);

user77318
  • 80
  • 5