zoukankan      html  css  js  c++  java
  • java获取两个时间的相隔时间,包括年、月、日、时、分、秒

            public static final int YEAR_RETURN = 0;  
              
            public static final int MONTH_RETURN = 1;  
              
            public static final int DAY_RETURN = 2;  
              
            public static final int HOUR_RETURN= 3;  
              
            public static final int MINUTE_RETURN = 4;  
              
            public static final int SECOND_RETURN = 5;  
              
              
            public static final String YYYY = "yyyy";  
              
            public static final String YYYYMM = "yyyy-MM";  
              
            public static final String YYYYMMDD = "yyyy-MM-dd";  
              
            public static final String YYYYMMDDHH= "yyyy-MM-dd HH";  
              
            public static final String YYYYMMDDHHMM = "yyyy-MM-dd HH:mm";  
              
            public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";  
          
          
        public static long getBetween(String beginTime, String endTime, String formatPattern, int returnPattern) throws ParseException{  
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat(formatPattern);  
                Date beginDate = simpleDateFormat.parse(beginTime);  
                Date endDate = simpleDateFormat.parse(endTime);  
                  
                Calendar beginCalendar = Calendar.getInstance();  
                Calendar endCalendar = Calendar.getInstance();  
                beginCalendar.setTime(beginDate);  
                endCalendar.setTime(endDate);  
                switch (returnPattern) {  
                case YEAR_RETURN:  
                    return GgczUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR);  
                case MONTH_RETURN:  
                    return GgczUtil.getByField(beginCalendar, endCalendar, Calendar.YEAR)*12 + GgczUtil.getByField(beginCalendar, endCalendar, Calendar.MONTH);  
                case DAY_RETURN:  
                    return GgczUtil.getTime(beginDate, endDate)/(24*60*60*1000);  
                case HOUR_RETURN:  
                    return GgczUtil.getTime(beginDate, endDate)/(60*60*1000);  
                case MINUTE_RETURN:  
                    return GgczUtil.getTime(beginDate, endDate)/(60*1000);  
                case SECOND_RETURN:  
                    return GgczUtil.getTime(beginDate, endDate)/1000;  
                default:  
                    return 0;  
                }  
            }  
          
        private static long getByField(Calendar beginCalendar, Calendar endCalendar, int calendarField){  
                return endCalendar.get(calendarField) - beginCalendar.get(calendarField);  
            }  
              
            private static long getTime(Date beginDate, Date endDate){  
                return endDate.getTime() - beginDate.getTime();  
            }  
              
            public static void main(String[] args) {  
                Date start = new Date("2008/05/00");
                Date end = new Date("2009/06/00");
                System.out.println("###start:==" + start);
                System.out.println("###end:==" + end);

                getMonth(start, end);
                System.out.println("###getMonth():=" + getMonth(start, end));
                try {  
                    System.out.println(GgczUtil.getBetween("2013-05-02", "2013-05-05", GgczUtil.YYYYMMDD, GgczUtil.MONTH_RETURN));  
                } catch (ParseException e) {  
                    e.printStackTrace();  
                }  
                  
            }

  • 相关阅读:
    require的特点
    require和load的不同之处
    关于“load”方法
    puts方法要点
    用类解释对象的由来
    以方法调用的原理解释Ruby中“puts ‘Hello‘”
    Ruby中方法的设计理念
    Ruby中puts,print,p的区别
    Ubuntu16.04安装MongoDB的Ruby驱动
    使用spring框架,用xml方式进行bean装配出现“The fully qualified name of the bean's class, except if it serves...”
  • 原文地址:https://www.cnblogs.com/ckaifeng/p/5435298.html
Copyright © 2011-2022 走看看