100天后的日期时间

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+"";
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值