import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; public class DateTime { public static void main(String[] args) { Calendar calendar=Calendar.getInstance();//获取日历 int year=calendar.get(Calendar.YEAR);//获取年 int month = (calendar.get((Calendar.MONTH))+1);//获取月 int day = calendar.get(Calendar.DAY_OF_MONTH);//获取日 int addDays= 100;//定义要增加的天数-100天 int totalDays=addDays+day;//将增加的天数+现在的_日(好换算成几月几日) LocalDateTime nowDateTime=LocalDateTime.now();//获取当前日期,时间 DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//指定当前时间的格式 DateTimeFormatter dateTimeFormatter2=DateTimeFormatter.ofPattern("HH:mm:ss");//指定100天后的时间格式 System.out.println("当前时间:"+nowDateTime.format(dateTimeFormatter));//当前日期时间 System.out.println(addDays+"天后的时间:"+daysToDate(year,month,totalDays)+" "+nowDateTime.format(dateTimeFormatter2));//100天后的日期时间 } public static String daysToDate(int year,int month,int totalDays) { /*平年二月28天*/ final int DAYS_28 = 28; /*闰年二月29天*/ final int DAYS_29 = 29; /*4、6、9、11月份均为30天*/ final int DAYS_30 = 30; /*1、3、5、7、8、10、12月份为31天*/ final int DAYS_31 = 31; //每减去一个月的天数当前月(month)加1 switch (month) { //用总天数totalDays减去当前月份以及之后月份的天数,直至totalDays小于28为止 case 12:{ if(totalDays>=28){ totalDays -= DAYS_30; month++; } else {break;} } case 11:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else {break;} } case 10:{ if(totalDays>=28){ totalDays -= DAYS_30; month++; } else {break;} } case 9:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else {break;} } case 8:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else { break; } } case 7:{ if(totalDays>=28){ totalDays -= DAYS_30; month++; } else {break;} } case 6:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else {break;} } case 5:{ if(totalDays>=28){ totalDays -= DAYS_30; month++; } else {break;} } case 4:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else {break;} } case 3:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else {break;} } case 2:{ if(totalDays>=28){ // 判断闰/平年 if (((year / 4 == 0) && (year / 100 != 0)) || (year / 400 == 0)) { totalDays -= DAYS_29; } else { totalDays -= DAYS_28; } month++; } else {break;} } case 1:{ if(totalDays>=28){ totalDays -= DAYS_31; month++; } else {break;} } } //返回100天之后的日期 return year+"-"+month+"-"+totalDays+""; } }
100天后的日期时间
最新推荐文章于 2024-07-29 22:15:27 发布