0

我想编写两种方法,它们都依赖于一天中还剩多少时间的信息(到一天结束还剩多少秒,以及一天中已经过去了多少百分比) . 不同的日子有不同的(有时只是)秒,有时是几个小时,所以我只依靠 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);
    }
}
4

2 回答 2

0

我建议您使用现代 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 教程:日期时间解释如何使用 java.time。

于 2021-02-22T18:18:50.863 回答
0

我不确定

不同的日子有不同的(有时只是)秒,有时是几个小时,所以我只依靠 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 + "%");

在这里您可以找到更多示例,并根据您的需要进行调整

于 2021-02-22T12:36:25.383 回答