在我的应用程序中有两个按钮。一个显示开始时间,第二个显示单击时的停止时间。我需要得到这两个值,计算差额,然后乘以另一个值。下面是我如何为其中一个标签设置值:
if (v == btnstart) {
Toast.makeText(getApplicationContext(), "Work Start", Toast.LENGTH_LONG).show();
Date d=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
String currentDateTimeString = sdf.format(d);
txtstart.setText(currentDateTimeString);
}
发布于 2015-05-14 02:47:38
您可以简单地使用System.currentTimeMillis()
进行计算。
if (v == btnstart) {
Toast.makeText(getApplicationContext(), "Work Start", Toast.LENGTH_LONG).show();
// Date d=new Date();
// SimpleDateFormat sdf=new SimpleDateFormat("hh:mm a");
// String currentDateTimeString = sdf.format(d);
startTime = System.currentTimeMillis();
txtstart.setText(currentDateTimeString);
} else if (v == btnDone) {
Toast.makeText(getApplicationContext(), "Work Done", Toast.LENGTH_LONG).show();
doneTime = System.currentTimeMillis();
takenTime = doneTime - startTime;
txtDone.setText(takenTime);
}
发布于 2015-05-14 02:02:07
当您单击StartTime按钮时,在一个长变量中获取使用System.currentTimeMillis();
的时间,因为时间将以毫秒为单位,同样,在StopTimer按钮上,您也可以这样做。在此之后,计算出以毫秒为单位的两次时间差。以防万一,如果您想将毫秒转换为秒,那么将值除以1000。希望这能帮到你。
发布于 2017-04-19 22:14:40
试一试
long start = System.currentTimeMillis();
long end = System.currentTimeMillis();
long diff = end - start;
DateTime dt = new DateTime(diff);
https://stackoverflow.com/questions/30233847
复制相似问题