Java: Date from unix timestamp
NickName:Ariyan Ask DateTime:2010-07-30T20:22:45

Java: Date from unix timestamp

I need to convert a unix timestamp to a date object.
I tried this:

java.util.Date time = new java.util.Date(timeStamp);

Timestamp value is: 1280512800

The Date should be "2010/07/30 - 22:30:00" (as I get it by PHP) but instead I get Thu Jan 15 23:11:56 IRST 1970.

How should it be done?

Copyright Notice:Content Author:「Ariyan」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/3371326/java-date-from-unix-timestamp

Answers
tmr 2013-09-16T21:47:41

If you are converting a timestamp value on a different machine, you should also check the timezone of that machine. For example;\n\nThe above decriptions will result different Date values, if you run with EST or UTC timezones.\n\nTo set the timezone; aka to UTC,\nyou can simply rewrite;\n\n TimeZone.setDefault(TimeZone.getTimeZone(\"UTC\"));\n java.util.Date time= new java.util.Date((Long.parseLong(timestamp)*1000));\n",


Arda Kazancı 2020-01-10T09:52:50

For kotlin\n\nfun unixToDate(timeStamp: Long) : String? {\n val time = java.util.Date(timeStamp as Long * 1000)\n val sdf = SimpleDateFormat(\"yyyy-MM-dd\")\n return sdf.format(time)\n\n}\n",


Damir Isanbirdin 2020-06-19T00:17:46

Sometimes you need to work with adjustments.\n\nDon't use cast to long! Use nanoadjustment.\n\nFor example, using Oanda Java API for trading you can get datetime as UNIX format.\n\nFor example: 1592523410.590566943\n\n System.out.println(\"instant with nano = \" + Instant.ofEpochSecond(1592523410, 590566943));\n System.out.println(\"instant = \" + Instant.ofEpochSecond(1592523410));\n\n\nyou get:\n\ninstant with nano = 2020-06-18T23:36:50.590566943Z\ninstant = 2020-06-18T23:36:50Z\n\n\nAlso, use:\n\n Date date = Date.from( Instant.ofEpochSecond(1592523410, 590566943) );\n",


Frank.Chang 2023-01-07T13:06:17

LocalDateTime is another choice, like:\nLocalDateTime.ofInstant(Instant.ofEpochSecond(unixtime), ZoneId.systemDefault())\n",


Pablo Santa Cruz 2010-07-30T12:24:01

For 1280512800, multiply by 1000, since java is expecting milliseconds:\n\njava.util.Date time=new java.util.Date((long)timeStamp*1000);\n\n\nIf you already had milliseconds, then just new java.util.Date((long)timeStamp);\n\n\nFrom the documentation:\n\n\n Allocates a Date object and\n initializes it to represent the\n specified number of milliseconds since\n the standard base time known as \"the\n epoch\", namely January 1, 1970,\n 00:00:00 GMT.\n",


micha 2014-07-11T17:49:51

java.time\n\nJava 8 introduced a new API for working with dates and times: the java.time package.\n\nWith java.time you can parse your count of whole seconds since the epoch reference of first moment of 1970 in UTC, 1970-01-01T00:00Z. The result is an Instant.\n\nInstant instant = Instant.ofEpochSecond( timeStamp );\n\n\nIf you need a java.util.Date to interoperate with old code not yet updated for java.time, convert. Call new conversion methods added to the old classes. \n\nDate date = Date.from( instant );\n",


Marco Fantasia 2011-09-13T15:52:56

This is the right way:\n\nDate date = new Date ();\ndate.setTime((long)unix_time*1000);\n",


Basil Bourque 2014-07-11T20:00:16

tl;dr\n\nInstant.ofEpochSecond( 1_280_512_800L )\n\n\n\n 2010-07-30T18:00:00Z\n\n\njava.time\n\nThe new java.time framework built into Java 8 and later is the successor to Joda-Time. \n\nThese new classes include a handy factory method to convert a count of whole seconds from epoch. You get an Instant, a moment on the timeline in UTC with up to nanoseconds resolution.\n\nInstant instant = Instant.ofEpochSecond( 1_280_512_800L );\n\n\n\n instant.toString(): 2010-07-30T18:00:00Z\n\n\nSee that code run live at IdeOne.com.\n\n\n\nAsia/Kabul or Asia/Tehran time zones ?\n\nYou reported getting a time-of-day value of 22:30 instead of the 18:00 seen here. I suspect your PHP utility is implicitly applying a default time zone to adjust from UTC. My value here is UTC, signified by the Z (short for Zulu, means UTC). Any chance your machine OS or PHP is set to Asia/Kabul or Asia/Tehran time zones? I suppose so as you report IRST in your output which apparently means Iran time. Currently in 2017 those are the only zones operating with a summer time that is four and a half hours ahead of UTC.\n\nSpecify 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 abbreviation such as EST or IST or IRST as they are not true time zones, not standardized, and not even unique(!). \n\nIf you want to see your moment through the lens of a particular region's time zone, apply a ZoneId to get a ZonedDateTime. Still the same simultaneous moment, but seen as a different wall-clock time.\n\nZoneId z = ZoneId.of( \"Asia/Tehran\" ) ;\nZonedDateTime zdt = instant.atZone( z ); // Same moment, same point on timeline, but seen as different wall-clock time.\n\n\n\n 2010-07-30T22:30+04:30[Asia/Tehran]\n\n\nConverting from java.time to legacy classes\n\nYou should stick with the new java.time classes. But you can convert to old if required.\n\njava.util.Date date = java.util.Date.from( instant );\n\n\nJoda-Time\n\nUPDATE: The Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes. \n\nFYI, the constructor for a Joda-Time DateTime is similar: Multiply by a thousand to produce a long (not an int!).\n\nDateTime dateTime = new DateTime( ( 1_280_512_800L * 1000_L ), DateTimeZone.forID( \"Europe/Paris\" ) );\n\n\nBest to avoid the notoriously troublesome java.util.Date and .Calendar classes. But if you must use a Date, you can convert from Joda-Time.\n\njava.util.Date date = dateTime.toDate();\n\n\n\n\nAbout java.time\n\nThe 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.\n\nThe Joda-Time project, now in maintenance mode, advises migration to the java.time classes.\n\nTo learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.\n\nYou may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.* classes.\n\nWhere to obtain the java.time classes? \n\n\nJava SE 8, Java SE 9, and later\n\n\nBuilt-in. \nPart of the standard Java API with a bundled implementation.\nJava 9 adds some minor features and fixes.\n\nJava SE 6 and Java SE 7\n\n\nMuch of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.\n\nAndroid\n\n\nLater versions of Android bundle implementations of the java.time classes.\nFor earlier Android (<26), the ThreeTenABP project adapts ThreeTen-Backport (mentioned above). See How to use ThreeTenABP….\n\n\n\nThe 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.",


Stefan 2012-06-26T10:15:35

Looks like Calendar is the new way to go:\n\nCalendar mydate = Calendar.getInstance();\nmydate.setTimeInMillis(timestamp*1000);\nout.println(mydate.get(Calendar.DAY_OF_MONTH)+\".\"+mydate.get(Calendar.MONTH)+\".\"+mydate.get(Calendar.YEAR));\n\n\nThe last line is just an example how to use it, this one would print eg \"14.06.2012\".\n\nIf you have used System.currentTimeMillis() to save the Timestamp you don't need the \"*1000\" part.\n\nIf you have the timestamp in a string you need to parse it first as a long: Long.parseLong(timestamp).\n\nhttps://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html",


f1sh 2010-07-30T12:24:58

Date's constructor expects the timeStamp value to be in milliseconds.\nMultiply your timestamp's value with 1000, then pass it to the constructor.",


More about “Java: Date from unix timestamp” related questions

Java: Date from unix timestamp

I need to convert a unix timestamp to a date object. I tried this: java.util.Date time = new java.util.Date(timeStamp); Timestamp value is: 1280512800 The Date should be "2010/07/30 - 22:30:00" ...

Show Detail

Java: Date from unix timestamp

I need to convert a unix timestamp to a date object. I tried this: java.util.Date time = new java.util.Date(timeStamp); Timestamp value is: 1280512800 The Date should be "2010/07/30 - 22:30:00" ...

Show Detail

Java: Date from unix timestamp

I need to convert a unix timestamp to a date object. I tried this: java.util.Date time = new java.util.Date(timeStamp); Timestamp value is: 1280512800 The Date should be "2010/07/30 - 22:30:00" ...

Show Detail

Java: Date from unix timestamp

I need to convert a unix timestamp to a date object. I tried this: java.util.Date time = new java.util.Date(timeStamp); Timestamp value is: 1280512800 The Date should be "2010/07/30 - 22:30:00" ...

Show Detail

Getting unix timestamp from Date()

I can convert a unix timestamp to a Date() object by putting the long value into the Date() constructor. For eg: I could have it as new Date(1318762128031). But after that, how can I get back the...

Show Detail

From timestamp to date in Java

I've a problem creating new date in Java. I receive from webservice a timestamp (for example 1397692800). You can see from this online converter that is equal to 17 Apr 2014. When I try to create a...

Show Detail

Problems converting a UNIX_TIMESTAMP from a SQL query into a Date in java

I have a MySQL query made in this way: SELECT AVG(REALPOWER) AS REALPOWER, `OBJECTID`, UNIX_TIMESTAMP(LASTUPDATE)/(30*60) AS LASTUPDATE FROM POWER GROUP BY UNIX_TIMESTAMP(LASTUPDATE)/(30*60),

Show Detail

Problems to convert date to timestamp, Spark date to timestamp from unix_timestamp return null

Problems to convert date to timestamp, Spark date to timestamp from unix_timestamp return null. scala&gt; import org.apache.spark.sql.functions.unix_timestamp scala&gt; spark.sql("select from_uni...

Show Detail

Unix command for Date conversion from timestamp to standard dateformat?

I need to convert date from timestamp(eg: 1362553920) format to standard date format (DD:MM:YYYY HH:MM:SS). Is there any unix commands available for this?. If it is not possible in unix, how ca...

Show Detail

Incorrect UNIX timestamp from String Date

I am trying to convert a date in String format into UNIX timestamp, I am able to convert it but when I check the timestamp it displays incorrect date. I am using the following code to convert a D...

Show Detail