I have a Date Object in following:

Date date=new Date("Mon, 05 May 2014 12:31:12 +0000")

I want to get Timestamp of date Object then :

date.getTime()

1399293072000

but this value not correct , correct value in following :

1399276872000 
//*** for get timestamp use of http://www.epochconverter.com ***\\

why ?

有帮助吗?

解决方案

First things first, from the JavaDoc for Date

Date(String s)
Deprecated. 
As of JDK version 1.1, replaced by DateFormat.parse(String s).

So the constructor you are using has been deprecated since 1997!

Next, onto the JavaDoc for Date.parse which the construtor uses:

It accepts many syntaxes; in particular, it recognizes the IETF standard date syntax: "Sat, 12 Aug 1995 13:30:00 GMT". It also understands the continental U.S. time-zone abbreviations, but for general use, a time-zone offset should be used: "Sat, 12 Aug 1995 13:30:00 GMT+0430" (4 hours, 30 minutes west of the Greenwich meridian). If no time zone is specified, the local time zone is assumed. GMT and UTC are considered equivalent.

Empathsis mine.

So, what timezone are you in? Presumably not UTC.

其他提示

You should specify local time zone while constructing Date object

Example:
Date date=new Date("Mon, 05 May 2014 12:31:12 GMT+0530");

Using Mon, 05 May 2014 12:31:12 +0000 at http://www.epochconverter.com I get 1399285872000 which is different from your timestamp.

Or you're passing different values, or there's a bug somewhere or

The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).

where the javadoc for java.util.Date.getTime() doesn't mention leap seconds.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top