JAVA 时间差距,两个时间相差多少天,时,分
答案:3 悬赏:0 手机版
解决时间 2021-02-26 06:20
- 提问者网友:沦陷
- 2021-02-25 20:53
JAVA 时间差距,两个时间相差多少天,时,分
最佳答案
- 五星知识达人网友:神鬼未生
- 2021-02-25 22:06
package test;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Goods {
public static void main(String[] args) {
Calendar g1 = new GregorianCalendar(2016, 1, 10, 1, 54, 30);
Calendar g2 = new GregorianCalendar(2017, 1, 10, 1, 54, 30);
long t1 = g1.getTimeInMillis();
long t2 = g2.getTimeInMillis();
long interval = t2 -t1;
long interday = interval / (24 * 60 * 60 * 1000);
interval %= 24 * 60 * 60 * 1000;
long interhour = interval / (60 * 60 * 1000);
interval %= 60 * 60 * 1000;
long interminute = interval / (60 * 1000);
interval %= 60 * 1000;
long interSecond = interval / (1000);
System.out.print("相差:"+interday +"天,");
System.out.print(interhour + "小时,");
System.out.print(interminute+"分,");
System.out.println(interSecond+"秒。");
}
}
import java.util.Calendar;
import java.util.GregorianCalendar;
public class Goods {
public static void main(String[] args) {
Calendar g1 = new GregorianCalendar(2016, 1, 10, 1, 54, 30);
Calendar g2 = new GregorianCalendar(2017, 1, 10, 1, 54, 30);
long t1 = g1.getTimeInMillis();
long t2 = g2.getTimeInMillis();
long interval = t2 -t1;
long interday = interval / (24 * 60 * 60 * 1000);
interval %= 24 * 60 * 60 * 1000;
long interhour = interval / (60 * 60 * 1000);
interval %= 60 * 60 * 1000;
long interminute = interval / (60 * 1000);
interval %= 60 * 1000;
long interSecond = interval / (1000);
System.out.print("相差:"+interday +"天,");
System.out.print(interhour + "小时,");
System.out.print(interminute+"分,");
System.out.println(interSecond+"秒。");
}
}
全部回答
- 1楼网友:胯下狙击手
- 2021-02-25 22:57
现在是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<0)
system.out.println("c1小于c2");
else
system.out.println("c1大于c2");
- 2楼网友:十鸦
- 2021-02-25 22:50
java.util.Date有方法getTime,得到是时间的毫秒值。。。。。。两个时间的毫秒值,相差、再转换成天、时、分,即可以
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯