Java: Date from Unix Timestamp

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 a getTime() method that returns the milliseconds since EPOC, it is required that you give to SimpleDateFormat 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 date time string to unix timestamp

java.time and ThreeTen Backport

The comment by ernest_k is well thought out and solves your issue:

    DateTimeFormatter formatter
= DateTimeFormatter.ofPattern("EEEMMMddHH:mm:sszyyyy", Locale.ENGLISH);
String dateTimeString = "TueDec2618:47:09UTC2017";
ZonedDateTime dateTime = ZonedDateTime.parse(dateTimeString, formatter);
long unixTimestamp = dateTime.toEpochSecond();
System.out.println("Parsed date-time " + dateTime + " Unix timestamp " + unixTimestamp);

The output from running on ThreeTen Backport 1.3.6, tested on Java 1.7.0_79, is:

Parsed date-time 2017-12-26T18:47:09Z[Zulu] Unix timestamp 1514314029

Question: How can I use ZonedDateTime and DateTimeFormatter on Java 7?

I am still on Java 7 btw so can't use ZonedDataTime and
DateTimeFormatter but I can use joda library.

Indeed you can. java.time just requires at least Java 6.

  • In Java 8 and later and on newer Android devices (from API level 26) the modern API comes built-in.
  • In Java 6 and 7 get the ThreeTen Backport, the backport of the modern classes (ThreeTen for JSR 310; see the links at the bottom).
  • On (older) Android use the Android edition of ThreeTen Backport. It’s called ThreeTenABP. And make sure you import the date and time classes from org.threeten.bp with subpackages.

While Joda-Time would be another nice solution, I believe that you should prefer the ThreeTen Backport over Joda-Time (though opinions differ). The Joda-Time home page advises:

Note that Joda-Time is considered to be a largely “finished” project.
No major enhancements are planned. If using Java SE 8, please migrate
to java.time (JSR-310).

So java.time seems to be the future-proof solution.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Java Specification Request (JSR) 310, where java.time was first described.
  • ThreeTen Backport project, the backport of java.time to Java 6 and 7 (ThreeTen for JSR-310).
  • ThreeTenABP, Android edition of ThreeTen Backport
  • Question: How to use ThreeTenABP in Android Project, with a very thorough explanation.
  • Joda-Time home page

Date to Unix timestamp

I think you can search for answers at StackOverflow, instead of posting a new question, since it make StackOverflow better, I just did a quick search and here they are:
Getting unix timestamp from Date()

or here is the code that straight forward:

Date currentDate = new Date();
currentDate.getTime() / 1000;

Recently, people prefer jodaTime:

DateTime dateTime = new DateTime();
long unix = dateTime.getMillis()/1000;


Related Topics



Leave a reply



Submit