1. 程式人生 > >獲取某一天的前幾天或者後幾天及得到一個月的最後一天

獲取某一天的前幾天或者後幾天及得到一個月的最後一天

package test2;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Test8 {
public static void main(String[] args) throws ParseException {
isLastDay();
}

//獲取指定日期的前多少天或後多少天
public static void getBeforeDay() throws ParseException{
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Date date = calendar.getTime();
date = new SimpleDateFormat("yyyyMMdd").parse("20130301"); 
calendar.setTime(date);
calendar.add(Calendar.DAY_OF_YEAR, -1);
System.out.println(sdf.format(calendar.getTime()));
}

//得到某月的第一天和最後一天
public static void isLastDay(){
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, 2013);
c.set(Calendar.MONTH, 0);
System.out.println(c.getActualMaximum(Calendar.DAY_OF_MONTH));
System.out.println(c.getActualMinimum(Calendar.DAY_OF_MONTH));
}
}