How to get the current time of UTC or GMT in millis in Android instead of getting the current date of the device

advertisements

This question already has an answer here:

  • How can I get the current date and time in UTC or GMT in Java? 28 answers

I am trying to get current UTC date time in millis. But every code I used for this returns me the device's current date time. When I chenge my device's date, time it shows me chenged one. So, I want to get GMT/UTC date time so that it will show me correct date even if user changes the date, time of his/her device.

Codes I tried:

Calendar calendar = Calendar.getInstance();
        long now = calendar.getTimeInMillis();

and

DateFormat df = DateFormat.getTimeInstance();
        df.setTimeZone(TimeZone.getTimeZone("UTC"));
        String gmtTime = df.format(new Date());
        Date gmtDate = df.parse(gmtTime);

Actually I want to set an alarm at November 15 2017, 5 PM using AlarmManager, receive that event hide some activities in my app which I don't want to show after this date, time.

How can I acheive this?

Thanks in advance!


Try TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);