问题
I need to convert a Date to its corresponding Milliseconds format.I am using getTime()
method of Date
class in Java for this. But the milliseond generated is not of my actual date. It is one day less than my date. For eg,
I have 22-Nov-2014
. If I convert this date to milliseconds format then 1,416,594,600,000
is generated. Actually this value corresponds to 21-Nov-2014
.
Please help me to get an exact milliseconds value corresponds to a Date
in java.
回答1:
1416594600000 corresponds to 2014-11-21T18:30:00Z. In other words, 6.30pm on November 21st 2014 in UTC. Epoch Converter is a great resource for checking things like that.
Now, a Date
object doesn't have any time zone information itself. It just represents a point in time. It sounds like your "22-Nov-2014" value was probably midnight in the local time zone (India?). If you are generating values from lots of different time zones and you don't store which time zone goes with which value, you've essentially lost some information here.
If you're trying to just represent a date (rather than a point in time) but you have to store it as a milliseconds-since-the-unix-epoch value, it probably makes sense to store midnight of that date in UTC, although you should also make it very clear that that's what you're doing. If you can, you should store the value in some way that makes it more obvious it's a date - such as using a DATE
field in a database. Date and time work is often really not as hard as we fear it to be if you know exactly what data you're modelling, and make that very clear everywhere in your code.
One way to make things clearer is to use a good date/time API which allows you to represent more kinds of data than java.util.Date
and java.util.Calendar
do. Joda Time is good for this, and Java 8 has the new java.time.*
API. I'd strongly advise you to move to one of those as soon as possible.
回答2:
tl;dr
LocalDate.of( 2014 , Month.NOVEMBER , 22 )
.atStartOfDay( ZoneId.of ( "Asia/Kolkata" ) )
.toInstant()
.toEpochMilli()
Details
The Answer by Jon Skeet is correct and wise.
Here is some example code using java.time classes.
LocalDate
The LocalDate class represents a date-only value without time-of-day and without 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.
ZoneId z = ZoneId.of( "America/Montreal" );
LocalDate today = LocalDate.now( z );
You can specify a LocalDate
.
LocalDate ld = LocalDate.of( 2014 , Month.NOVEMBER , 22 );
ld.toString(): 2014-11-22
ZonedDateTime
You desire a count-of-milliseconds-since-epoch. I do not recommend using a count-from-epoch for handling date-time. But if you insist, here we go.
A count-from-epoch of milliseconds means we need a date and a time-of-day. We have a date. I assume you want the first moment of the day as the time-of-day. Do not assume this time is 00:00:00
. Anomalies such as Daylight Saving Time (DST) may mean the day starts at some other time such as 01:00:00
. Let java.time determine that first moment by generating a ZonedDateTime
object from a LocalDate
.
LocalDate ld = LocalDate.of ( 2014 , Month.NOVEMBER , 22 );
ZoneId z = ZoneId.of ( "Asia/Kolkata" );
ZonedDateTime zdt = ld.atStartOfDay ( z );
zdt.toString(): 2014-11-22T00:00+05:30[Asia/Kolkata]
Instant
You can extract an Instant
if desired. The Instant class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction). A developer should think of UTC as the One True Time, and not think about their own parochial time zone while working.
Instant instant = zdt.toInstant ();
instant.toString(): 2014-11-21T18:30:00Z
From the instant we can ask for a count of milliseconds since epoch. Be aware this involves data-loss as any nanoseconds will be truncated to milliseconds.
long epochMillis = instant.toEpochMilli();
java.util.Date
I suggest avoiding the legacy date-time classes. But if you must, you can convert. Look to new methods added to the old classes like java.util.Date.
java.util.Date utilDate = java.util.Date.from( instant );
About java.time
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, & java.text.SimpleDateFormat.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
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?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
- The ThreeTenABP project adapts ThreeTen-Backport (mentioned above) for Android specifically.
- See How to use….
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.
来源:https://stackoverflow.com/questions/24817528/convert-date-to-milliseconds-in-java