Return | Method | Summary |
---|---|---|
static Calendar | getInstance() | Gets a calendar using the default time zone and locale. |
static Calendar | getInstance(Locale aLocale) | Gets a calendar using the default time zone and specified locale. |
static Calendar | getInstance(TimeZone zone) | Gets a calendar using the specified time zone and default locale. |
static Calendar | getInstance(TimeZone zone, Locale aLocale) | Gets a calendar with the specified time zone and locale. |
The following code creates Calendar instance and then adds or substracts weeks to current date
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Now: " + (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
// add week to current date
now.add(Calendar.WEEK_OF_MONTH, 1);
System.out.println("date after one week : " + (now.get(Calendar.MONTH) + 1)
+ "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
// add minus value
now = Calendar.getInstance();
now.add(Calendar.WEEK_OF_MONTH, -1);
System.out.println("date before 1 week : "
+ (now.get(Calendar.MONTH) + 1) + "-" + now.get(Calendar.DATE) + "-"
+ now.get(Calendar.YEAR));
}
}
The output:
Now: 10-30-2010
date after one week : 11-6-2010
date before 1 week : 10-23-2010
Create a Calendar object and add or substract years to current date
import java.util.Calendar;
public class Main {
public static void main(String[] args) {
Calendar now = Calendar.getInstance();
System.out.println("Current date : " + (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
// add year to current date
now.add(Calendar.YEAR, 1);
System.out.println("1 year after: " + (now.get(Calendar.MONTH) + 1)
+ "-" + now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
// add minus value
now = Calendar.getInstance();
now.add(Calendar.YEAR, -1);
System.out.println("1 year before: " + (now.get(Calendar.MONTH) + 1) + "-"
+ now.get(Calendar.DATE) + "-" + now.get(Calendar.YEAR));
}
}
The output:
Current date : 10-30-2010
1 year after: 10-30-2011
1 year before: 10-30-2009
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |