0

我有一个应用程序可以对我们的服务器进行网络调用并检索它的时间。然后它将这个时间与电话上的时间(电话的内部时间)进行比较。

有 10 分钟的容差,如果超过这个时间,用户就会被导航出应用程序。

这工作正常。我遇到的问题是该应用程序正在由不同国家的公司使用,具有不同的时区。用户被踢出应用程序。如何将本地时间功能应用于我的 DateTime 对象。我们的服务器在英国,客户在直布罗陀。提前致谢。

下面是从我们的服务器检索 DateTime 的代码

 @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                NfcScannerApplication.timeIsCorrect();
                externalTime = result;
                Log.e(TAG, "external time in post execute from service = " + externalTime);


                DateTimeFormatter df = DateTimeFormat.forPattern("dd/MM/yyyy H:mm:ss");
                DateTime externalDateTime = null;

                try{

                externalDateTime= df.parseDateTime(externalTime);


 nfcscannerapplication.setExternalTime(externalDateTime);



            String systemExternalTimeTolerance = appSharedPrefs.getString("180", null);
            Log.e(TAG, " comp opt 180 = " + systemExternalTimeTolerance);
            long toleranceInMillis = Long.parseLong(systemExternalTimeTolerance) * 1000 * 60;
            Log.e(TAG, "toleranceInMillis = " + toleranceInMillis);



            long differenceBetweenTimes = nfcscannerapplication.getInternalTime().getMillis() - 
                 nfcscannerapplication.getExternalTime().getMillis();


            Log.e(TAG, "About to make comparison1 differenceBetweenTimes = " + differenceBetweenTimes);
            if(differenceBetweenTimes > toleranceInMillis){
                NfcScannerApplication.timeIsIncorrect();
                 DateTimeFormatter df4 = DateTimeFormat.forPattern("dd-MM-yyyy H:mm");
                    String formattedExternalTime2 = df4.print(externalDateTime);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        NfcBaseActivity.this);

                    // set title
                    alertDialogBuilder.setTitle("Incorrect Phone Time ");

                    // set dialog message
                    alertDialogBuilder
                        .setMessage("Please check your phone's system time. The current date and time is "+formattedExternalTime2 +"\n" + "\n" + "Click Ok to exit app")
                        .setCancelable(false)
                        .setPositiveButton("Ok",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {
                                Intent entryActivityintent = new Intent(NfcBaseActivity.this,
                                        EntryActivity.class);
                                entryActivityintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(entryActivityintent);
                                //System.exit(0);
                            }
                          });

                        // create alert dialog
                        AlertDialog alertDialog = alertDialogBuilder.create();

                        // show it
                        if(NfcScannerApplication.isActivityVisible() == true){
                        alertDialog.show();
                        }

            }
4

1 回答 1

0
DateTimeZone ukTime = DateTimeZone.forID("Europe/London");
            DateTimeZone.setDefault(ukTime);

            DateTime ukinternalTime = new DateTime(nfcscannerapplication.getInternalTime().getMillis());
            //local time
            LocalDateTime localDateTime = ukinternalTime.
                withZone(DateTimeZone.getDefault()).toLocalDateTime();




            long differenceBetweenTimes = localDateTime.toDateTime().getMillis() - 
                 nfcscannerapplication.getExternalTime().getMillis();
于 2013-10-18T15:17:04.337 回答