java项目开发 系统时间处理类_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java项目开发 系统时间处理类

java项目开发 系统时间处理类

 2015/4/9 15:31:07  zyiqibook  程序员俱乐部  我要评论(0)
  • 摘要:javaDate系统时间处理类更多源码进入:在一起技术分享packagecom.xfx.bee.utils;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;/***系统时间处理类*[url=http://www.zyiqibook
  • 标签:Java 项目 开发
java Date 系统时间处理类
更多源码进入:在一起 技术分享
class="java">
    package com.xfx.bee.utils;  
       
    import java.text.ParseException;  
    import java.text.SimpleDateFormat;  
    import java.util.Calendar;  
    import java.util.Date;  
       
    /** 
     * 系统时间处理类 
     * [url=http://www.zyiqibook.com]在一起 学习交流分享网 功能源码分享[/url] 
     * 
     */  
    public class DateUtils {  
           
        private static final String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };  
       
        /** 
         * 验证时间是不是在一天之内 
         */  
        public static boolean validInOneDate(Date dates) {  
            Calendar c = Calendar.getInstance();  
            c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) - 1);  
            return c.getTime().before(dates);  
        }  
           
        /** 
         * 验证时间是不是在两天之内 
         */  
        public static boolean validInTwoDate(Date dates) {  
            Calendar c = Calendar.getInstance();  
            c.set(Calendar.DAY_OF_MONTH,c.get(Calendar.DAY_OF_MONTH) - 2);  
            return c.getTime().before(dates);  
        }  
           
        /** 
         * string 转换为 Date 
         */  
        public static Date convertStringToDate(String strDate)throws ParseException {  
           Date aDate = null;  
              
           SimpleDateFormat df;  
           df = new SimpleDateFormat("yyyy-MM-dd HH:mm");  
       
           try {  
               aDate = df.parse(strDate);  
           } catch (ParseException pe) {  
               throw new ParseException(pe.getMessage(), pe.getErrorOffset());  
           }  
              
           return aDate;  
       }  
           
        /** 
         * 页面查询条件格式化起始日期 
         */  
        public static Date formatBeginDate(Date beginDate){  
            Calendar beginCal = Calendar.getInstance();  
            beginCal.setTime(beginDate);  
            beginCal.set(Calendar.HOUR_OF_DAY, 0);  
            beginCal.set(Calendar.MINUTE, 0);  
            beginCal.set(Calendar.SECOND, 0);  
            // 查询开始时间  
            Date begin = beginCal.getTime();  
               
            return begin;  
        }  
           
        /** 
         * 页面查询条件格式化截止日期 
         */  
        public static Date formatEndDate(Date endDate){  
            Calendar endCal = Calendar.getInstance();  
            Date nowtime = new Date();  
            if(endDate==null || nowtime.before(endDate)){  
                endCal.setTime(nowtime);  
                endCal.set(Calendar.HOUR_OF_DAY, 23);  
                endCal.set(Calendar.MINUTE, 59);  
                endCal.set(Calendar.SECOND, 59);  
            }else{  
                endCal.setTime(endDate);  
                endCal.set(Calendar.HOUR_OF_DAY, 23);  
                endCal.set(Calendar.MINUTE, 59);  
                endCal.set(Calendar.SECOND, 59);  
            }  
            Date end = endCal.getTime();  
               
            return end;  
        }  
           
        /** 
         * 根据日期获得星期 
         */  
        public static String getWeekOfDate(Date date) {  
          Calendar calendar = Calendar.getInstance();  
          calendar.setTime(date);  
          int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;  
          return weekDaysName[intWeek];  
        }   
           
        /**   
         * 计算两个日期之间相差的天数   
         * @param smdate 较小的时间  
         * @param bdate  较大的时间  
         */     
        public static String daysBetween(Date smdate,Date bdate) throws ParseException{      
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
            smdate=sdf.parse(sdf.format(smdate));  
            bdate=sdf.parse(sdf.format(bdate));  
            Calendar cal = Calendar.getInstance();  
            cal.setTime(smdate);  
            long time1 = cal.getTimeInMillis();  
            cal.setTime(bdate);  
            long time2 = cal.getTimeInMillis();  
            long between_days=(time2-time1)/(1000*3600*24);  
               
           return String.valueOf(between_days);       
        }      
           
        public static void main(String[] args) throws Exception {    
            String date1="2013-06-24 10:30:30"; //    
            String date2="2013-06-26 12:30:31";   
            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
            java.util.Date start = sdf.parse(date1);    
            java.util.Date end = sdf.parse(date2);    
            System.out.println(daysBetween(start,end));  
            java.util.Date end2 = sdf.parse("2013-09-23 12:30:31");    
            System.out.println(validInTwoDate(end2));  
        }  
    }  
发表评论
用户名: 匿名