I need a function that gives me how many seconds passed from the midnight. I am currently using System.currentTimeMillis()
but it gives me the UNIX like timestamp.
It would be a bonus for me if I could get the milliseconds too.
I need a function that gives me how many seconds passed from the midnight. I am currently using System.currentTimeMillis()
but it gives me the UNIX like timestamp.
It would be a bonus for me if I could get the milliseconds too.
If you're using Java >= 8, this is easily done :
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime midnight = now.atStartOfDay()
Duration duration = Duration.between(midnight, now);
long secondsPassed = duration.getSeconds();
If you're using Java 7 or less, you have to get the date from midnight via Calendar, and then substract.
Calendar c = Calendar.getInstance();
long now = c.getTimeInMillis();
c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
long passed = now - c.getTimeInMillis();
long secondsPassed = passed / 1000;
Using the java.time
framework built into Java 8 and later. See Tutorial.
import java.time.LocalTime
import java.time.ZoneId
LocalTime now = LocalTime.now(ZoneId.systemDefault()) // LocalTime = 14:42:43.062
now.toSecondOfDay() // Int = 52963
It is good practice to explicit specify ZoneId
, even if you want default one.
Duration.between( todayStart , now )
.getSeconds()
Java 8 and later has the java.time framework baked in.
By using ZonedDateTime
and time zone, we are handling anomalies such as Daylight Saving Time (DST). For example, in the United States a day can be 23, 24, or 25 hours long. So the time until tomorrow can vary by ±1 hour from one day to another.
First get the current moment.
ZoneId z = ZoneId.of( "America/Montreal" );
ZonedDateTime now = ZonedDateTime.now( z );
Now extract the date-only portion, a LocalDate
, and use that date to ask java.time when that day began for our desired time zone. Do not assume the day began at 00:00:00. Anomalies such as Daylight Saving Time (DST) mean that the day may begin at another time such as 01:00:00.
ZonedDateTime todayStart = now.toLocalDate().atStartOfDay( z ); // Crucial to specify our desired time zone!
Now we can get the delta between the current moment and the start of today. Such a span of time unattached to the timeline is represented by the Duration
class.
Duration duration = Duration.between( todayStart , now );
Ask the Duration
object for the total number of seconds in the entire span of time.
long secondsSoFarToday = duration.getSeconds();
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.
The simplest and fastest method to get the seconds since midnight for the current timezone:
One-time setup:
static final long utcOffset = TimeZone.getDefault().getOffset(System.currentTimeMillis());
If you use Apache Commons, you can use DateUtils.DAY_IN_MILLIS, otherwise define:
static final long DAY_IN_MILLIS = 24 * 60 * 60 * 1000;
And then, whenever you need the time...:
int seconds = (int)((System.currentTimeMillis() + utcOffset) % DateUtils.DAY_IN_MILLIS / 1000);
Note that you'll need to re-setup if there's a possibility that your program will run long enough, and Daylight Saving Times change occurs...
Use getTime().getTime() instead of getTimeInMillis() if you get an error about calendar being protected. Remember your imports:
import java.util.*;
will include them all while you are debugging:
Calendar now = Calendar.getInstance();
Calendar midnight = Calendar.getInstance();
midnight.set(Calendar.HOUR_OF_DAY, 0);
midnight.set(Calendar.MINUTE, 0);
midnight.set(Calendar.SECOND, 0);
midnight.set(Calendar.MILLISECOND, 0);
long ms = now.getTime().getTime() - midnight.getTime().getTime();
totalMinutesSinceMidnight = (int) (ms / 1000 / 60);
(System.currentTimeMillis()/1000) % (24 * 60 * 60)
Using JodaTime you can call:
int seconds = DateTime.now().secondOfDay().get();
int millis = DateTime.now().millisOfDay().get();
Like @secmask, if you need milli-seconds since GMT midnight, try
long millisSinceGMTMidnight = System.currentTimeMillis() % (24*60*60*1000);