How to get time from midnight in seconds

Firstly, this is a simple question that I am stuck in my Java 1 class. This is the static time that I set as 8:49:12 today, and I have to figure out how many seconds have passed after midnight and before midnight . 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 problem is that I do not know how to get time from and from midnight.

So basically the output should be:

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

And a space after - seconds.

Thanks, and explain why and how you decided how to solve it. I want to know: P

+4
source share
7 answers

Try simple math:

 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)); 
+5
source
 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; 
+3
source

Given the timing, you can 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); 

What are the exits ...

 from = 32372; to 54028 

Or you can 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()); 

What are the exits ...

 From midnight: 32372 To Midnight: 54028 
+3
source

JAVA has a rename called TimeUnit , which can convert time to any temporary device as follows:

  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) ); 
+3
source

Your answer is in the java.util.Calendar class. See get () / set () Methods: http://docs.oracle.com/javase/6/docs/api/java/util/Calendar.html

0
source
 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 the JodaTime library

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

0
source

@Vimal Bera's accepted answer is quite and sufficient due to the mathematical simplicity of the problem. But if you still prefer the “formalized” (supported by the library) approach without doing a “big” calculation, you can do it 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 
0
source

All Articles