【问题标题】:gps Location and local timegps 位置和当地时间
【发布时间】:2017-10-11 20:04:10
【问题描述】:

我尝试使用以下公式从 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);}

【问题讨论】:

    标签: java android time gps


    【解决方案1】:

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

    返回此修复的 UTC 时间,以 1970 年 1 月 1 日以来的毫秒数为单位。

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

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

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

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

    【讨论】:

    • 我知道 location.getTime() 通常获取 UTC 时间,但我不需要格式化的日期,而是以毫秒为单位的本地时间。当然我可以再次解析格式化的日期以获取当地时间,但这不会很有效。
    • 你想用当地时间做什么?这些信息可能有助于给出更好的答案。
    • 我想计算本地时间与 gps 的时间和系统时间之间的偏移量以更正系统时间(如果需要)。
    • 您可以使用 TimeZone.getDefault().getRawOffset() 以毫秒为单位获得该偏移量,然后将该偏移量添加到 location.getTime() 的结果中。
    • 我之前使用过 RawOffset,我在我的 Android 平板电脑上得到了正确的时间,但在三星 Galaxy S3 和 S7 上却没有......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 2011-02-11
    • 2016-07-30
    相关资源
    最近更新 更多