1

I am using the following code to get current time in UNIX

System.currentTimeMillis() / 1000L

But it is returning the current GMT but I need the device time in UNIX. for Example, if the time zone is GMT + 5 it should return GMT + 5 but currently it's not.

I even tried this

 Date utcDate=new Date();
 utcDate.setTime(System.currentTimeMillis());
long currentTime = utcDate.getTime();

but no luck.

All other answers on SO is returning current GMT only. I can't hard code the Time Zone as it could be anywhere around the world.

Update:

I don't need my device's timezone I need UNIX time in my TimeZone. The Mark Duplicate question is only providing the time zone and Time in Date or Calendar Formate

Thanks

Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70
  • Possible duplicate of [Get current time in a given timezone : android](https://stackoverflow.com/questions/16202956/get-current-time-in-a-given-timezone-android) – Reaper Jul 14 '17 at 10:56
  • Possible duplicate of [How to get the current date and time of your timezone in Java?](https://stackoverflow.com/questions/1305350/how-to-get-the-current-date-and-time-of-your-timezone-in-java) – Sufian Jul 14 '17 at 10:57

1 Answers1

1

So I have solved it this way

       Calendar cal = Calendar.getInstance();

       TimeZone timeZone =  cal.getTimeZone();


        Date cals =    Calendar.getInstance(TimeZone.getDefault()).getTime();

        long milliseconds =   cals.getTime();

        milliseconds = milliseconds + timeZone.getOffset(milliseconds);


       long unixTimeStamp = milliseconds / 1000L;

Hope it helped others.

Thanks

Syeda Zunaira
  • 5,191
  • 3
  • 38
  • 70