Showing posts with label unix. Show all posts
Showing posts with label unix. Show all posts

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

Sunday, 8 March 2020

Get Instant 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 converts unix time stamp to Instant.

long unixTimestamp = 1577603360;
Instant instant = Instant.ofEpochSecond(unixTimestamp);

App.java
package com.sample.app;

import java.time.Instant;

public class App {

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

  Instant instant = Instant.ofEpochSecond(unixTimestamp);

  System.out.println(instant);
 }

}

Output
2019-12-29T07:09:20Z

You may like
Previous                                                    Next                                                    Home

Saturday, 7 March 2020

Get Date from Unix timestamp

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 Date.
public static Date getDate(long unixTimestamp) {
         return new Date(unixTimestamp * 1000);
}

App.java
package com.sample.app;

import java.util.Date;

public class App {

 public static Date getDate(long unixTimestamp) {
  return new Date(unixTimestamp * 1000);
 }

 public static void main(String[] args) {
  Date date = getDate(1577603360);
  
  System.out.println(date);
 }

}

Output
Sun Dec 29 12:39:20 IST 2019

You may like
Previous                                                    Next                                                    Home