• 调试 两个日期相差的天数


    package test;
    
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
    
    public class Time {
        
        public static int differentDays(Date date1,Date date2){
            Calendar cal1 = Calendar.getInstance();
            Calendar cal2 = Calendar.getInstance();
            cal1.setTime(date1);
            cal2.setTime(date2);
            int day1 = cal1.get(Calendar.DAY_OF_YEAR);
            int day2 = cal2.get(Calendar.DAY_OF_YEAR);
            
            int year1 = cal1.get(Calendar.YEAR);
            int year2 = cal2.get(Calendar.YEAR);
            
            if(year1 != year2){   //不同年
                int timeDistance1 = 0 ;
                int timeDistance2 = 0 ;
                for(int i=year2;i<year1;i++){
                     if(i%4==0 && i%100!=0 || i%400==0){    //闰年            
                         timeDistance1 += 366;
                     }
                     else{    //不是闰年
                         timeDistance1 += 365;
                     }
                }
                    if(year2%4==0 && year2%100!=0 || year2%400==0){    //闰年            
                        timeDistance2 +=1;
                    }
                   
    
                return  (day1+timeDistance1-day2-timeDistance2)  ;
            }else{    //同年
                return day1-day2;
            }
        }
        
        public static void main(String[] args) throws ParseException {
             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
             Date date1 = simpleDateFormat.parse("2020-01-01");
             Date date2 = simpleDateFormat.parse("2019-12-25");
             System.out.println(differentDays(date1,date2));
        }
        
    
    }


      
      public static void calculateTimeDifferenceBySimpleDateFormat() throws ParseException {
            SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            /*天数差*/
            Date fromDate1 = simpleFormat.parse("2021-02-28 12:00");
            Date fromDate2 = simpleFormat.parse("2019-02-28 12:00");
            long from1 = fromDate1.getTime();
            long from2 = fromDate2.getTime();
            int days = (int) ((from1-from2) / (1000 * 60 * 60 * 24));
            System.out.println("两个时间之间的天数差为:" + days);
    
            /*小时差*/
            Date fromDate3 = simpleFormat.parse("2020-01-14 00:00");
            Date fromDate4 = simpleFormat.parse("2019-01-14 00:00");
            long from3 = fromDate3.getTime();
            long from4 = fromDate4.getTime();
            int hours = (int) ((from3-from4) / (1000 * 60 * 60));
            System.out.println("两个时间之间的小时差为:" + hours);
    
            /*分钟差*/
            Date fromDate5 = simpleFormat.parse("2020-01-01 12:00");
            Date fromDate6 = simpleFormat.parse("2020-01-01 11:00");
            long from5 = fromDate5.getTime();
            long from6 = fromDate6.getTime();
            int minutes = (int) ((from5 - from6) / (1000 * 60));
            System.out.println("两个时间之间的分钟差为:" + minutes);
    
    
    
    
    
  • 相关阅读:
    php7 & lua 压测对比
    .NET CORE——Console中使用依赖注入
    EntityFramework Core 自动绑定模型映射
    月末总结与推书
    Dapper连接与事务的简单封装
    EntityFramework Core 学习扫盲
    从输入url到页面返回到底发生了什么
    [译]C#和.NET中的字符串
    利用C#迭代器的一个杨辉三角示例
    用 dotTrace 进行性能分析时,各种不同性能分析选项的含义和用途
  • 原文地址:https://www.cnblogs.com/lifan12589/p/12220998.html
Copyright © 2020-2023  润新知