0

I'd like to write two methods which both rely on the information on how much time is left in the day (how many seconds there are left till the end of the day, as well as how much percent of the day already have passed). Different days have a different span of (sometimes just) seconds and sometimes hours so it won't work for me to just count on 24 * 60 * 60 and subtract the result of sinceMidnight(). I tried to add a day to the current time, yet till now I failed.

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);
    }
}
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • I recommend you don’t use `Calendar` and `SimpleDateFormat`. Those classes are poorly designed and long outdated, the latter in particular notoriously troublesome. Since you can use `LocalDate` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/), just stick to java.time for all of your date and time work. – Ole V.V. Feb 22 '21 at 18:00

2 Answers2

0

I'm not sure about

Different days have a different span of (sometimes just) seconds and sometimes hours so it won't work for me to just count on 24 * 60 * 60 and subtract the result of sinceMidnight().

Maybe you are talking about some work days?

Anyway - in order to calculate durations, periods, java.time has good tools to do the math for you.

        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, and adjust this to your needs

mehowthe
  • 761
  • 6
  • 14
0

I recommend that you use java.time, the modern Java date and time API, for your time work. I think you meant to say that a day can be 23, 24 or 25 hours long or some other length. To take this into account use ZonedDateTime with the correct time zone since this class knows about such anomalies.

    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);

Output was when running just now:

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

I am leaving to you to wrap the code in nice methods like you did in the question.

Link

Oracle tutorial: Date Time explaining how to use java.time.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161