w3resource logo


Java Programming Exercies

Java DateTime, Calendar Exercises: Convert a unix timestamp to date in Java

Java DateTime, Calendar : Exercise-36 with Solution

Write a Java program to convert a unix timestamp to date in Java.

Sample Solution:

Java Code:

import java.util.*;
import java.text.*;

public class Exercise36 {
  public static void main(String[] args)
   //Unix seconds
   long unix_seconds = 1372339860;
   //convert seconds to milliseconds
   Date date = new Date(unix_seconds*1000L); 
   // format of the date
   SimpleDateFormat jdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
   String java_date = jdf.format(date);

Sample Output:

2013-06-27 09:31:00 GMT-04:00

Java Practice online:

Improve this sample solution and post your code through Disqus