1

I am fetching current date and time from GPS but it gives me date 3 days before the current date. In MyLocationListener class loc.getTime(); gives me date 3 days before the current date .Code which I am using

public class Location_gps extends Activity
{
 Button btn;
 LocationManager mlocManager;
 LocationListener mlocListener;
 Location loc;
 SimpleDateFormat sdf;
 Date d;

@Override
public void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_location_gps);

  btn = (Button)findViewById(R.id.btn);

  /* Use the LocationManager class to obtain GPS locations */
  mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

  mlocListener = new MyLocationListener();
  mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

  loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
 d = new Date(loc.getTime());


  btn.setOnClickListener(new OnClickListener()
  {
  @Override
  public void onClick(View v)
  {
   String Text = "My current location is: " +
        "\nLatitude = " + loc.getLatitude() +
        "\nLongitude = " + loc.getLongitude() +
        "\nAccuracy = " + loc.getAccuracy() +
        "\nAltitude = " + loc.getAltitude() +
        "\nProvider = " + loc.getProvider() +
        "\nTime = " + sdf.format(d); 

Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
  }
 });

}

public class MyLocationListener implements LocationListener
{

  @Override
  public void onLocationChanged(Location loc)
  {

    loc.getLatitude();
    loc.getLongitude();

    String Text = "My current location is: " +
    "\nLatitude = " + loc.getLatitude() +
    "\nLongitude = " + loc.getLongitude() +
    "\nAccuracy = " + loc.getAccuracy() +
    "\nAltitude = " + loc.getAltitude() +
    "\nTime = " + loc.getTime();

    Toast.makeText( getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onProviderDisabled(String provider)
  {
    Toast.makeText( getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
  }

  @Override
  public void onProviderEnabled(String provider)
  {
    Toast.makeText( getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onStatusChanged(String provider, int status, Bundle extras)
  {

  }
}
}

How I fetch current date and time from GPS. Please help me.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
nitin tyagi
  • 1,176
  • 1
  • 19
  • 52

3 Answers3

2

As you use "getLastKnownLocation" method, it returns you the last parameters since it received a location update.

loc = mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

If your device did not recently receive an update, it will return the last update which is 3 days past for your case. If it cannot receive gps update, check if your "gps" enabled or try it in open air.

0

This seems to be an Android Bug...Please have a look at the following:

http://www.zdnet.com/blog/hardware/the-time-displayed-on-most-android-phones-is-wrong/19387

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
-1

You should be using NTP api's instead of GPS. Check out http://code.google.com/p/ntp-sync/ and "Is Android using NTP to sync time?". Basically search for NTP on Android. NTP is the network time protocol standard across all devices, servers and networks.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148