Add / Subtract Date using Calendar

Many methods of Date() class is deprecated and the replacement is Calendar class. The following example shows how to display last month’s date.

java.text.SimpleDateFormat dateFormat = new java.text.SimpleDateFormat(“dd-MMMM-yyyy);
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.MONTH,-1);
System.out.println(“Last Month Date : ” + dateFormat.format(c1.getTime()));

Advertisements

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: