简体   繁体   English

计算经过时间?

[英]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 . 在活动B中,用户将键入一些内容,然后按一下按钮返回活动A。在活动A中,它将显示几秒钟 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 以下是不使用java.time.LocalDateTime的工作代码。如果要使用该代码,可以进行必要的更改

You can use Shared preference to save time in activity A. 您可以使用共享首选项来节省活动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: 现在有2种解决方案可以继续:

  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. 每次要显示值时,都从SharedPreferences中检索数据并计算差异。

Select the option according to the number of times you want to display this data 根据您要显示此数据的次数选择选项

To calculate difference in activity B: 要计算活动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 方法:在此活动中,也从上方使用相同的getCurrentTimeDate

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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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