I want to get the current time and date in milliseconds. How can I get this?
I tried this:
Date date=new Date() ;
System.out.println("Today is " +date.getTime());
It will return the milliseconds from the 1 Jan 1970.
But I want the current millisecods of the today's date, like:
23:59:00 = 86340000 milliseconds
JavaScript - Date getMilliseconds() Method Javascript date getMilliseconds() method returns the milliseconds in the specified date according to local time. The value returned by getMilliseconds() is a number between 0 and 999.
A millisecond (from milli- and second; symbol: ms) is a unit of time in the International System of Units (SI) equal to one thousandth (0.001 or 10−3 or 1/1000) of a second and to 1000 microseconds.
A millisecond (ms or msec) is one thousandth of a second and is commonly used in measuring the time to read to or write from a hard disk or a CD-ROM player or to measure packet travel time on the Internet. For comparison, a microsecond (us or Greek letter mu plus s) is one millionth (10-6) of a second.
To convert this instant to the number of milliseconds from the epoch, use the toEpochMilli () method. Alternatively, you can convert this instant to the number of seconds elapsed since the Unix epoch with the getEpochSecond () method. That’s all about getting milliseconds elapsed since the epoch in Java.
* 1 tick = 0.0001 milliseconds = 100 nanoseconds. Genesis & History. This site provides the current time in milliseconds elapsed since the UNIX epoch (Jan 1, 1970) as well as in other common formats including local / UTC time comparisons. You can also convert milliseconds to date & time and the other way around.
Millis is the popular abbreviation for milliseconds. The formal one would be ms. Another one is millisecs but this is very rare. Leap seconds are one-second adjustments added to the UTC time to synchronize it with solar time. Leap seconds tend to cause trouble with software. For example, on June 30, 2012 you had the time 23:59:60.
Therefore the maximum difference between 2 local times on Earth is 26 hours. This site provides the current time in milliseconds elapsed since the UNIX epoch (Jan 1, 1970) as well as in other common formats including local / UTC time comparisons.
This is not the correct approach for Java 8 or newer. This answer is retained for posterity; for any reasonably modern Java use Basil Bourque's approach instead.
The following seems to work.
Calendar rightNow = Calendar.getInstance();
// offset to add since we're not UTC
long offset = rightNow.get(Calendar.ZONE_OFFSET) +
rightNow.get(Calendar.DST_OFFSET);
long sinceMidnight = (rightNow.getTimeInMillis() + offset) %
(24 * 60 * 60 * 1000);
System.out.println(sinceMidnight + " milliseconds since midnight");
The problem is that date.getTime()
returns the number of milliseconds from 1970-01-01T00:00:00Z, but new Date()
gives the current local time. Adding the ZONE_OFFSET
and DST_OFFSET
from the Calendar
class gives you the time in the default/current time zone.
Try:
(d.getTime() % (86400000))
Note: 86400000 is the number of milliseconds in a day.
The modern approach uses java.time classes that supplant the troublesome old legacy date-time classes.
Getting the span of time from the beginning of today to the current moment is more complicated that you might expect.
First, determining “today” requires a time zone. A time zone is crucial in determining a date. For any given moment, the date varies around the globe by zone. For example, a few minutes after midnight in Paris France is a new day while still “yesterday” in Montréal Québec.
Specify a proper time zone name in the format of continent/region
, such as America/Montreal
, Africa/Casablanca
, or Pacific/Auckland
. Never use the 3-4 letter pseudo-zones such as EST
or IST
as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the JVM’s current default is applied implicitly. Better to be explicit, as the default may be changed at any moment during runtime by any code in any thread of any app within the JVM.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
The ZonedDateTime
class represents a moment (a date & time-of-day) as seen by the people of a particular region (a time zone, a ZoneId
).
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
Next we need to determine the first moment of the day. Do not assume the day starts at 00:00:00. Because of anomalies such as Daylight Saving Time (DST), the day may start at another time such as 01:00:00. Let java.time determine the first moment of the day in a particular zone on a particular date.
First extract the date-only portion of our ZonedDateTime
object above.
LocalDate ld = zdt.toLocalDate() ;
Ask for first moment of the day in our desired zone.
ZonedDateTime zdtStartOfDay = ld.atStartOfDay( z ) ;
Now we have the pair of pieces we need: first moment of the day, and the current moment. We ask the ChronoUnit
enum object MILLISECONDS
to calculate the time elapsed between that pair. Note that this may involve data loss, as the ZonedDateTime
objects may hold microseconds or nanoseconds being ignored in this calculation of milliseconds.
long millisElapsedToday = ChronoUnit.MILLISECONDS.between( zdtStartOfDay , zdt ) ;
You may also be interested in representing that span-of-time as unattached to the timeline in a Duration
object.
Duration d = Duration.between( zdtStartOfDay , zdt ) ;
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.
With a JDBC driver complying with JDBC 4.2 or later, you may exchange java.time objects directly with your database. No need for strings or java.sql* classes.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With