Monday, 9 March 2020

Get Calendar from Unix time stamp

Unix time is the number of seconds that have elapsed since 00:00:00, 1 January 1970, UTC, without counting the leap seconds.

You can get Unix time stamp by executing the command date '+%s' on terminal.

$date '+%s'
1577603360

Below snippet convers unix time stamp to Calendar.

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(unixTimestamp * 1000);

App.java
package com.sample.app;

import java.util.Calendar;

public class App {

 public static void main(String[] args) {
  long unixTimestamp = 1577603360;

  Calendar calendar = Calendar.getInstance();
  calendar.setTimeInMillis(unixTimestamp * 1000);

  System.out.println(calendar.getTime());

 }

}

Output
Sun Dec 29 12:39:20 IST 2019



You may like
Previous                                                    Next                                                    Home

No comments:

Post a Comment