Unix time conversion

Hanybalo
Hanybalo used Ask the Experts™
on
Hi all

I have a unix time and would like to create a class that converts it back to ss:mm:hh  dd/mm/yyyy,

how would l do this? I might be posting this question too early, I am reading on it too, forgive me if it is a silly question.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
>>I have a unix time

Meaning?
Commented:
You probably want something like this.  I haven't checked this for errors.

You'll need to look at java.util.Date and java.format.SimpleDateFormat for specifics (like what tokens to use to produce the output format you want).

Date aDate = new Date();
aDate.setTime(numberOfSecondsSince1970);
SimpleDateFormat outgoingDateFormatter = new SimpleDateFormat("%ss:%mm:%hh %dd/%MM/%yyyy");
String formattedDateString = outgoingDateFormatter.format(yourDate);
UNIX time (time_t) is a 32-bit unsigned counter of seconds since 1-1-1970 00:00:00, because that's the start of UNIX according to Ken Thompson.
JAVA time is a 64-bit signed counter of milliseconds since the same event.
So, if you multiply your UNIX time by 1000, you can do almost anything you want: see the JAVA manuals.
Because it's signed, you can even go back in time before Ken said "go".

The SimpleDateFormat above is a strong example of what can be done.
;JOOP!
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Commented:
Good catch.   Yes, setTime needs to be specified in milliseconds, not seconds.
Top Expert 2016

Commented:
Why not

Date aDate = new Date(numberOfMillisecondsSince1970);

?

Author

Commented:
hi guys would it not be best to use calender, and also in the obove example the result is in BST, I guess this is due to it being locale dependent.
How would I change it so that is uses GMT and not rely on the local time setting.

thanks
Top Expert 2016
Commented:
>>and also in the obove example the result is in BST

That's only what you're seeing as that's the current system setting. The contents of the timestamp is absolute and therefore not affected by the current setting. Run the following for supporting evidence:

/*
 * Demonstrates (when there are offsets involved e.g. BST)
 * that Dates represent an absolute fixed offset
 * from a starting time and are not dependent on other factors
 * such as TimeZones, offsets and so on.
 */
import java.util.*;

public class DateAbsolute {

  public static void main(String[] args) {
    Calendar c = Calendar.getInstance();
    TimeZone tz = TimeZone.getTimeZone("UTC");
    GregorianCalendar cg = new GregorianCalendar(tz);
    System.out.println("Current hour (normal): " + c.get(Calendar.HOUR_OF_DAY));
    System.out.println("Current hour (UTC)   : " + cg.get(Calendar.HOUR_OF_DAY));
    System.out.println();
    System.out.println("Now we set a random date:");
    Random dateGenerator = new Random();
    long randomDate = dateGenerator.nextLong();
    //System.out.println("Random number of milliseconds is " + randomDate);
    c.setTime(new java.util.Date(randomDate));
    cg.setTime(new java.util.Date(randomDate));
    System.out.println("Current hour (normal): " + c.get(Calendar.HOUR_OF_DAY));
    System.out.println("Current hour (UTC)   : " + cg.get(Calendar.HOUR_OF_DAY));
  }

}

Author

Commented:
Thanks to all I dont know whos answer to accept,
Top Expert 2016

Commented:
:-)

Author

Commented:
can this string be converted to java time

 1999-01-06 07:14:23

?
Top Expert 2016

Commented:
What do you mean by 'Java time'?

Author

Commented:
sorry fixed that by of couse Date.getTime();
:-)'

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial