String 类型的数组里有数据如:2011-08-17 17:47:11,2011-08-17 17:47:45,2011-08-17 17:48:07,2011-08-17 17:49:35等,
后来我又把数组里的时间放到list里面,请问我如何算出list里面后一个值减去前一个值得到的数据呢?可能得到秒,也可能得到分钟!
请大侠写个代码参考参考、在线等、、、、比较急、、
JAVA关于时间相减的问题,急、、、在线等!!
答案:2 悬赏:20
解决时间 2021-11-16 23:37
- 提问者网友:失败的占卜者
- 2021-11-16 11:30
最佳答案
- 二级知识专家网友:都不是誰的誰
- 2021-11-16 12:42
现写一个,没有完全实现你的功能,可以参考
package com.ljh.date;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateFunction {
public DateFunction() {
}
public long fromDateStringToLong(String inVal) {
Date date = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss");
try {
date = inputFormat.parse(inVal);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
}
public String fromLongToDate(long inVal) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:ss");
Date currentTime = new Date(inVal);
return sdf.format(currentTime);
}
public static void main(String[] args) {
// 2005-03-28 14:50:23 - 2004-10-21 14:17:03
DateFunction df = new DateFunction();
long startT=df.fromDateStringToLong("2005-03-03 14:51:23");
long endT=df.fromDateStringToLong("2004-03-03 13:50:23");
// long fin=df.fromDateStringToLong("1970-01-01 0:00");
// System.out.println(startT+"=="+endT+"=="+(startT-endT)+"=="+fin);
long mint=(startT-endT)/(1000);
int hor=(int)mint/3600;
int secd=(int)mint%3600;
int day=(int)hor/24;
System.out.println("共"+day+"天 准确时间是:小时="+hor+" 分钟"+secd );
}
}
package com.ljh.date;
import java.util.Date;
import java.text.SimpleDateFormat;
public class DateFunction {
public DateFunction() {
}
public long fromDateStringToLong(String inVal) {
Date date = null;
SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-mm-dd hh:ss");
try {
date = inputFormat.parse(inVal);
} catch (Exception e) {
e.printStackTrace();
}
return date.getTime();
}
public String fromLongToDate(long inVal) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:ss");
Date currentTime = new Date(inVal);
return sdf.format(currentTime);
}
public static void main(String[] args) {
// 2005-03-28 14:50:23 - 2004-10-21 14:17:03
DateFunction df = new DateFunction();
long startT=df.fromDateStringToLong("2005-03-03 14:51:23");
long endT=df.fromDateStringToLong("2004-03-03 13:50:23");
// long fin=df.fromDateStringToLong("1970-01-01 0:00");
// System.out.println(startT+"=="+endT+"=="+(startT-endT)+"=="+fin);
long mint=(startT-endT)/(1000);
int hor=(int)mint/3600;
int secd=(int)mint%3600;
int day=(int)hor/24;
System.out.println("共"+day+"天 准确时间是:小时="+hor+" 分钟"+secd );
}
}
全部回答
- 1楼网友:万千宠爱
- 2021-11-16 13:26
//可以参照一下。
import java.text.parseexception;
import java.text.simpledateformat;
public class javatest {
public static void main(string[] args) throws parseexception {
// todo auto-generated method stub
simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");
java.util.date now = df.parse("2004-03-26 13:31:34");
java.util.date date=df.parse("2004-01-02 11:30:53");
long l=now.gettime()-date.gettime();
long day=l/(24*60*60*1000);
long hour=(l/(60*60*1000)-day*24);
long min=((l/(60*1000))-day*24*60-hour*60);
long s=(l/1000-day*24*60*60-hour*60*60-min*60);
system.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯