计算自2000年1月1日起,N天后的日期
来源:互联网 发布:可控核聚变 知乎 编辑:程序博客网 时间:2024/05/02 23:12
/** * 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;}}