Java date minus seven days.

Java date minus seven days.


Posted in : Core Java Posted on : November 29, 2010 at 3:50 PM Comments : [ 0 ]

Java date minus seven days.

Java date minus seven days.

In this example, you will see how to subtract seven days from current date in java. The add(int field, int amount) is used for addition or subtraction. The addition or subtraction dependence of sign of amount. If sign of amount is negative than it will do subtraction, otherwise addition.

Calendar.DATE -- It  returns current date.
Calendar.DAY_OF_WEEK -- It  returns number of days in a week.

Code:

SubtractDays.java
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class SubtractDays {
public static void main(String[] args) {
	Calendar currCal = Calendar.getInstance();
	SimpleDateFormat obDateFormat = new SimpleDateFormat("EEE yyyy-MMMM-dd");
	System.out.println("Current date : "
		+ obDateFormat.format(currCal.getTime()));
	currCal.add(Calendar.DATE, -Calendar.DAY_OF_WEEK);
	System.out.println("Current date : "
		+ obDateFormat.format(currCal.getTime()));
     }
}

Output

Current date : Mon 2010-November-29 Current date : Mon 2010-November-22

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics