【问题标题】:How to count days between to dates [duplicate]如何计算日期之间的天数[重复]
【发布时间】:2019-03-06 09:51:57
【问题描述】:

所以我想计算两个日期选择器之间的天数。 所以我尝试制作两个数组,但没有奏效 有什么想法吗?

这是我的日期选择器

【问题讨论】:

标签: java android date days date-difference


【解决方案1】:

将您的日历字符串日、月、年转换为日期类。

更多讨论在这里:Java string to date conversion

例如

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date d1, d2;

try {
    d1 = dateFormat.parse(year + "-" + month + "-" + day); //as per your example
    d2 = //do the same thing as above
    long days = getDifferenceDays(d1, d2);
} 
catch (ParseException e) {
    e.printStackTrace();
}


public static long getDifferenceDays(Date d1, Date d2) {
long diff = d2.getTime() - d1.getTime();
return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}

【讨论】:

    【解决方案2】:

    创建一个方法getDates()

    private static ArrayList<Date> getDates(String dateString1, String dateString2)
    {
        ArrayList<Date> arrayofdates = new ArrayList<Date>();
        DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
    
        Date date1 = null;
        Date date2 = null;
    
        try {
            date1 = df1 .parse(dateString1);
            date2 = df1 .parse(dateString2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    
        Calendar calender1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        calender1.setTime(date1);
        calender2.setTime(date2);
    
        while(!calender1.after(calender2))
        {
            arrayofdates.add(calender1.getTime());
            calender1.add(Calendar.DATE, 1);
        }
        return arrayofdates;
    }
    

    然后在此方法中传递参数以获取日期数组 当您使用 DatePicker 然后

    DateFormat df1 = new SimpleDateFormat("dd-MM-yyyy");
        ArrayList<Date> mBaseDateList = getDates(df1.format(cal1.time), df1.format(cal2.time))
    

    【讨论】:

    • 但是如何为我的日期选择器获取字符串和日历...正如您在我上传的照片中看到的那样。哦,对了
    • 为 calendar 创建两个全局变量。 calA&calB。当您从 datepicker 中选择日期时,只需执行此操作 - calA.setTime(cal1.getTime());。这将改变 calA 的时间。对 calB 执行相同的操作。当您将参数传递给getDates() 时。 df1.format(cal1.time) 会自动转换成字符串。
    【解决方案3】:
    Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Date d1, d2;
         Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
        for (int i = 0; i < n; i++) {
            try {
                d2 = sdf.parse(in.next());
                d1 = sdf.parse(in.next());
                long differene = (d2.getTime() - d1.getTime()) / (1000 * 60 * 60 * 24);
                System.out.println(Math.abs(differene));
            } catch (Exception e) {
            }
        }
    

    【讨论】:

      【解决方案4】:
      public static int getDaysBetweenDates(Date fromDate, Date toDate) {
          return Math.abs((int) ((toDate.getTime() - fromDate.getTime()) / (1000 * 60 * 60 * 24)));
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-07
        • 2013-09-11
        • 2017-08-11
        • 2014-07-08
        • 2013-11-29
        • 2011-10-29
        • 2019-02-10
        • 1970-01-01
        相关资源
        最近更新 更多