eng_mazzy
eng_mazzy

Reputation: 1049

Get formatted date object from Calendar object

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?

Upvotes: 1

Views: 788

Answers (2)

user77318
user77318

Reputation: 80

Are you looking for this?

Date date = sdf.parse(date);

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

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

Upvotes: 2

Related Questions