Calendar日历类 :
package daysreplace;
import com.sun.jdi.IntegerValue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DATE);
//获取月份 从0开始 需要+1
System.out.println(year + "年" + (month + 1) + "月" + day + "日");
}
}
package daysreplace;
import com.sun.jdi.IntegerValue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ParseException {
Calendar c = Calendar.getInstance();
//加10年
c.add(Calendar.YEAR,10);
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DATE);
//获取月份 从0开始 需要+1
System.out.println(year + "年" + (month + 1) + "月" + day + "日");
}
}
小案例:二月有多少天?
package daysreplace;
import com.sun.jdi.IntegerValue;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws ParseException {
System.out.println("请输入年份");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
Calendar c = Calendar.getInstance();
c.set(year,2,1);
c.add(Calendar.DATE,-1);
System.out.println(year+"年的二月有"+c.get(Calendar.DATE)+"天");
}
}