日历开始日期在同一天差异的结束日期之前 [英] calendar start date before end date on same day difference

查看:38
本文介绍了日历开始日期在同一天差异的结束日期之前的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个日历日期,其中的天数、小时数和分钟数之间存在差异.

I have two calendar dates where i am getting the difference between in days, hours, and minutes.

如果结束日期大于开始日期,这将非常有效.不起作用的是,如果开始日期与结束日期在一周中的同一天,但比结束日期早.例如:结束日期是星期六下午 2:20,开始日期是星期六晚上 7:20.它以 0 天和 5 小时计算.但是,应该更像是 7 天.

This works perfectly if the end date is greater than the start date. What doesnt work is if the start date is the same day of week as the end date, but an earlier time than the end date. For example: end date 2:20 pm Saturday, and start date is 7:20 pm on saturday. It calculates it at like 0days, and 5 hours. But, it should be more like 7 days.

这是代码

    long t1 = curCal.getTimeInMillis();
    long t2 = setCal.getTimeInMillis();

    if(t2 < t1){
         days = t1-t2;
    }else{
         days = t2-t1;
    }

    long toDays = TimeUnit.MILLISECONDS.toDays(days);
    long toHours = TimeUnit.MILLISECONDS.toHours(days) % 24;
    long toMinutes = TimeUnit.MILLISECONDS.toMinutes(days) % 60;
    String toastMessage =   String.format(" %d Days %d Hours %d Minutes", toDays, toHours, toMinutes);
    Toast.makeText(context, "ALARM in"  + " " + toastMessage , Toast.LENGTH_LONG).show();

结束日期与开始日期为同一天,但结束日期早于开始日期的情况如何处理?

How can i handle the case where the end date is the same day as the start date, but the end date is a time before the start date?

谢谢

编辑

我想我解决了我的问题.我正在为其他有同样问题的人添加它.如果结束日期 = 开始日期(同一天)将 7 添加到结束日期的日历对象.伪代码

I think i solved my problem. I am adding it for anyone else having the same issue. if end date = startdate(same day) add 7 to the calendar object for enddate. psuedocode

if (enddate == startdate)) {
   enddate.add(Calendar.DAY_OF_YEAR, 7);
}

推荐答案

    ZoneId zone = ZoneId.of("Europe/Busingen");

    DayOfWeek alarmDay = DayOfWeek.SUNDAY;
    LocalTime alarmTime = LocalTime.of(14, 20);
    ZonedDateTime now = ZonedDateTime.now(zone);

    ZonedDateTime alarmDateTime = now.with(alarmDay).with(alarmTime);
    if (alarmDateTime.isBefore(now)) {
        alarmDateTime = alarmDateTime.plusWeeks(1);
    }
    Duration difference = Duration.between(now, alarmDateTime);
    String toastMessage = String.format(" %d Days %d Hours %d Minutes",
            difference.toDaysPart(), difference.toHoursPart(), difference.toMinutesPart());
    System.out.println(toastMessage);

刚刚跑步(星期日 22:03:17 在 Büsingen)我得到了:

Running just now (Sunday 22:03:17 in Büsingen) I got:

6 天 16 小时 16 分钟

6 Days 16 Hours 16 Minutes

我相信我提供的答案不仅是现代的,而且是更可靠的.

I believe that I am contributing the answer that is not only the modern one but also the more robust one.

  • 现代:Calendar 类早已过时,按照今天的标准设计很差.相反,我使用并推荐 java.time,现代 Java 日期和时间 API.
  • 强大:据我所知,您的代码不仅在今天和闹钟日期是一周中的同一天时存在问题,而且如果闹钟落在星期.我会考虑到这一点.
  • 此外准确:如果您在夏令时 (DST) 之间进行交叉转换,则在计算中使用毫秒值时可能会得到错误的小时数.在这里使用两个 ZonedDateTime 对象可以最大限度地减少意外.它确实需要您填写您想要的时区,因为夏季时间转换是特定于时区的.
  • 此外,更精确地建模:使用日历、日期和时间作为每周重复的闹钟似乎有点有趣.您需要的是星期几和一天中的某个时间,所以我使用它.java.time 提供所需的类,DayOfWeek 枚举和 LocalTime 类.
  • Modern: The Calendar class is long outdated and by today’s standards poorly designed. Instead I use and recommend java.time, the modern Java date and time API.
  • Robust: As far as I can tell your code doesn’t only have an issue when today and alarm date are the same day of week, but also if the alarm falls on an earlier day of week. I take that into account.
  • Furthermore accurate: In cases where you cross transitions to and from summer time (DST), you may get the wrong number of hours when you use the millisecond values in your calculation. Using two ZonedDateTime objects minimizes surprises here. It does require you to fill in your desired time zone where I put Europe/Busingen since summer time transitions are time zone specific.
  • Furthermore more precisely modelled: Using a Calendar, a date and time, for a weekly recurring alarm seems a bit funny. What you need is a day-of-week and a time of day, so I use that. java.time offers the classes needed, the DayOfWeek enum and the LocalTime class.

事实上,我是如此现代,以至于我使用了 Java 9 中引入的 Duration 类的 toXxxPart 方法.用于格式化 Duration 如果您还没有使用 Java 9,则需要先从持续时间中减去天数以获得小时数:使用 minusDays 方法.然后用 minusHours 类似地得到分钟.

I am in fact so modern that I am using the toXxxPart methods of the Duration class that were introduced in Java 9. For formatting the Duration if you are not yet using Java 9 you will need to subtract first the days from the duration to get the hours: use the minusDays method. Then do similarly with minusHours to get the minutes.

    long toDays = difference.toDays();
    difference = difference.minusDays(toDays);
    long toHours = difference.toHours();
    difference = difference.minusHours(toHours);
    long toMinutes = difference.toMinutes();

问题:我可以在 Android 上使用 java.time 吗?

是的,java.time 在新旧 Android 设备上运行良好.它只需要至少 Java 6.

Question: Can I use java.time on Android?

Yes, java.time works nicely on older and newer Android devices. It just requires at least Java 6.

  • 在 Java 8 及更高版本以及更新的 Android 设备(据我所知,从 API 级别 26 开始)中,内置了现代 API.
  • 在 Java 6 和 7 中获得 ThreeTen Backport,即新类的向后移植(ThreeTen for JSR 310;请参阅底部的链接).
  • 在(旧版)Android 上使用 ThreeTen Backport 的 Android 版本.它被称为 ThreeTenABP.并确保使用子包从 org.threeten.bp 导入日期和时间类.
  • In Java 8 and later and on newer Android devices (from API level 26, I’m told) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the new classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.
  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.timeto Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.

这篇关于日历开始日期在同一天差异的结束日期之前的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆