TheAnonymous010 TheAnonymous010 - 6 months ago 23
Android Question

Two Separate getTime() Calls Return Same Value at Different Times

I am making an app that saves when the user enters a location and exits. I have to separate

Date
objects for this. When I print out the values, they return the same exact time although they are saved at a different time. Any ideas what may be causing this?

My checkLocation() method:

private void checkLocation() {
float[] distance = new float[2];
for(int i = 1; i <= AssetLoader.getTotalLocations(); i++) {
float mItemLat = AssetLoader.getItemLatitude(String.valueOf(i));
float mItemLong = AssetLoader.getItemLongitude(String.valueOf(i));

Location.distanceBetween(mLastLocation.getLatitude(), mLastLocation.getLongitude(),
mItemLat, mItemLong, distance);

if(distance[0] > 55){
Toast.makeText(getBaseContext(), "Outside, distance from center: " + distance[0] + " radius: " + "55", Toast.LENGTH_LONG).show();
if(startDate != null) {
stopDate = calendar.getTime();
Log.e("STOP DATE", "SAVED");
saveTimeDifference(startDate, stopDate, String.valueOf(i));
}
} else {
Toast.makeText(getBaseContext(), "Inside, distance from center: " + distance[0] + " radius: " + "55", Toast.LENGTH_LONG).show();
if(startDate == null) {
startDate = calendar.getTime();
Log.e("START DATE", "SAVED");
}
}
}
}


Where time difference and other stuff gets calculated:

public static void saveTimeDifference(Date startDate, Date endDate, String id) {
//TODO: FIX THIS

//milliseconds
long difference = endDate.getTime() - startDate.getTime();
AssetLoader.setItemTimeDifference(id, difference);

System.out.println("startDate : " + startDate);
System.out.println("endDate : "+ endDate);
System.out.println("difference : " + difference);

long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;

long elapsedDays = difference / daysInMilli;
difference = difference % daysInMilli;

long elapsedHours = difference / hoursInMilli;
difference = difference % hoursInMilli;

long elapsedMinutes = difference / minutesInMilli;
difference = difference % minutesInMilli;

long elapsedSeconds = difference / secondsInMilli;

Log.e("saveTimeDifference()", elapsedDays + " days, " + elapsedHours + " hours, " + elapsedMinutes + " minutes, " + elapsedSeconds + " seconds");

AssetLoader.setItemDetails(AssetLoader.getItemName(String.valueOf(id)), AssetLoader.getItemDetails(AssetLoader.getItemName(String.valueOf(id))) + "\nStart Date: " + startDate + " Stop Date: " + startDate);

}


Please note that the current time is being accessed at different times. It seems to save it wrong or return the wrong time when getTime() is called on the GregorianCalendar object.

Thanks for any suggestions!

Answer

calendar.getTime() returns a java.util.Date object for the time currently stored in the Calendar object.

When a Calendar is initially obtained by calling Calendar.getInstance() (or other means), it has the "current time".

It will still have that same time 5 hours later. It is not a "live" time, so multiple calls to calendar.getTime() will get the same value.

If you want to capture the current time, just call new Date():

Date startDate = new Date();
// do work
Date stopDate = new Date();
long difference = endDate.getTime() - startDate.getTime();
System.out.println("Work took " + difference + "ms");
Comments