月份添加数字获取时间

public static void main(String[] args) {
Date date= new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String d1 = format.format(date);
String d2 = "24";  //月份
String dd[] = d1.split("-"); //分割为,年,月,日
//那么如果月份要相加,就有可能超过一年,所以:
//要加的年份,月份为12个月为一年
int month1= Integer.parseInt(dd[1]);
int month2= Integer.parseInt(d2);
String year = (month1+month2)/12+"";//我这里就简写下了,见谅

int year1=Integer.parseInt(dd[0]);
int year2= Integer.parseInt(year);
//那么多出来的月份,就是最后日期的月份
int yue = (month1+month2)%12;  //我想,这样应该不难理解吧
String mm = "";
//但是呢,如果是个位数,那么十位上就要加上“0”
if(yue<10){
mm = "0"+yue;
}else{
mm = yue+"";
}
dd[0]=year1+year2+"";
dd[1]=mm;
d1=dd[0]+dd[1]+dd[2];

System.out.println(d1);

}

猜你喜欢

转载自blog.csdn.net/Nietzsche1990/article/details/81014090