- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何计算时间差并以 00:00 格式显示结果。我找不到方法,如何将字符串数据发送到日期以及如何以 EditText 00:00 格式显示结果。我自己试过了,但它显示找不到源的错误。下面是代码。
public class TimeCalculate extends Activity {
private String mBlock = null;
private String mBlockoff = null;
private String mBlockon = null;
Date date1,date2;
EditText block;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText blockoff = (EditText) findViewById(R.id.blockoff);
mBlockoff = blockoff.getText().toString();
EditText blockon = (EditText) findViewById(R.id.blockon);
mBlockon = blockon.getText().toString();
block = (EditText) findViewById(R.id.block);
mBlock = getDifference(date1, date2);
date1 = new Date(mBlockoff);
date2 = new Date(mBlockon);
blockon.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
block = (EditText) findViewById(R.id.block);
mBlock = getDifference(date1, date2);
block.setText(mBlock);
}
}
public static String getDifference(Date startTime, Date endTime) {
String timeDiff;
if (startTime == null)
return "[corrupted]";
Calendar startDateTime = Calendar.getInstance();
startDateTime.setTime(startTime);
Calendar endDateTime = Calendar.getInstance();
endDateTime.setTime(endTime);
long milliseconds1 = startDateTime.getTimeInMillis();
long milliseconds2 = endDateTime.getTimeInMillis();
long diff = milliseconds2 - milliseconds1;
long hours = diff / (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
minutes = minutes - 60 * hours;
long seconds = diff / (1000);
timeDiff = hours + ":" + minutes;
return timeDiff;
}
}
我使用 SampleDateFormat 修改的代码
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm");
try {
date1 = simpleDateFormat.parse(mBlockoff);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
date2 = simpleDateFormat.parse(mBlockon);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
然后我调用了下面的方法
blockon.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
mBlock = getDifference(date1, date2);
block.setText(mBlock);
}
最佳答案
其中一种方法可以是:
计算以毫秒为单位的差异,将其转换为秒,然后使用 DateUtils.elapsedTime(int sec)
将耗时格式化为 hh:mm 格式。
请参阅 DateUtils 的文档, Formatter .
编辑:示例代码:此函数将以 hh:mm:ss
格式返回时间
String getDifference(long now, long then){
if(now > then)
return DateUtils.formatElapsedTime((now - then)/1000L);
else
return "error";
}
关于android - 如何计算时间差并将结果显示在00 :00 format?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163666/
我对 EPOCH 时间有疑问。 我需要计算两个数据包之间的时间差。我不太确定如何: printf("Epoch Time: %d:%d seconds\n", header->ts.tv_sec, h
我正在尝试了解数字旋转的速度。我有一个程序,其中计数器从 0 开始。 每次用户单击按钮“加 1”。它将计数器递增 1。 计数器最大值为 255。在 255 之后它回到 0。 现在我想显示从 0 到递增
基本上我想做的是根据 SIP 信令获取通话持续时间。 我有一个包含如下所示记录的表,我正在尝试编写一个返回以下内容的 SELECT 语句: id callid date
您好,我需要有关 MySQL 数据库查询的帮助。 我有一个看起来像这样的表: ID TRACKID DATE Name Action 38
细节。我有包含以下列的注释表。 ID - INT(3) Date - DateTime Note - VARCHAR(100) Tile - Varchar(100
午夜过后我很难计算时间: String time = "15:00-18:05"; //Calculating OK //String time = "22:00-01:05"; //Not
我使用 XML 从 MySQL 数据库中抓取了两个日期,如下所示。我想获得以秒为单位的时差。 我写了这个脚本,但它给出了“NaN” function show(){ var t1 = new Dat
如何在 MYSQL phpMyAdmin 数据库中以 (TIME 00:00:00) 格式更新另一个字段(整列)的时间差 当我在 PHP 中执行此操作时,我不断收到 0:00:00 并且没有结果。其
我的表单中有两个字段,用户可以在其中选择输入时间(开始时间、结束时间),我想在更改这些字段时重新计算另一个字段的值。 我想做的是获取 2 次之间的小时数。因此,例如,如果我的开始时间为 5:30,结束
我想使用 Date 和 Calendar 类计算 java 中两个日期/时间之间的差异。我的格式是“2012-01-24 12:30:00 PM”。 我已经实现了我自己的方法,也用谷歌搜索它以与其他人
MySQL 服务器上的访客数据库如下所示id 是主键整数类型,firstname 和 lastname 是文本类型,访客 id 是整数类型 id firstname lastname
我是一名优秀的程序员,十分优秀!