Java: Date from unix timestamp
For 1280512800
, multiply by 1000, since java is expecting milliseconds:
java.util.Date time=new java.util.Date((long)timeStamp*1000);
If you already had milliseconds, then just new java.util.Date((long)timeStamp);
From the documentation:
Allocates a Date object and
initializes it to represent the
specified number of milliseconds since
the standard base time known as "the
epoch", namely January 1, 1970,
00:00:00 GMT.
Convert unix timestamp to date in java
You can use SimlpeDateFormat to format your date like this:
long unixSeconds = 1372339860;
// convert seconds to milliseconds
Date date = new java.util.Date(unixSeconds*1000L);
// the format of your date
SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
// give a timezone reference for formatting (see comment at the bottom)
sdf.setTimeZone(java.util.TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
The pattern that SimpleDateFormat
takes if very flexible, you can check in the javadocs all the variations you can use to produce different formatting based on the patterns you write given a specific Date
. http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html
- Because a
Date
provides agetTime()
method that returns the milliseconds since EPOC, it is required that you give toSimpleDateFormat
a timezone to format the date properly acording to your timezone, otherwise it will use the default timezone of the JVM (which if well configured will anyways be right)
Getting unix timestamp from Date()
getTime()
retrieves the milliseconds since Jan 1, 1970 GMT passed to the constructor. It should not be too hard to get the Unix time (same, but in seconds) from that.
Java get current day from unix timestamp
You can use SimpleDateFormat to format your date:
long unixSeconds = 1372339860;
Date date = new Date(unixSeconds*1000L); // *1000 is to convert seconds to milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z"); // the format of your date
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4")); // give a timezone reference for formating (see comment at the bottom
String formattedDate = sdf.format(date);
System.out.println(formattedDate);
You can also convert it to milliseconds by multiplying the timestamp by 1000:
java.util.Date dateTime=new java.util.Date((long)timeStamp*1000);
After doing it, you can get what you want:
Calendar cal = Calendar.getInstance();
cal.setTime(dateTime);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH); //here is what you need
int day = cal.get(Calendar.DAY_OF_MONTH);
Convert Unix time into readable Date in Java
SimpleDateFormat sdf = new SimpleDateFormat("MMMM d, yyyy 'at' h:mm a");
String date = sdf.format(myTimestamp);
Java Converting 19-digit Unix Timestamp to a Readable Date
Try using this
Date date = new java.util.Date(timeStamp/1000000);
Instead of multiplying by 1000, divide by 1000000
How to convert a Unix timestamp to 2000-01-01 or 2000-05-24 20:00:00 format or reverse in Deno?
Aside from the standard Javascript ways, there is a date/time library for Deno called Ptera, that can be used as follows:
import { datetime } from "https://deno.land/x/ptera/mod.ts";
const dt = datetime("2000-05-24 20:00:00");
console.log(dt.format("X")); // X for Unix timestamp in seconds 959198400
console.log(dt.format("x")); // x for "Unix timestamp" in milliseconds 959198400000
const dt2 = datetime(1646245390158);
console.log(dt2.format("YYYY-MM-dd HH:mm:ss")); // output: 2022-03-02 19:23:10
A UNIX timestamp is the number of seconds since 1970-01-01 00:00:00 UTC, the Javascript timestamp is in milliseconds and sometimes in documentations, they also call this as a UNIX timestamp or UNIX Epoch time.
A detailed reference about the formatting options is available here.
Java convert unix timestamp to wrong time
You're multiplying a timestamp which is already in milliseconds since the Unix epoch by 1000. You just want:
Date date = new Date(timestamp);
If you look at all of the date, not just the time, you'll see it's currently in 46886!
Related Topics
Android Change Material Elevation Shadow Color
Fill Fields in Webview Automatically
Create a Bitmap/Drawable from File Path
Disable Ssl Certificate Check in Retrofit Library
Is It Ok to Save a JSON Array in Sharedpreferences
Getting JSONobject from JSONarray
Is There an Easier Way to Parse Xml in Java
Android Fragmentmanager Backstackrecord.Run Throwing Nullpointerexception
How to Set Seekbar Min and Max Value
How to Navigate from One Screen to Another Screen
Android - How to Replace Part of a String by Another String
How to Get Difference Between Two Dates in Android, Tried Every Thing and Post
How to Get the Selected Value of a Spinner
Linearlayoutmanager Setreverselayout() == True But Items Stack from Bottom
Java.Lang.Nullpointerexception: Attempt to Invoke Virtual Method on a Null Object Reference