【问题标题】:Measuring Time-span from startpoint till current time, end of day, or endpoint测量从起点到当前时间、一天结束或终点的时间跨度
【发布时间】:2021-02-22 10:37:48
【问题描述】:

我想编写两种方法,它们都依赖于一天中还剩多少时间的信息(距离一天结束还剩多少秒,以及一天中已经有多少百分比)已经通过)。不同的日子有不同的(有时只是)秒,有时是几个小时,所以我只依靠 24 * 60 * 60 并减去 sinceMidnight() 的结果是行不通的。 我试图在当前时间上增加一天,但到目前为止我失败了。

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;

public class TimesOfSeconds {

    public static void sinceMidnight(int intHours, int intMinutes, int intSeconds) {
        int hoursInSeconds = intHours * 60 * 60;
        int minutesInSeconds = intMinutes * 60;
        int secondsSinceMidnight = hoursInSeconds + minutesInSeconds + intSeconds;
        System.out.println(secondsSinceMidnight + " Seconds since Midnight!");
    }

    public static void tillEndOfDay() {

    }

    public static void percentOfDayPassed() {

    }

    public static void getTheTimeRight(int intHours, int intMinutes, int intSeconds) {
        System.out.println(intHours + " " + intMinutes + " " + intSeconds);
    }

    public static void main(String[] args) {

        Calendar cal = Calendar.getInstance();
        SimpleDateFormat justHours = new SimpleDateFormat("HH");
        int intHours = Integer.parseInt(justHours.format(cal.getTime()));
        SimpleDateFormat justMinutes = new SimpleDateFormat("mm");
        int intMinutes = Integer.parseInt(justMinutes.format(cal.getTime()));
        SimpleDateFormat justSeconds = new SimpleDateFormat("ss");
        int intSeconds = Integer.parseInt(justSeconds.format(cal.getTime()));

        getTheTimeRight(intHours, intMinutes, intSeconds);
        sinceMidnight(intHours, intMinutes, intSeconds);
    }
}

【问题讨论】:

  • 我建议你不要使用CalendarSimpleDateFormat。这些类设计不佳且早已过时,后者尤其是出了名的麻烦。由于您可以使用 java.time, the modern Java date and time API 中的 LocalDate,因此只需坚持使用 java.time 即可完成所有日期和时间工作。

标签: java date time elapsedtime timeofday


【解决方案1】:

我不确定

不同的日子有不同的(有时只是)秒,有时是几个小时,所以我只依靠 24 * 60 * 60 并减去 sinceMidnight() 的结果是行不通的。

也许你在谈论一些工作日?

无论如何 - 为了计算持续时间,周期,java.time 有很好的工具来为你做数学。

        LocalDateTime todayMidnight = LocalDate.now().atStartOfDay();
        Duration since = Duration.between(todayMidnight, LocalDateTime.now());
        long sinceMidnight = since.getSeconds();
        System.out.println(sinceMidnight + " Seconds since Midnight!");

        LocalDateTime endOfTheDay = LocalDate.now().plusDays(1).atStartOfDay();
        Duration till = Duration.between(LocalDateTime.now(), endOfTheDay);
        long tillEndOfDay = till.getSeconds();
        System.out.println(tillEndOfDay + " Seconds till end of the day");

        long percentOfDayPassed = since.getSeconds() * 100 / (60*60*24);
        System.out.println("percentOfDayPassed: " + percentOfDayPassed + "%");

Here you can find some more examples,并根据您的需要进行调整

【讨论】:

    【解决方案2】:

    我建议您使用现代 Java 日期和时间 API java.time 来处理您的时间工作。我想你的意思是说一天可以是 23、24 或 25 小时或其他长度。考虑到这一点,请使用 ZonedDateTime 和正确的时区,因为此类知道此类异常。

        ZoneId zone = ZoneId.systemDefault();
        
        ZonedDateTime timeNow = ZonedDateTime.now(zone);
        System.out.println("Time now:                 " + timeNow.toLocalTime());
        
        LocalDate today = timeNow.toLocalDate();
        ZonedDateTime startOfDay = today.atStartOfDay(zone);
        System.out.println("Seconds since midnight:   "
                + ChronoUnit.SECONDS.between(startOfDay, timeNow));
        
        ZonedDateTime endOfDay = today.plusDays(1).atStartOfDay(zone);
        System.out.println("Seconds until midnight:   "
                + ChronoUnit.SECONDS.between(timeNow, endOfDay));
        
        long nanosOfDayPassed = ChronoUnit.NANOS.between(startOfDay, timeNow);
        long dayLengthNanos = ChronoUnit.NANOS.between(startOfDay, endOfDay);
        double percentagePassed = 100.0 * nanosOfDayPassed / dayLengthNanos;
        System.out.format("Percentage of day passed: %f %%%n", percentagePassed);
    

    刚刚运行时的输出:

    Time now:                 19:17:38.994190
    Seconds since midnight:   69458
    Seconds until midnight:   16941
    Percentage of day passed: 80,392354 %
    

    我将留给你像你在问题中那样用很好的方法包装代码。

    链接

    Oracle tutorial: Date Time 解释如何使用 java.time。

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 2021-11-09
      • 2020-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-10
      相关资源
      最近更新 更多