Time conversion in java

How can I convert unix time to java?

Get the following:

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

But in java

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

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

+3
source

Do you mean this?

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

prints

Thu May 12 06:00:00 BST 2011
0
source

Take a look at the SimpleDateFormat class. The documentation describes how to create the correct format that you can use to convert from date to String and vice versa.

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

to make sure the formatting matches your default language.

0
source

All Articles