题目
java计算时间差
得到两个时间,希望您能给出最简洁的方法计算出几时几分几秒?
得到两个时间,希望您能给出最简洁的方法计算出几时几分几秒?
提问时间:2020-11-02
答案
现在是2004-03-26 13:31:40
过去是:2004-01-02 11:30:24
要获得两个日期差,差的形式为:XX天XX小时XX分XX秒
方法一:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date d1 = df.parse("2004-03-26 13:31:40");
Date d2 = df.parse("2004-01-02 11:30:24");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
}
catch (Exception e)
{
}
方法二:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = df.parse("2004-03-26 13:31:40");
java.util.Date date=df.parse("2004-01-02 11:30:24");
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+"秒");
方法三:
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date begin=dfs.parse("2004-01-02 11:30:24");
java.util.Date end = dfs.parse("2004-03-26 13:31:40");
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
long day1=between/(24*3600);
long hour1=between%(24*3600)/3600;
long minute1=between%3600/60;
long second1=between%60/60;
System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
java 比较时间大小
String s1="2008-01-25 09:12:09";
String s2="2008-01-29 09:12:11";
java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
try
{
c1.setTime(df.parse(s1));
c2.setTime(df.parse(s2));
}catch(java.text.ParseException e){
System.err.println("格式不正确");
}
int result=c1.compareTo(c2);
if(result==0)
System.out.println("c1相等c2");
else if(result
过去是:2004-01-02 11:30:24
要获得两个日期差,差的形式为:XX天XX小时XX分XX秒
方法一:
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try
{
Date d1 = df.parse("2004-03-26 13:31:40");
Date d2 = df.parse("2004-01-02 11:30:24");
long diff = d1.getTime() - d2.getTime();
long days = diff / (1000 * 60 * 60 * 24);
}
catch (Exception e)
{
}
方法二:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date now = df.parse("2004-03-26 13:31:40");
java.util.Date date=df.parse("2004-01-02 11:30:24");
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+"秒");
方法三:
SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Date begin=dfs.parse("2004-01-02 11:30:24");
java.util.Date end = dfs.parse("2004-03-26 13:31:40");
long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒
long day1=between/(24*3600);
long hour1=between%(24*3600)/3600;
long minute1=between%3600/60;
long second1=between%60/60;
System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");
java 比较时间大小
String s1="2008-01-25 09:12:09";
String s2="2008-01-29 09:12:11";
java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
java.util.Calendar c1=java.util.Calendar.getInstance();
java.util.Calendar c2=java.util.Calendar.getInstance();
try
{
c1.setTime(df.parse(s1));
c2.setTime(df.parse(s2));
}catch(java.text.ParseException e){
System.err.println("格式不正确");
}
int result=c1.compareTo(c2);
if(result==0)
System.out.println("c1相等c2");
else if(result
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
最新试题
- 1英语翻译
- 2已知x^2-3x-2=0,求代数式2x^3-6x^2-4x+1的值
- 3一列火车从某站匀速驶过,张站长在铁轨旁,从车头到达他身边到车尾驶过都看了看表,共24秒,已知火车从车头进入站台到车尾离开站台共用50秒,且站台长325米,求:火车长多少米?火车速度是每小时多少公里?
- 4有一PH为12的NaOH溶液100ml,要使它的PH降到11
- 5检测生物组织中还原糖 脂肪 和蛋白质
- 6法布尔的《昆虫记》评价
- 7peple say that the modern car was made in America.的同义句转换
- 8已知直角三角形ABC;角C=30度,角B=60度,角A=90度,斜边长BC=6KM,求AC长度
- 9固体物理习题1
- 10能的转化与能量守恒
热门考点
- 1生物怎样减少实验误差
- 2在一次“飞车过黄河”的表演中,汽车在空中飞经最高点后在对岸着地,已知汽车从最高点至着地点经历的时间
- 3F1,F2 是双曲线的焦点若双曲线右支存在P点满足|PF2|=|F1F2|
- 4英语 The more we get together ,the (h) we will be
- 5吸收二氧化碳时,为什么一般选用氢氧化钠溶液而不用澄清石灰水?
- 6I was so familiar with him that I recognized his voice _______ I picked up the phone.
- 7《我爱红杜鹃》阅读答案
- 8改英语句子.我真的不知道这些哪儿错了..
- 9已知y=f(x)的定义域为R+,且对任意的X,Y属于R,恒有f(xy)=f(x)+f(y)当X〉1时,f(x)〈0
- 10So de we和So we do区别