精华内容
下载资源
问答
  • JAVA 计算时间差

    2019-10-05 01:28:00
    JAVA 计算时间差 比如:现在是2004-03-26 13:31:40 过去是:2004-01-02 11:30:24我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒方法一: 1 DateFormat df = new S...

    比如:现在是2004-03-26 13:31:40 
           过去是:2004-01-02 11:30:24 
    我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒 
    方法一:

     1 DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
     2 try  
     3 {   
     4     Date d1 = df.parse("2004-03-26 13:31:40");   
     5     Date d2 = df.parse("2004-01-02 11:30:24");   
     6     long diff = d1.getTime() - d2.getTime();   
     7     long days = diff / (1000 * 60 * 60 * 24);   
     8 }   
     9 catch (Exception e)   
    10 {   
    11 } 

    方法二:

    1 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
    2    java.util.Date now = df.parse("2004-03-26 13:31:40");   
    3    java.util.Date date=df.parse("2004-01-02 11:30:24");   
    4    long l=now.getTime()-date.getTime();   
    5    long day=l/(24*60*60*1000);   
    6    long hour=(l/(60*60*1000)-day*24);   
    7    long min=((l/(60*1000))-day*24*60-hour*60);   
    8    long s=(l/1000-day*24*60*60-hour*60*60-min*60);   
    9    System.out.println(""+day+"天"+hour+"小时"+min+"分"+s+"秒");

    方法三:

    1 SimpleDateFormat dfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
    2    java.util.Date begin=dfs.parse("2004-01-02 11:30:24");   
    3    java.util.Date end = dfs.parse("2004-03-26 13:31:40");   
    4    long between=(end.getTime()-begin.getTime())/1000;//除以1000是为了转换成秒   
    5    long day1=between/(24*3600);   
    6    long hour1=between%(24*3600)/3600;   
    7    long minute1=between%3600/60;   
    8    long second1=between%60/60;   
    9    System.out.println(""+day1+"天"+hour1+"小时"+minute1+"分"+second1+"秒");

    比较时间:

     1 String s1="2008-01-25 09:12:09";   
     2 String s2="2008-01-29 09:12:11";   
     3 java.text.DateFormat df=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
     4 java.util.Calendar c1=java.util.Calendar.getInstance();   
     5 java.util.Calendar c2=java.util.Calendar.getInstance();   
     6 try  
     7 {   
     8 c1.setTime(df.parse(s1));   
     9 c2.setTime(df.parse(s2));   
    10 }catch(java.text.ParseException e){   
    11 System.err.println("格式不正确");   
    12 }   
    13 int result=c1.compareTo(c2);   
    14 if(result==0)   
    15 System.out.println("c1相等c2");   
    16 else if(result<0)   
    17 System.out.println("c1小于c2");   
    18 else  
    19 System.out.println("c1大于c2"); 

     

    posted on 2017-07-05 19:54 booleen 阅读(...) 评论(...) 编辑 收藏

    转载于:https://www.cnblogs.com/booleen/p/7123198.html

    展开全文
  • java计算时间差

    2016-01-26 17:13:35
    1.java计算时间差 public static int daysBetween(String startDate,String endDate) throws ParseException{ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); Calendar cal = Calendar.getInsta

    1.java计算时间差

    public static int daysBetween(String startDate,String endDate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(startDate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(endDate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
    }



    展开全文
  • java 计算时间差

    千次阅读 2013-12-16 19:51:43
    java计算时间差的标准用法,记录下来方便以后查找 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....

    java计算时间差的标准用法,记录下来方便以后查找

    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计算时间差

    2018-09-19 15:59:56
    import java.util.*; import java.text.SimpleDateFormat; public class Main{ public static void main(String []args) { Scanner sc=new Scanner(System.in); String date1=sc.nextLine(...
    import java.util.*;
    import java.text.SimpleDateFormat;
    public class Main{
        public static void main(String []args)
        {
            Scanner sc=new Scanner(System.in);
            String date1=sc.nextLine();
            String date2=sc.nextLine();
            int year1=Integer.parseInt(date1.substring(0,4));
            int mon1=Integer.parseInt(date1.substring(4,6));
    //20110412
    //20110422
    //我的输入格式是这样的
            SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");
            try{
            Date d1=df.parse(date1);
            Date d2=df.parse(date2);
            long result=(d2.getTime()-d1.getTime())/1000/60/60/24;
    //这个可是说是函数的bug吗,1000年时它少算一天,自己手动排除啊
            if(year1%4==0&&year1%100==0&&year1%400!=0&&mon1<=2)
                    result-=1;
            System.out.println(result+1);
            }catch(Exception e)
            {
                System.out.println(e);
            }
        }
    }

     

    展开全文
  • JAVA计算时间差

    2018-12-24 15:11:30
    1.首先需要将类型转为date。 2.调用.getTime()方法进行相减,得出来的是毫秒 Long responseTimeMs = nowDate.getTime() - appStatistical....3.计算小时  Long responseHour = responseTimeMs / 1000 / 60 / ...
  • Java计算时间差、日期差总结

    万次阅读 多人点赞 2018-03-13 18:59:00
    Java计算时间差、日期差总结 最近工作中遇到需要计算时间差,搜索了几种计算时间差的方法,这里总结一下 1、java 7中的日历类Calendar Calendar类使用其静态的getInstance()方法获取一个日历实例,该实例为当前...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 1,074
精华内容 429
关键字:

java计算时间差

java 订阅