简体   繁体   中英

Calculating the elapsed time?

I wonder how to achieve something like this:

In Activity B, user will type something and press the button back to Activity A. In Activity A, it will display few seconds ago . If the status is an hours ago, it will display one hours ago.

How to achieve ?

You could do something like this

 long startTime
 long duration

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    startTime = SystemClock.uptimeMillis();
} 




 @Override
public void onBackPressed() {
    duartion = SystemClock.uptimeMillis() - startTime;
    //Duration you get in MS
    once you are done call 
    super.onBackPressed();//finish the activity

} 

Below is a working code without using java.time.LocalDateTime You can make the necessary changes if you want to use that

You can use Shared preference to save time in activity A.

To save time:

SharedPreferences preferences = null;
preferences = act.getSharedPreferences("your_name", Activity.MODE_PRIVATE);
preferences.edit().putString("TIME", getCurrentTimeDate())
.commit();

Use this to get time:

private String getCurrentTimeDate() {

    Calendar calendar = new GregorianCalendar();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    return formatter.format(calendar.getTime());
}

Now there are 2 solutions to proceed:

  1. Set timer as service and it will keep running in background and keep updating the time

  2. Retrieve data from SharedPreferences and calculate difference every time you want to display the value.

Select the option according to the number of times you want to display this data

To calculate difference in activity B:

    Calendar calendar = new GregorianCalendar();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    SharedPreferences shared = getSharedPreferences("your_name", MODE_PRIVATE);
    String saved_time = (shared.getString("TIME", ""));

    Date date = null,date2 = null;
    try {
        date = formatter.parse(saved_time);
    } catch (ParseException e) {
        e.printStackTrace();
    }


    String currentTime = getCurrentTimeDate();
    try {
        date2 = formatter.parse(currentTime);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    printDifference(date,date2);

Methods: Use the same getCurrentTimeDate from above in this activity too

public void printDifference(Date startDate, Date endDate){

    //milliseconds
    long different = endDate.getTime() - startDate.getTime();

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

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

    //long elapsedDays = different / daysInMilli;
    //different = different % daysInMilli;

    long elapsedHours = different / hoursInMilli;
    different = different % hoursInMilli;

    long elapsedMinutes = different / minutesInMilli;
    different = different % minutesInMilli;

    long elapsedSeconds = different / secondsInMilli;

    System.out.printf(
            "%d hours, %d minutes, %d seconds%n",
            elapsedHours, elapsedMinutes, elapsedSeconds);

}

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