user3164420
user3164420

Reputation: 51

programmatically how to get actual time in android if i changed the device time

can Anybody please give me exact solution of this problem programmaticaly i.e. when i change the device time it's taking device time but i want that it should take actual time from internet. i am using this code...

  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String date = dateFormat.format(new Date(System.currentTimeMillis()));

i tried this also

final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    int myfrmt =(c.get(Calendar.YEAR) + c.get(Calendar.MONTH) + c.get(Calendar.DAY_OF_MONTH) + c.get(Calendar.HOUR_OF_DAY)+ c.get(Calendar.MINUTE));
    String satyam=Integer.toString(myfrmt);`

but in both the case i got device time .... not real time

Upvotes: 3

Views: 6336

Answers (3)

Chomu
Chomu

Reputation: 224

Have you tried this:

LocationManager locMan = (LocationManager) activity.getSystemService(activity.LOCATION_SERVICE);
long time = locMan.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getTime();

This will provide the time through your network provider.

Upvotes: 2

Starscream1984
Starscream1984

Reputation: 3062

I would suggest making a simple AJAX call to

http://www.timeapi.org/utc/now

A handy web service which returns current GMT time that you could manipulate.

Upvotes: 0

Robin Ellerkmann
Robin Ellerkmann

Reputation: 2113

Just have a look at this. The thread has multiple answers which are worth checking out. I would recommend the "correct" answer although you have to include a library for this. If you use the first answer please also notice the comments concerning server choice.

Hope it helps.

Upvotes: 0

Related Questions