java - GPS 位置和本地时间

标签 java android time gps

我尝试使用以下公式从 Location.getTime() 获取本地时间:

long localTime = location.getTime() + Calendar.getInstance().getTimeZone().getOffset(Calendar.ZONE_OFFSET);

;

但我在不同的 Android 版本和不同的模拟器上得到不同的时间。我如何才能始终获得正确的时间?

完整代码为:

private final long TimeOffset = Calendar.getInstance().getTimeZone().getOffset(Calendar.ZONE_OFFSET);
locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
    locationlistener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            boolean wasNull = locFine == null;
            if (location.getProvider().equals(android.location.LocationManager.GPS_PROVIDER)) {
                locFine = location;
                //long TimeOffset = Calendar.getInstance().getTimeZone().getRawOffset();
                long gpsTime = locFine.getTime() + TimeOffset;
                long SystemTime = Calendar.getInstance().getTimeInMillis();
                timeOffsetGPS = gpsTime - SystemTime;
                Date dtgps = new Date(locFine.getTime());
                Log.d("Location", "Time GPS: " + dtgps); // This is what we want!
                if (context != null && a != null) {
                    if (wasNull) lib.ShowToast(a, getString(R.string.gotGPS));
                    /*
                    lib.ShowMessage(a,"gps time: " + dtgps
                            + "\nsystem time: " + new Date(SystemTime)
                            + "\noffset: " + timeOffsetGPS / 1000
                            + "\ncorrected gpstime: " + new Date(gpsTime));
                    */
                }

            }

        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {
            if (a != null) lib.ShowToast(context,  context.getString(R.string.gpsstatus) + " " + s);
        }

        @Override
        public void onProviderEnabled(String s) {
            if (context != null) lib.ShowToast(context, s + " " + getString(R.string.enabled));
        }

        @Override
        public void onProviderDisabled(String s) {
            if (context != null)
                lib.ShowMessage(context, s + " " + getString(R.string.disabled));
        }
    };
    try {
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER, 1000, 5, locationlistener);}

最佳答案

来自 Location 类的 getTime() 方法的文档,位于 https://developer.android.com/reference/android/location/Location.html :

Return the UTC time of this fix, in milliseconds since January 1, 1970.

因此,在 onLocationChanged() 方法中,您可以获得一个像这样的 Date 对象,它表示获取位置修复时的时间戳(您确实在代码中间的某一点有这个):

Date fixDateTime = new Date(location.getTime());

由于 Date 对象在内部将日期/时间存储为 UTC,因此您可以使用任何日期/时间格式化函数来显示任何相关时区的时间戳。您不需要添加或减去任何偏移量。

如果您不熟悉日期/时间格式化函数,请首先阅读 SimpleDateFormat 的文档: https://developer.android.com/reference/java/text/SimpleDateFormat.html .

关于java - GPS 位置和本地时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46696621/

相关文章:

java - 如何解决预期的 org.apache.hadoop.io.Text,在 mapreduce 作业中收到 org.apache.hadoop.io.LongWritable

java - 创建自己的 "SeekBar"

mysql - 从mysql中的时间间隔中选择时间

java - Eclipse Java 调试器不会进入 lambda 或方法引用

java - struts中的正则表达式

android - Flex 移动警报 - 本地通知

java - 当我将按钮放在 Activity 上时,Android 应用程序崩溃,空 Activity 正在工作

c++ - 将当前时间从 Windows 转换为 C 或 C++ 中的 unix 时间戳

mysql - 如何在 Redshift HH :MM:SS 中只保存时间

java - 如何使用 Intent 打开 "Add a Google Account" Activity ?