1. 程式人生 > >計算自2000年1月1日起,N天后的日期

計算自2000年1月1日起,N天后的日期

/**
 * calculate the date from 2000-1-1 after a number of days
 */
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class TwoThousandDate {
	public void getCurrentDate(int dump,DateReturn date){

		Calendar ca=Calendar.getInstance();
		ca.set(Calendar.YEAR,2000);
		ca.set(Calendar.MONTH, Calendar.JANUARY);
		ca.set(Calendar.DAY_OF_MONTH,1);
		
		ca.add(Calendar.DAY_OF_MONTH, dump);

		// SimpleDateFormat define the new date format。
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");  //構造任意格式
		
		String time = sm.format(ca.getTime());
		String[] timeArray= time.split("-");
		date.setYear(Integer.parseInt(timeArray[0]));
		date.setMonth(Integer.parseInt(timeArray[1]));
		date.setDay(Integer.parseInt(timeArray[2]));
		
	}
	public static void main(String[] args){
		TwoThousandDate twoThousandDate = new TwoThousandDate();
		DateReturn date = new DateReturn();
		twoThousandDate.getCurrentDate(10,date);
		System.out.println(date.getCurrentDate());
	}
	
}
class DateReturn{
	private int year;
	private int month;
	private int day;
	
	public void setYear(int year){
		this.year = year;
	}
	public void setMonth(int month){
		this.month = month;
	}
	public void setDay(int day){
		this.day = day;
	}
	public String getCurrentDate(){
		return this.year + "-" + this.month + "-" + this.day;
	}
	
}