Search code examples
javaandroiddatedatetimeunix-timestamp

How to convert current UTC time into Linux Timestamp


I am trying to get current android device time, and convert it into UTC timezone, then i need to convert it into Unix Timestamp.

I google it, found some solutions, tried few, but nothing helping me here.

This is what i am doing now.

  Date date;
  SimpleDateFormat dateFormatGmt = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");
  dateFormatGmt.setTimeZone(TimeZone.getTimeZone("UTC"));

  SimpleDateFormat dateFormatLocal = new SimpleDateFormat("yyyy-MMM-dd HH:mm:ss");

  date=  dateFormatLocal.parse( dateFormatGmt.format(new Date()) );

  date.getTime();

Output:

  • date(Its returning the correct UTC date time) Thu Jan 26 08:06:20 GMT+05:00 2017
  • date.getTime() returns 1485399980000

When i put this Timestamp in online tools, Its not returning right output.

Kindly guide me how to convert current UTC time into UnixTimestamp


Solution

  • What you need is much simpler:

    new Date().getTime()
    

    It is alread in UTC. To get a Linux timestamp you have to divide this by 1000.