简体   繁体   中英

Difference between todays Date and older Date not calculating correctly

I know this has been asked before but I can't find my exact problem.

When the user clicks a button to add a marker to a map, I am storing a true boolean tagUserLocation (to tell my Map Activity onCreate that there is a latitude and longitude stored for the users location, which needs to be added to the database before populating the map). The user then sees an activity with radio buttons to select the type of marker.

When they select the marker type and click the positive button in an Alert dialog, I am storing the number of markers the user had added and the date for the most recent one addedTagDate to my Shared Preferences. This is so I can prevent the user adding another marker for a while.

When my Map activity starts in the onCreate, it detects the tagUserLocation is true, and triggers a method to store the new marker details to the DB before creating the Map.

This is the onClick method for the Alert dialog that sets the addedTagCount and addedTagDate in millis. IT then directs the user back to the Map activity which detects there is a new marker and saves it to the DB before recreating the map.

builder.setPositiveButton(yes, new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {

        int i = sharedPreferences.getInt("addedTagCount", 0);
        Log.i(TAG, "Testing_" + "posButton 1 " + " " + i);
        if(i < 1){
            Date todaysDate = new Date();
            long millis = todaysDate.getTime();
            i++;
            sharedPreferences.edit().putInt("addedTagCount", i).commit();
            sharedPreferences.edit().putLong("addedTagDate", millis).commit();
            Log.i(TAG, "Testing_" + "posButton 2 " + " " + i);
        }

        Intent intentMapsActivity = new Intent(context, Activity_MapsActivity.class);
        context.startActivity(intentMapsActivity);
        passedActivity.finish();
        dialog.dismiss();
    }
});

When the user tries to add another marker, I am checking the stored addedTagDate against todaysDate to see if a certain amount of time has expired before allowing him to add another marker.

public boolean checkTimerForNewTag(Activity passedActivity) {
    SharedPreferences sharedPreferences = passedActivity.getSharedPreferences("com.name.myApp", Context.MODE_PRIVATE);
    //Check if timer to allow another tag has expired
    Date todaysDate = new Date();
    Date taggedDate = new Date(sharedPreferences.getLong("addedTagDate", 0));
    long taggedDateAsLong = taggedDate.getTime();
    Log.i(TAG, "checkTimerForNewTag_" + "taggedDateAsLong: " + taggedDate.getTime());

    if (taggedDateAsLong != 0) {
        long diffInMillis = todaysDate.getTime() - taggedDate.getTime();
        long sec = diffInMillis / 1000;
        long min = sec / 60;
        long hours = min / 60;

        Log.i(TAG, "checkTimerForNewTag_" + "Date Adjust: " + taggedDate.getTime() + " - " + todaysDate.getTime() + " = diffInMills: " + diffInMillis + " and sec: " + sec + " and min: " + min + " and hours: " + hours);

        if (sec >= 20) {
            Log.i(TAG, "checkTimerForNewTag_" + " hours > 1");
            sharedPreferences.edit().putInt("addedTagCount", 0).commit();
            sharedPreferences.edit().remove("addedTagDate").commit();
            return true;
        }
    }

    return false;
}

The first marker is added, and the addedTagDate stored. But when the user clicks the button a 2nd time the checkTimerForNewTag() gives a time difference which is incorrect. It is too little. Example repro steps:

Expected Outcome:

  1. User clicks button
  2. User redirected to Map activity
  3. 1st Marker added.
  4. User waits 30 seconds and clicks button again
  5. Time difference is >=20 seconds
  6. 2nd Marker Added.

Actual Outcome:

  1. User clicks button

  2. User redirected to Map activity

  3. 1st Marker added.

  4. User waits 30 seconds and clicks button again

  5. Time difference is only 81 (Or 70, 44, 126 etc.) milliseconds.

  6. User clicks button again after a few seconds

  7. Time difference is correct and if >= 20 sec a 2nd Marker is added.

Log: 1st click works fine, a marker is added and the addedTagCount is stored to SP's.

2020-03-08 17:06:12.327 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_taggedDateAsLong: 1583687115677
2020-03-08 17:06:12.327 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_Date Adjust: 1583687115677 - 1583687172327 = diffInMills: 56650 and sec: 56 and min: 0 and hours: 0

2nd click did not work, I waited 30 seconds but the diff is only 81 millis.

2020-03-08 17:06:16.134 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_taggedDateAsLong: 1583687176053
2020-03-08 17:06:16.134 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_Date Adjust: 1583687176053 - 1583687176134 = diffInMills: 81 and sec: 0 and min: 0 and hours: 0

3rd click works, I waited 2 seconds, 7 sec and 12 sec - Everything is working again.

2020-03-08 17:06:18.100 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_taggedDateAsLong: 1583687176053
2020-03-08 17:06:18.101 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_Date Adjust: 1583687176053 - 1583687178100 = diffInMills: 2047 and sec: 2 and min: 0 and hours: 0
2020-03-08 17:06:23.072 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_taggedDateAsLong: 1583687176053
2020-03-08 17:06:23.072 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_Date Adjust: 1583687176053 - 1583687183072 = diffInMills: 7019 and sec: 7 and min: 0 and hours: 0
2020-03-08 17:06:28.248 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_taggedDateAsLong: 1583687176053
2020-03-08 17:06:28.249 17912-17912/com.name.myApp I/UserTagging_: checkTimerForNewTag_Date Adjust: 1583687176053 - 1583687188248 = diffInMills: 12195 and sec: 12 and min: 0 and hours: 0

I have scoured my app to make sure I'm not doing anything untoward with the shared pref value. I'm convinced it must be a rounding problem or something similar with the Date function. Any ideas are much appreciated?

In checkTimerForNewTag you have:

sharedPreferences.edit().putInt("addedTagCount", 0).commit();

You check this in your first method:

int i = sharedPreferences.getInt("addedTagCount", 0);
    if(i < 1){
        Date todaysDate = new Date();
        long millis = todaysDate.getTime();
        i++;
        sharedPreferences.edit().putInt("addedTagCount", i).commit();
        sharedPreferences.edit().putLong("addedTagDate", millis).commit();
        Log.i(TAG, "Testing_" + "posButton 2 " + " " + i);
    }

Wouldn't i always be < 1 given you set it to 0 in checkTimerForNewTag?

FINALLY GOT IT!!!!!

I need to have an 'old date value' and a 'new date value', in addition to the immediate date value that I compare against. Whenever the time is updated I move the new one into the old slot, so I can always refer to the previous one.

Also, my old value was being called when the positive button was clicked, which is at the start of the process flow. So when my timer had expired, the value for the next click is already reached instead of starting at zero again.

Basically the below was being called at the start of the process flow, instead of the end.

sharedPreferences.edit().putLong("addedTagDate", millis).commit();

I figured it out when I put the following line into the very end of the process flow just to make my log read clearer. I realised that my timer variable was being set in the row after the last end point, rather than immediately before the next one.

Log.i(TAG, "*********************************Marker Created******************************");

At last!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM