6

First off, this is a simple question that I'm stuck on in my Java 1 class. It's a static time that I set already as 8:49:12 "today" and I'm to figure out how many seconds past midnight and "to" midnight this represents. 8 hours, 49 minutes, and 12 seconds. Here is my code now:

hour    = 8;
minute  = 59;
second  = 32;
System.out.println("The static time used for this program was: " + hour + ":" + minute + ":" + second);

My issue is that I have no clue on how to get the time from and since midnight.

So basically the output needs to be:

Number of seconds since midnight: 
Number of seconds to midnight:

And the space after is for the seconds.

Thanks, and please explain why, and how you chose how to solve this. I want to learn :P

rogerdpack
  • 56,766
  • 33
  • 241
  • 361
TaylorTDHouse
  • 233
  • 2
  • 4
  • 8
  • 4
    Sounds to me like you just want someone to do your work for you. Please show what you have tried so far. – Scott Helme Sep 24 '13 at 06:52
  • 1
    Possible duplicate of [How can I find the amount of seconds passed from the midnight with Java?](http://stackoverflow.com/questions/4389500/how-can-i-find-the-amount-of-seconds-passed-from-the-midnight-with-java) – Przemek Jan 13 '16 at 20:36

7 Answers7

9

Try this simple maths :

System.out.println("Number of seconds since midnight:" +(second + (minute*60) + (hour*3600)));
System.out.println("Number of seconds to midnight:" +((60-second) + ((60-1-minute)*60) + (24-1-hour)*3600));
Vimal Bera
  • 10,155
  • 4
  • 23
  • 47
4

There is an enum in JAVA called TimeUnit that can convert time to any time unit like this:

        int hour    = 8;
        int minute  = 59;
        int second  = 32;
        System.out.println("The static time used for this program was: " + hour + ":" + minute + ":" + second);

        long secInMidnight = TimeUnit.HOURS.toSeconds(24);
        long timeInSeconds = (TimeUnit.HOURS.toSeconds(8) + TimeUnit.MINUTES.toSeconds(minute) + second);

        System.out.println("\nSince midnight: " + timeInSeconds + "\nUntil midnight: " + (secInMidnight - timeInSeconds) );
Heisenberg
  • 3,113
  • 3
  • 26
  • 52
3
final static int SEC_IN_MIN = 60;
final static int SEC_IN_HOUR = SEC_IN_MIN * 60;
int secFromMidnight = hour * SEC_IN_HOUR + minute * SEC_IN_MIN + second;
int secToMidnight = (24 * SEC_IN_HOUR) - secFromMidnight;
Gabriel Negut
  • 13,422
  • 4
  • 36
  • 44
3

Given the idiosyncrasies of time, you could simply use the Calendar API

Calendar fromMidnight = Calendar.getInstance();
fromMidnight.set(Calendar.HOUR, 0);
fromMidnight.set(Calendar.MINUTE, 0);
fromMidnight.set(Calendar.SECOND, 0);
fromMidnight.set(Calendar.MILLISECOND, 0);

Calendar toMidnight = Calendar.getInstance();
toMidnight.setTime(fromMidnight.getTime());
toMidnight.add(Calendar.DATE, 1);

System.out.println(fromMidnight.getTime());
System.out.println(toMidnight.getTime());

Calendar toFromTime = Calendar.getInstance();
toFromTime.set(Calendar.HOUR, 8);
toFromTime.set(Calendar.MINUTE, 59);
toFromTime.set(Calendar.SECOND, 32);
toFromTime.set(Calendar.MILLISECOND, 0);

long secondsFromMidnight = (toFromTime.getTimeInMillis() - fromMidnight.getTimeInMillis()) / 1000;
long secondsToMidnight = (toMidnight.getTimeInMillis() - toFromTime.getTimeInMillis()) / 1000;

System.out.println("from = " + secondsFromMidnight + "; to " + secondsToMidnight);

Which outputs...

from = 32372; to 54028

Or you could use JodaTime...

MutableDateTime now = MutableDateTime.now();
now.setMillisOfDay(0);
now.setSecondOfDay(32);
now.setMinuteOfDay(59);
now.setHourOfDay(8);
DateTime fromMidnight = now.toDateTime().toDateMidnight().toDateTime();
DateTime toMidnight = fromMidnight.plusDays(1);

Duration duration = new Duration(fromMidnight, toMidnight);

Duration dFromMidnight = new Duration(fromMidnight, now);
System.out.println("From midnight: " + dFromMidnight.getStandardSeconds());

Duration dToMidnight = new Duration(now, toMidnight);
System.out.println("To Midnight: " + dToMidnight.getStandardSeconds());

Which outputs...

From midnight: 32372
To Midnight: 54028
MadProgrammer
  • 336,120
  • 22
  • 219
  • 344
2

The accepted answer of @Vimal Bera is fine and completely sufficient due to the mathematical simplicity of the problem. But if you still prefer a "formalized" (supported by library) approach without doing "much" calculation then you could do this in Java-8:

LocalTime time = LocalTime.of(8, 59, 32);
System.out.println(
  "Seconds since midnight: " 
  + time.get(ChronoField.SECOND_OF_DAY)); // 32372
System.out.println(
  "Seconds to midnight: " 
  + (86400 - time.get(ChronoField.SECOND_OF_DAY))); // 54028
Meno Hochschild
  • 40,702
  • 7
  • 93
  • 118
0

Your answer is in java.util.Calendar class. Please see get()/set() methods: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

angel_navarro
  • 1,757
  • 10
  • 11
0
import java.util.Calendar;

public class HelloWorld{

 public static void main(String []args){
    int hour    = 8;
    int minute  = 59;
    int second  = 32;
    System.out.println("The static time used for this program was: " + hour + ":" + minute + ":" + second);
    final Calendar currentTime = Calendar.getInstance();
    currentTime.set(2013,Calendar.SEPTEMBER,24,8,59,32);
    final Calendar midNight = Calendar.getInstance();
    midNight.clear();
    midNight.set(2013, Calendar.SEPTEMBER, 25);
    System.out.println(.001*(midNight.getTimeInMillis() - currentTime.getTimeInMillis()));
 }

}

Consider using Calendar and or try JodaTime library

http://www.joda.org/joda-time/

smajlo
  • 942
  • 10
  • 21