London
London

Reputation: 15274

Time conversion in java

How can I convert unix time in java ?

Get this :

~#>date -d @1305176400
Thu May 12 00:00:00 CDT 2011

But in java

Upvotes: 4

Views: 1049

Answers (5)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79005

java.time

In March 2014, Java 8 introduced the modern, java.time date-time API which supplanted the error-prone legacy java.util date-time API. Any new code should use the java.time API.

Solution using modern date-time API

You can get the desired result with following steps:

  1. Use Instant#ofEpochSecond to create an instance of Instant from epoch seconds.
  2. Convert the Instant into a ZonedDateTime by applying ZoneId.of("America/Chicago").
  3. Use a DateTimeFormatter instantiated with the required pattern/format to format the resulting ZonedDateTime.

Demo:

class Main {
    private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu",
            Locale.ENGLISH);

    public static void main(String args[]) {
        long epochSeconds = 1305176400L;
        Instant instant = Instant.ofEpochSecond(epochSeconds);

        ZoneId zoneId = ZoneId.of("America/Chicago");
        ZonedDateTime zdt = instant.atZone(zoneId);
        System.out.println(zdt);

        // Representation in a custom format
        System.out.println(zdt.format(formatter));
    }
} 

Output:

2011-05-12T00:00-05:00[America/Chicago]
Thu May 12 00:00:00 CDT 2011

Online Demo

Avoid using abbreviations for time zone ID

Given below is an excerpt from the legacy TimeZone documentation:

Three-letter time zone IDs

For compatibility with JDK 1.1.x, some other three-letter time zone IDs (such as "PST", "CTT", "AST") are also supported. However, their use is deprecated because the same abbreviation is often used for multiple time zones (for example, "CST" could be U.S. "Central Standard Time" and "China Standard Time"), and the Java platform can then only recognize one of them.

So, instead of the abbreviated time zone ID, you should use the full form e.g. by using DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss VV uuuu", Locale.ENGLISH), you will get Thu May 12 00:00:00 America/Chicago 2011 as the output.

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 3

JohnnyO
JohnnyO

Reputation: 3068

Date date = new Date(msSinceEpoch);  //note: input is ms since epoch, not seconds
String s = DateFormat.getDateTimeInstance().format(date);

that will make sure the formatting is consistent for your default locale.

Upvotes: 0

Alex Gitelman
Alex Gitelman

Reputation: 24722

Look at SimpleDateFormat class. Documentation describes how to craft correct format that can be used to convert from date to String and vice versa.

Upvotes: -1

Aidanc
Aidanc

Reputation: 7011

String date = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss")
                  .format(new java.util.Date (epoch*1000));

See: http://www.epochconverter.com/

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533472

Do you mean like this?

Date d = new Date(1305176400 * 1000L);
System.out.println(d);

prints

Thu May 12 06:00:00 BST 2011

Upvotes: 0

Related Questions