`
jsamson
  • 浏览: 116851 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java Date应用实例集(Calendar)

阅读更多

          //取得指定月份的第一天与取得指定月份的最后一天 

            *   取得指定月份的第一天 
            * 
            *   @param   strdate   String 
            *   @return   String 
            */ 
          public   String   getMonthBegin(String   strdate) 
          ...{ 
                  java.util.Date   date   =   parseFormatDate(strdate); 
                  return   formatDateByFormat(date,"yyyy-MM")   +   "-01"; 
          } 
  
          /** *//** 
            *   取得指定月份的最后一天 
            * 
            *   @param   strdate   String 
            *   @return   String 
            */ 
          public   String   getMonthEnd(String   strdate) 
          ...{ 
                  java.util.Date   date   =   parseFormatDate(getMonthBegin(strdate)); 
                  Calendar   calendar   =   Calendar.getInstance(); 
                  calendar.setTime(date); 
                  calendar.add(Calendar.MONTH,1); 
                  calendar.add(Calendar.DAY_OF_YEAR,   -1); 
                  return   formatDate(calendar.getTime()); 
          } 
  
          /** *//** 
            *   常用的格式化日期 
            * 
            *   @param   date   Date 
            *   @return   String 
            */ 
          public   String   formatDate(java.util.Date   date) 
          ...{ 
                  return   formatDateByFormat(date,"yyyy-MM-dd"); 
          } 
  
          /** *//** 
            *   以指定的格式来格式化日期 
            * 
            *   @param   date   Date 
            *   @param   format   String 
            *   @return   String 
            */ 
          public   String   formatDateByFormat(java.util.Date   date,String   format) 
          ...{ 
                  String   result   =   ""; 
                  if(date   !=   null) 
                  ...{ 
                          try 
                          ...{ 
                                  SimpleDateFormat   sdf   =   new   SimpleDateFormat(format); 
                                  result   =   sdf.format(date); 
                          } 
                          catch(Exception   ex) 
                          ...{ 
                                  LOGGER.info("date:"   +   date); 
                                  ex.printStackTrace(); 
                          } 
                  } 
                  return   result;   package   com.app.util;  
   
  /** *//**  
    *   日期操作  
    *    
    *   @author   xxx  
    *   @version   2.0   jdk1.4.0   tomcat5.1.0   *   Updated   Date:2005/03/10  
    */  
  public   class   DateUtil   ...{  
  /** *//**  
    *   格式化日期  
    *    
    *   @param   dateStr  
    *                         字符型日期  
    *   @param   format  
    *                         格式  
    *   @return   返回日期  
    */  
  public   static   java.util.Date   parseDate(String   dateStr,   String   format)   ...{  
  java.util.Date   date   =   null;  
  try   ...{  
  java.text.DateFormat   df   =   new   java.text.SimpleDateFormat(format);  
  String   dt=Normal.parse(dateStr).replaceAll(  
  "-",   "/");  
  if((!dt.equals(""))&&(dt.length()<format.length()))...{  
  dt+=format.substring(dt.length()).replaceAll("[YyMmDdHhSs]","0");  
  }  
  date   =   (java.util.Date)   df.parse(dt);  
  }   catch   (Exception   e)   ...{  
  }  
  return   date;  
  }  
   
  public   static   java.util.Date   parseDate(String   dateStr)   ...{  
  return   parseDate(dateStr,   "yyyy/MM/dd");  
  }  
   
  public   static   java.util.Date   parseDate(java.sql.Date   date)   ...{  
  return   date;  
  }  
   
  public   static   java.sql.Date   parseSqlDate(java.util.Date   date)   ...{  
  if   (date   !=   null)  
  return   new   java.sql.Date(date.getTime());  
  else  
  return   null;  
  }  
   
  public   static   java.sql.Date   parseSqlDate(String   dateStr,   String   format)   ...{  
  java.util.Date   date   =   parseDate(dateStr,   format);  
  return   parseSqlDate(date);  
  }  
   
  public   static   java.sql.Date   parseSqlDate(String   dateStr)   ...{  
  return   parseSqlDate(dateStr,   "yyyy/MM/dd");  
  }  
   
   
  public   static   java.sql.Timestamp   parseTimestamp(String   dateStr,  
  String   format)   ...{  
  java.util.Date   date   =   parseDate(dateStr,   format);  
  if   (date   !=   null)   ...{  
  long   t   =   date.getTime();  
  return   new   java.sql.Timestamp(t);  
  }   else  
  return   null;  
  }  
   
  public   static   java.sql.Timestamp   parseTimestamp(String   dateStr)   ...{  
  return   parseTimestamp(dateStr,   "yyyy/MM/dd   HH:mm:ss");  
  }  
   
  /** *//**  
    *   格式化输出日期  
    *    
    *   @param   date  
    *                         日期  
    *   @param   format  
    *                         格式  
    *   @return   返回字符型日期  
    */  
  public   static   String   format(java.util.Date   date,   String   format)   ...{  
  String   result   =   "";  
  try   ...{  
  if   (date   !=   null)   ...{  
  java.text.DateFormat   df   =   new   java.text.SimpleDateFormat(format);  
  result   =   df.format(date);  
  }  
  }   catch   (Exception   e)   ...{  
  }  
  return   result;  
  }  
   
  public   static   String   format(java.util.Date   date)   ...{  
  return   format(date,   "yyyy/MM/dd");  
  }  
   
  /** *//**  
    *   返回年份  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回年份  
    */  
  public   static   int   getYear(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.get(java.util.Calendar.YEAR);  
  }  
   
  /** *//**  
    *   返回月份  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回月份  
    */  
  public   static   int   getMonth(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.get(java.util.Calendar.MONTH)   +   1;  
  }  
   
  /** *//**  
    *   返回日份  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回日份  
    */  
  public   static   int   getDay(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.get(java.util.Calendar.DAY_OF_MONTH);  
  }  
   
  /** *//**  
    *   返回小时  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回小时  
    */  
  public   static   int   getHour(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.get(java.util.Calendar.HOUR_OF_DAY);  
  }  
   
  /** *//**  
    *   返回分钟  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回分钟  
    */  
  public   static   int   getMinute(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.get(java.util.Calendar.MINUTE);  
  }  
   
  /** *//**  
    *   返回秒钟  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回秒钟  
    */  
  public   static   int   getSecond(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.get(java.util.Calendar.SECOND);  
  }  
   
  /** *//**  
    *   返回毫秒  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回毫秒  
    */  
  public   static   long   getMillis(java.util.Date   date)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTime(date);  
  return   c.getTimeInMillis();  
  }  
   
  /** *//**  
    *   返回字符型日期  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回字符型日期  
    */  
  public   static   String   getDate(java.util.Date   date)   ...{  
  return   format(date,   "yyyy/MM/dd");  
  }  
   
  /** *//**  
    *   返回字符型时间  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回字符型时间  
    */  
  public   static   String   getTime(java.util.Date   date)   ...{  
  return   format(date,   "HH:mm:ss");  
  }  
   
  /** *//**  
    *   返回字符型日期时间  
    *    
    *   @param   date  
    *                         日期  
    *   @return   返回字符型日期时间  
    */  
  public   static   String   getDateTime(java.util.Date   date)   ...{  
  return   format(date,   "yyyy/MM/dd   HH:mm:ss");  
  }  
   
  /** *//**  
    *   日期相加  
    *    
    *   @param   date  
    *                         日期  
    *   @param   day  
    *                         天数  
    *   @return   返回相加后的日期  
    */  
  public   static   java.util.Date   addDate(java.util.Date   date,   int   day)   ...{  
  java.util.Calendar   c   =   java.util.Calendar.getInstance();  
  c.setTimeInMillis(getMillis(date)   +   ((long)   day)   *   24   *   3600   *   1000);  
  return   c.getTime();  
  }  
   
  /** *//**  
    *   日期相减  
    *    
    *   @param   date  
    *                         日期  
    *   @param   date1  
    *                         日期  
    *   @return   返回相减后的日期  
    */  
  public   static   int   diffDate(java.util.Date   date,   java.util.Date   date1)   ...{  
  return   (int)   ((getMillis(date)   -   getMillis(date1))   /   (24   *   3600   *   1000));  
  }  
  }class CalendaUseing ...{
//日历类的应用
Calendar   now   =   Calendar.getInstance();  
  int   year   =   now.get(Calendar.YEAR);  
  int   date   =   now.get(Calendar.DAY_OF_MONTH);  
  int   month   =   now.get(Calendar.MONTH)   +   1;  
  int   hour   =   now.get(Calendar.HOUR);  
  int   min   =   now.get(Calendar.MINUTE);  
  int   sec   =   now.get(Calendar.SECOND);
}
  package   com.infoearth;  
   
  import   java.sql.Timestamp;  
  import   java.text.SimpleDateFormat;  
  import   java.util.*;  
  public   class   ManageWeek   ...{  
    //判断两个日期是否在同一周  
    boolean   isSameWeekDates(Date   date1,   Date   date2)   ...{  
      Calendar   cal1   =   Calendar.getInstance();  
      Calendar   cal2   =   Calendar.getInstance();  
      cal1.setTime(date1);  
      cal2.setTime(date2);  
      int   subYear   =   cal1.get(Calendar.YEAR)   -   cal2.get(Calendar.YEAR);    import   java.text.DateFormat;  
  import   java.util.*;  
   
  public   class   JspCalendar   ...{  
          Calendar     calendar   =   null;  
   
          public   JspCalendar()   ...{  
  calendar   =   Calendar.getInstance();  
  Date   trialTime   =   new   Date();  
  calendar.setTime(trialTime);  
          }  
   
          public   int   getYear()   ...{  
  return   calendar.get(Calendar.YEAR);  
          }  
           
          public   String   getMonth()   ...{  
  int   m   =   getMonthInt();  
  String[]   months   =   new   String   []   ...{   "January",   "February",   "March",  
  "April",   "May",   "June",  
  "July",   "August",   "September",  
  "October",   "November",   "December"   };  
  if   (m   >   12)  
          return   "Unknown   to   Man";  
   
  return   months[m   -   1];  
   
          }  
   
          public   String   getDay()   ...{  
  int   x   =   getDayOfWeek();  
  String[]   days   =   new   String[]   ...{"Sunday",   "Monday",   "Tuesday",   "Wednesday",    
              "Thursday",   "Friday",   "Saturday"};  
   
  if   (x   >   7)  
          return   "Unknown   to   Man";  
   
  return   days[x   -   1];  
   
          }  
           
          public   int   getMonthInt()   ...{  
  return   1   +   calendar.get(Calendar.MONTH);  
          }  
   
          public   String   getDate()   ...{  
  return   getMonthInt()   +   "/"   +   getDayOfMonth()   +   "/"   +     getYear();  
   
          }  
   
          public   String   getTime()   ...{  
  return   getHour()   +   ":"   +   getMinute()   +   ":"   +   getSecond();  
          }  
   
          public   int   getDayOfMonth()   ...{  
  return   calendar.get(Calendar.DAY_OF_MONTH);  
          }  
   
          public   int   getDayOfYear()   ...{  
  return   calendar.get(Calendar.DAY_OF_YEAR);  
          }  
   
          public   int   getWeekOfYear()   ...{  
  return   calendar.get(Calendar.WEEK_OF_YEAR);  
          }  
   
          public   int   getWeekOfMonth()   ...{  
  return   calendar.get(Calendar.WEEK_OF_MONTH);  
          }  
   
          public   int   getDayOfWeek()   ...{  
  return   calendar.get(Calendar.DAY_OF_WEEK);  
          }  
             
          public   int   getHour()   ...{  
  return   calendar.get(Calendar.HOUR_OF_DAY);  
          }  
           
          public   int   getMinute()   ...{  
  return   calendar.get(Calendar.MINUTE);  
          }  
   
   
          public   int   getSecond()   ...{  
  return   calendar.get(Calendar.SECOND);  
          }  
   
          public   static   void   main(String   args[])   ...{  
  JspCalendar   db   =   new   JspCalendar();  
  p("date:   "   +   db.getDayOfMonth());  
  p("year:   "   +   db.getYear());  
  p("month:   "   +   db.getMonth());  
  p("time:   "   +   db.getTime());  
  p("date:   "   +   db.getDate());  
  p("Day:   "   +   db.getDay());  
  p("DayOfYear:   "   +   db.getDayOfYear());  
  p("WeekOfYear:   "   +   db.getWeekOfYear());  
  p("era:   "   +   db.getEra());  
  p("ampm:   "   +   db.getAMPM());  
  p("DST:   "   +   db.getDSTOffset());  
  p("ZONE   Offset:   "   +   db.getZoneOffset());  
  p("TIMEZONE:   "   +   db.getUSTimeZone());  
          }  
   
          private   static   void   p(String   x)   ...{  
  System.out.println(x);  
          }  
   
   
          public   int   getEra()   ...{  
  return   calendar.get(Calendar.ERA);  
          }  
   
          public   String   getUSTimeZone()   ...{  
  String[]   zones   =   new   String[]   ...{"Hawaii",   "Alaskan",   "Pacific",  
                "Mountain",   "Central",   "Eastern"};  
   
  return   zones[10   +   getZoneOffset()];  
          }  
   
          public   int   getZoneOffset()   ...{  
  return   calendar.get(Calendar.ZONE_OFFSET)/(60*60*1000);  
          }  
   
   
          public   int   getDSTOffset()   ...{  
  return   calendar.get(Calendar.DST_OFFSET)/(60*60*1000);  
          }  
   
           
          public   int   getAMPM()   ...{  
  return   calendar.get(Calendar.AM_PM);  
          }  
  }
public   static   Calendar   getDateFromIDCard(String   IDCardNum)...{  
  int   year,   month,   day,   idLength   =   IDCardNum.length();  
          Calendar   cal   =   Calendar.getInstance();  
   
          if(idLength   ==   18)...{  
          year   =   Integer.parseInt(IDCardNum.substring(6,10));  
          month   =   Integer.parseInt(IDCardNum.substring(10,12));  
          day   =   Integer.parseInt(IDCardNum.substring(12,14));  
          }  
          else   if(idLength   ==   15)...{  
          year   =   Integer.parseInt(IDCardNum.substring(6,8))   +   1900;  
          month   =   Integer.parseInt(IDCardNum.substring(8,10));  
          day   =   Integer.parseInt(IDCardNum.substring(10,12));  
          }  
          else   ...{  
          return   null;  
          }  
          cal.set(year,   month,   day);  
          return   cal;  
  }  
   
          public   static   int   getWorkDay(Date   d1,   Date   d2)...{  
          int[]   freeDays   =   ...{0,   6};//default:   Sunday   and   Saturday   are   the   free   days.  
          return   getWorkDay(d1,   d2,   freeDays);  
          }  
           
          public   static   int   getFreeDay(Date   date,   int   dNum)...{  
          int[]   freeDays   =   ...{0,   6};//default:   Sunday   and   Saturday   are   the   free   days.  
          return   getFreeDay(date,   dNum,   freeDays);  
          }  
           
          public   static   int   getWorkDay(Date   d1,   Date   d2,   int[]   freeDays)...{  
          int   dNum   =   0;  
          dNum   =   (int)   ((d2.getTime()   -   d1.getTime())   /   1000   /   60   /   60   /24)   +   1;  
           
          return   dNum   -   getFreeDay(d1,   dNum,   freeDays);  
          }  
           
          public   static   int   getFreeDay(Date   date,   int   dNum,   int[]   freeDays)...{  
          Calendar   cal   =   Calendar.getInstance();  
          cal.setTime(date);  
          int   start   =   cal.get(Calendar.DAY_OF_WEEK)   -   1;  
          int   freeNum   =   0;  
          for(int   i   =   0;   i   <   dNum;   i++)...{  
          for(int   j   =   0;   j   <   freeDays.length;   j++)...{  
                  if((start   +   i)   %   7   ==   freeDays[j])...{  
                  freeNum++;  
                  }  
          }  
          }  
          return   freeNum;  
          }  
           
  /** *//**  
    *   日期相加  
    *    
    *   @param   date  
    *                         日期  
    *   @param   day  
    *                         天数  
    *   @return   返回相加后的日期  
    */  
  public   static   Date   changeDay(Date   date,   int   offset)...{  
    Calendar   calendar   =   Calendar.getInstance();  
    calendar.setTime(date);  
    calendar.set(Calendar.DAY_OF_YEAR,(calendar.get(Calendar.DAY_OF_YEAR)   +   offset));  
    return   calendar.getTime();  
  }  
   
  public   static   Calendar   changeDay(Calendar   calendar,   int   offset)...{  
    calendar.set(Calendar.DAY_OF_YEAR,(calendar.get(Calendar.DAY_OF_YEAR)   +   offset));  
    return   calendar;  
  }  
   
    //判断两个日期是否在同一周  
    static   boolean   isSameWeekDates(Date   date1,   Date   date2)   ...{  
    long   diff   =   getMonday(date1).getTime()   -   getMonday(date2).getTime();  
    if(Math.abs(diff)   <   1000   *   60   *   60   *   24)...{  
    return   true;  
    }  
    else...{  
    return   false;  
    }  
    }  
   
  //获得周一的日期  
  public   static   Date   getMonday(Date   date)...{  
  Calendar   c   =   Calendar.getInstance();  
  c.setTime(date);  
  c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);  
  return   c.getTime();  
  }  
   
  /** *//**  
    *   日期相减  
    *    
    *   @param   date  
    *                         日期  
    *   @param   date1  
    *                         日期  
    *   @return   返回相减后的日期  
    */  
  public   static   int   getDiffDate(java.util.Date   date,   java.util.Date   date1)   ...{  
  return   (int)   ((date.getTime()   -   date1.getTime())   /   (24   *   3600   *   1000));  
  }  
   
  public   static   int   getDiffDate(Calendar   date,   Calendar   date1)   ...{  
  return   getDiffDate(date.getTime(),   date1.getTime());  
  }  
   
  /** *//**  
    *   格式化日期  
    *    
    *   @param   dateStr  
    *                         字符型日期  
    *   @param   formatStr  
    *                         格式  
    *   @return   返回日期  
    */  
  public   static   java.util.Date   parseDate(String   dateStr,   String   formatStr)   ...{  
  SimpleDateFormat   format   =   new   SimpleDateFormat(formatStr);  
  try   ...{  
  return   format.parse(dateStr);  
  }   catch   (ParseException   e)   ...{  
  e.printStackTrace();  
  return   null;  
  }  
  }  
   
      public   static   boolean   isLeapYear(int   year)...{  
      Calendar   calendar   =   Calendar.getInstance();  
                  calendar.set(year,   2,   1);  
                  calendar.add(Calendar.DATE,   -1);  
                  if   (calendar.get(Calendar.DAY_OF_MONTH)   ==   29)   ...{  
                          System.out.println(year   +   "   year   is   a   leap   year.");  
                          return   true;  
                  }   else   ...{  
                          System.out.println(year   +   "   year   is   not   a   leap   year.");  
                          return   false;  
                  }  
      }  

      if   (0   ==   subYear)   ...{  
          if   (cal1.get(Calendar.WEEK_OF_YEAR)   ==   cal2.get(Calendar.WEEK_OF_YEAR))  
        return   true;  
      }  
      else   if   (1   ==   subYear   &&   11   ==   cal2.get(Calendar.MONTH))   ...{  
          //   如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周  
          if   (cal1.get(Calendar.WEEK_OF_YEAR)   ==   cal2.get(Calendar.WEEK_OF_YEAR))  
        return   true;  
      }  
      else   if   (-1   ==   subYear   &&   11   ==   cal1.get(Calendar.MONTH))   ...{  
          if   (cal1.get(Calendar.WEEK_OF_YEAR)   ==   cal2.get(Calendar.WEEK_OF_YEAR))  
        return   true;  
      }  
      return   false;  
    }  
     
     
    //产生周序列  
    public   static   String     getSeqWeek()...{  
      Calendar   c   =   Calendar.getInstance(Locale.CHINA);  
      String   week   =   Integer.toString(c.get(Calendar.WEEK_OF_YEAR));  
      if(week.length()==1)week   =   "0"   +   week;  
      String   year   =   Integer.toString(c.get(Calendar.YEAR));      
      return   year+week;  
       
    }  
       
      //获得周一的日期  
      public   static   String   getMonday(Date   date)...{  
        Calendar   c   =   Calendar.getInstance();  
        c.setTime(date);  
        c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);  
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());  
      }  
       
       
      //获得周五的日期  
      public   static   String   getFriday(Date   date)...{  
        Calendar   c   =   Calendar.getInstance();  
        c.setTime(date);  
        c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);        
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());  
      }  
      public   static   void   main(String[]   args)...{  
      }  
  }  
/**//*上面的都是jdk中的类,给一个Jakarta   Commons   Lang的 
  
  在Java中处理日期和时间是一件相当棘手的事。如果要使用java.text.SimpleDateFormat、 java.util.Calendar、java.util.Date等类,需要一定时间来适应,还需要对每一个涉及到的类和接口非常了解,才能顺利地处理日期和时间。   
    Lang组件彻底地简化了日期的处理,并可对其进行格式化。您可以轻松地格式化日期以进行显示、比较日期、舍入或截断日期,甚至能获取特定范围内的所有日期。     */
  
  //清单4.   处理日期和时间 
  public   static   void   main(String[]   args)   throws   InterruptedException,   ParseException   ...{ 
          //date1   created 
          Date   date1=   new   Date(); 
          //Print   the   date   and   time   at   this   instant 
          System.out.println("The   time   right   now   is   >>"+date1); 
  
          //Thread   sleep   for   1000   ms 
          Thread.currentThread().sleep(DateUtils.MILLIS_IN_SECOND); 
  
          //date2   created. 
          Date   date2=   new   Date(); 
  
          //Check   if   date1   and   date2   have   the   same   day 
          System.out.println("Is   Same   Day   >>   " 
                  +   DateUtils.isSameDay(date1,   date2)); 
  
          //Check   if   date1   and   date2   have   the   same   instance 
          System.out.println("Is   Same   Instant   >>   " 
                  +DateUtils.isSameInstant(date1,   date2)); 
  
          //Round   the   hour 
          System.out.println("Date   after   rounding   >>" 
                  +DateUtils.round(date1,   Calendar.HOUR)); 
  
          //Truncate   the   hour 
          System.out.println("Date   after   truncation   >>" 
                  +DateUtils.truncate(date1,   Calendar.HOUR)); 
  
          //Three   dates   in   three   different   formats 
          String   []   dates=...{"2005.03.24   11:03:26",   "2005-03-24   11:03",   "2005/03/24"}; 
  
          //Iterate   over   dates   and   parse   strings   to   java.util.Date   objects 
          for(int   i=0;   i   <   dates.length;   i++)...{ 
                  Date   parsedDate=   DateUtils.parseDate(dates[i], 
                  new   String   []...{"yyyy/MM/dd",   "yyyy.MM.dd   HH:mm:ss",   "yyyy-MM-dd   HH:mm"}); 
  
                  System.out.println("Parsed   Date   is   >>"+parsedDate); 
          } 
  
          //Display   date   in   HH:mm:ss   format 
          System.out.println("Now   >>" 
                  +DateFormatUtils.ISO_TIME_NO_T_FORMAT.format(System.currentTimeMillis())); 
  }  
/**//*清单4演示了org.apache.commons.lang.DateUtils和 org.apache.commons.lang.DateFormatStringUtils类的部分功能。还有其他许多方法可以进行同样的操作,但输入格式不同。故而,如果万一您必须分析和格式化一个日期值,只需要借助提供的方法之一,利用一行代码就可以实现了。   
    执行清单4中代码后的输入如清单5所示。*/
  
  //清单5.   输出 
  The   time   right   now   is   >>Sat   Apr   09   14:40:41   GMT+05:30   2005 
  Is   Same   Day   >>   true 
  Is   Same   Instant   >>   false 
  Date   after   rounding   >>Sat   Apr   09   15:00:00   GMT+05:30   2005 
  Date   after   truncation   >>Sat   Apr   09   14:00:00   GMT+05:30   2005 
  Parsed   Date   is   >>Thu   Mar   24   11:03:26   GMT+05:30   2005 
  Parsed   Date   is   >>Thu   Mar   24   11:03:00   GMT+05:30   2005 
  Parsed   Date   is   >>Thu   Mar   24   00:00:00   GMT+05:30   2005 
  Now   >>14:40:43 
    /**//*在清单4中,我们创建了两个日期,这两个日期仅有一秒的差别。然后使用isSameInstant()和isSameDay()方法检查这两个日期是否相同。接下来将日期进行舍入和截断,然后使用在数组中指定的各种格式对特殊日期用例进行解析。 
    将您的应用程序集成到第三方应用程序时,经常不能完全确定输入的格式。我曾经做过一个对旧版应用程序的集成,对于每个问题,该应用程序似乎总是有三个答案。所以,如果必须对此类应用程序提供的日期进行解析,您需要提供三个和四个不同的日期格式。清单4中使用parseDate()方法就是为了完成这项任务。这样,即使输入有变化,仍然能更对日期进行解析。还要注意,数组内模式的顺序与输入的顺序并不相同,但该方法仍然找到了适当的模式,并据此进行解析。 
    最后,我们按照ISO_TIME_NO_T_FORMAT格式(HH:mm:ss)对日期进行格式化并打印输入。*/

Java Date应用实例集2

public   static   Calendar   getDateFromIDCard(String   IDCardNum)...{  
  int   year,   month,   day,   idLength   =   IDCardNum.length();  
          Calendar   cal   =   Calendar.getInstance();  
   
          if(idLength   ==   18)...{  
          year   =   Integer.parseInt(IDCardNum.substring(6,10));  
          month   =   Integer.parseInt(IDCardNum.substring(10,12));  
          day   =   Integer.parseInt(IDCardNum.substring(12,14));  
          }  
          else   if(idLength   ==   15)...{  
          year   =   Integer.parseInt(IDCardNum.substring(6,8))   +   1900;  
          month   =   Integer.parseInt(IDCardNum.substring(8,10));  
          day   =   Integer.parseInt(IDCardNum.substring(10,12));  
          }  
          else   ...{  
          return   null;  
          }  
          cal.set(year,   month,   day);  
          return   cal;  
  }  
   
          public   static   int   getWorkDay(Date   d1,   Date   d2)...{  
          int[]   freeDays   =   ...{0,   6};//default:   Sunday   and   Saturday   are   the   free   days.  
          return   getWorkDay(d1,   d2,   freeDays);  
          }  
           
          public   static   int   getFreeDay(Date   date,   int   dNum)...{  
          int[]   freeDays   =   ...{0,   6};//default:   Sunday   and   Saturday   are   the   free   days.  
          return   getFreeDay(date,   dNum,   freeDays);  
          }  
           
          public   static   int   getWorkDay(Date   d1,   Date   d2,   int[]   freeDays)...{  
          int   dNum   =   0;  
          dNum   =   (int)   ((d2.getTime()   -   d1.getTime())   /   1000   /   60   /   60   /24)   +   1;  
           
          return   dNum   -   getFreeDay(d1,   dNum,   freeDays);  
          }  
           
          public   static   int   getFreeDay(Date   date,   int   dNum,   int[]   freeDays)...{  
          Calendar   cal   =   Calendar.getInstance();  
          cal.setTime(date);  
          int   start   =   cal.get(Calendar.DAY_OF_WEEK)   -   1;  
          int   freeNum   =   0;  
          for(int   i   =   0;   i   <   dNum;   i++)...{  
          for(int   j   =   0;   j   <   freeDays.length;   j++)...{  
                  if((start   +   i)   %   7   ==   freeDays[j])...{  
                  freeNum++;  
                  }  
          }  
          }  
          return   freeNum;  
          }  
           
  /** *//**  
    *   日期相加  
    *    
    *   @param   date  
    *                         日期  
    *   @param   day  
    *                         天数  
    *   @return   返回相加后的日期  
    */  
  public   static   Date   changeDay(Date   date,   int   offset)...{  
    Calendar   calendar   =   Calendar.getInstance();  
    calendar.setTime(date);  
    calendar.set(Calendar.DAY_OF_YEAR,(calendar.get(Calendar.DAY_OF_YEAR)   +   offset));  
    return   calendar.getTime();  
  }  
   
  public   static   Calendar   changeDay(Calendar   calendar,   int   offset)...{  
    calendar.set(Calendar.DAY_OF_YEAR,(calendar.get(Calendar.DAY_OF_YEAR)   +   offset));  
    return   calendar;  
  }  
   
    //判断两个日期是否在同一周  
    static   boolean   isSameWeekDates(Date   date1,   Date   date2)   ...{  
    long   diff   =   getMonday(date1).getTime()   -   getMonday(date2).getTime();  
    if(Math.abs(diff)   <   1000   *   60   *   60   *   24)...{  
    return   true;  
    }  
    else...{  
    return   false;  
    }  
    }  
   
  //获得周一的日期  
  public   static   Date   getMonday(Date   date)...{  
  Calendar   c   =   Calendar.getInstance();  
  c.setTime(date);  
  c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);  
  return   c.getTime();  
  }  
   
  /** *//**  
    *   日期相减  
    *    
    *   @param   date  
    *                         日期  
    *   @param   date1  
    *                         日期  
    *   @return   返回相减后的日期  
    */  
  public   static   int   getDiffDate(java.util.Date   date,   java.util.Date   date1)   ...{  
  return   (int)   ((date.getTime()   -   date1.getTime())   /   (24   *   3600   *   1000));  
  }  
   
  public   static   int   getDiffDate(Calendar   date,   Calendar   date1)   ...{  
  return   getDiffDate(date.getTime(),   date1.getTime());  
  }  
   
  /** *//**  
    *   格式化日期  
    *    
    *   @param   dateStr  
    *                         字符型日期  
    *   @param   formatStr  
    *                         格式  
    *   @return   返回日期  
    */  
  public   static   java.util.Date   parseDate(String   dateStr,   String   formatStr)   ...{  
  SimpleDateFormat   format   =   new   SimpleDateFormat(formatStr);  
  try   ...{  
  return   format.parse(dateStr);  
  }   catch   (ParseException   e)   ...{  
  e.printStackTrace();  
  return   null;  
  }  
  }  
   
      public   static   boolean   isLeapYear(int   year)...{  
      Calendar   calendar   =   Calendar.getInstance();  
                  calendar.set(year,   2,   1);  
                  calendar.add(Calendar.DATE,   -1);  
                  if   (calendar.get(Calendar.DAY_OF_MONTH)   ==   29)   ...{  
                          System.out.println(year   +   "   year   is   a   leap   year.");  
                          return   true;  
                  }   else   ...{  
                          System.out.println(year   +   "   year   is   not   a   leap   year.");  
                          return   false;  
                  }  
      }  
  package com.util;

import java.io.Serializable;
import java.sql.Date;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.sql.Time;

/** *//**
* 日期时间类
*
* @see     参考类1
*/

public class SitDateTime implements Serializable...{
    /** *//**程序逻辑错误(7113)-非法的日**/
    public static final int leInvalidDay = 7113;
    /** *//**程序逻辑错误(7112)-非法的月份**/
    public static final int leInvalidMonth = 7112;
    /** *//**程序逻辑错误(7114)-每周的第一天不是星期一**/
    public static final int leMondayNotFirstDayOfWeek = 7114;
    /** *//**程序逻辑错误(7115)-日期小于系统允许的最小日期**/
    public static final int leLessThanMinimumDate = 7115;
    /** *//**程序逻辑错误(7116)-日期大于系统允许的最大日期**/
    public static final int leMoreThanMaximumDate = 7116;
    /** *//**程序逻辑错误(7115)-时间小于系统允许的最小时间**/
    public static final int leLessThanMinimumTime = 7117;
    /** *//**程序逻辑错误(7116)-时间大于系统允许的最大时间**/
    public static final int leMoreThanMaximumTime = 7118;
    /** *//**最大日期**/
    public static final int maxDate = 29991231;
    /** *//**最小日期**/
    public static final int minDate = 18991231;
    /** *//**空日期(使用最小日期)-字符串型**/
    public static final String strDateNull = "18991231";
    /** *//**SQL空日期(使用最小日期, 已有单引号)-字符串型**/
    public static final String strSqlDateNull = "'18991231'";
    /** *//**月份-1月份**/
    public static final int monthJanuary = 1;
    /** *//**月份-2月份**/
    public static final int monthFebruary = 2;
    /** *//**月份-3月份**/
    public static final int monthMarch = 3;
    /** *//**月份-4月份**/
    public static final int monthApril = 4;
    /** *//**月份-5月份**/
    public static final int monthMay = 5;
    /** *//**月份-6月份**/
    public static final int monthJune = 6;
    /** *//**月份-7月份**/
    public static final int monthJuly = 7;
    /** *//**月份-8月份**/
    public static final int monthAugust = 8;
    /** *//**月份-9月份**/
    public static final int monthSeptember = 9;
    /** *//**月份-10月份**/
    public static final int monthOctober = 10;
    /** *//**月份-11月份**/
    public static final int monthNovember = 11;
    /** *//**月份-12月份**/
    public static final int monthDecember = 12;
    /** *//**周-星期日**/
    public static final char weekSunday = '7';
    /** *//**周-星期一**/
    public static final char weekMonday = '1';
    /** *//**周-星期二**/
    public static final char weekTuesday = '2';
    /** *//**周-星期三**/
    public static final char weekWednesday = '3';
    /** *//**周-星期四**/
    public static final char weekThursday = '4';
    /** *//**周-星期五**/
    public static final char weekFriday = '5';
    /** *//**周-星期六**/
    public static final char weekSaturday = '6';
    /** *//**周-每周的第一天是星期一**/
    public static final char weekFirstDayOfWeek = '1';
    /** *//**最大时间**/
    public static final int maxTime = 235959;
    /** *//**最小时间**/
    public static final int minTime = 0;
    /** *//**空时间(使用最小时间)-字符串型**/
    public static final String strTimeNull = "0";
    /** *//**SQL空时间(使用最小时间, 已有单引号)-字符串型**/
    public static final String strSqlTimeNull = "'0'";
    private static final String strSqlSeparator = "'";
    private int nDate;
    private int nTime;
    private static final String strSqlDateSeparator = "/";
    private static final int nDayMilliseconds = 86400000;
    private Calendar calendar = Calendar.getInstance();

    /** *//**
     * 日期时间构造函数
     * 使用日期整数构造日期
     * 注意: 该日期整数必须与通过UtDate.getDate()
     * 方式得到的日期格式相同
     *
     * @param  整数日期
     * @param  整数时间
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(int iDate,int iTime) ...{
        calendar.setFirstDayOfWeek(2);
        nDate = iDate;
        nTime = iTime;
        validDate(nDate);
        validTime(iTime);
        setCalendarProperty(calendar, nDate,nTime);
    }

    /** *//**
     * 日期时间构造函数
     * 使用日期整数构造日期
     * 注意: 该日期整数必须与通过UtDate.getDate()
     * 方式得到的日期格式相同
     *
     * @param  整数日期
     * @param
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(int iDate) ...{
        calendar.setFirstDayOfWeek(2);
        nDate = iDate;
        nTime = 0;
        validDate(nDate);
        validTime(0);
        setCalendarProperty(calendar, nDate,nTime);
    }

    /** *//**
     * 日期时间构造函数
     * 使用年、月、日、时、分、秒构造日期时间
     *
     * @param 年
     * @param 月
     * @param 日
     * @param 时
     * @param 分
     * @param 秒
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(int year, int month, int day, int hour, int minute, int second) ...{
        calendar.clear();
        calendar.setFirstDayOfWeek(2);
        calendar.set(year, month- 1,day,hour,minute,second);
        setIntegerProperty();
        validDate(nDate);
        validTime(nTime);
    }

    /** *//**
     * 日期时间构造函数
     * 使用年、月、日、时、分、秒构造日期时间
     *
     * @param 年
     * @param 月
     * @param 日
     * @param 时
     * @param 分
     * @param 秒
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(int year, int month, int day) ...{
        calendar.clear();
        calendar.setFirstDayOfWeek(2);
        calendar.set(year, month- 1,day);
        setIntegerProperty();
        validDate(nDate);
        validTime(nTime);
    }

    /** *//**
     * 日期时间构造函数
     * 使用时间戳构造日期
     *
     * @param  时间戳
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(long l) ...{
        Date date = new Date(l);
        calendar.setFirstDayOfWeek(2);
        calendar.setTime(date);
        setIntegerProperty();
        validDate(nDate);
        validTime(nTime);
    }
    /** *//**
     * 日期时间构造函数
     * 使用字符串对象构造日期
     * 此构造函数可以处理两种类型的日期字符串
     * 日期型字符串,
     * 格式是"YYYYMMDD","HHMMSS"
     * 其中YYYY-年(4位);
     * MM-月(取值1至12, 不足两位时左补0);
     * DD-日(不足两位时, 左补0)
     * HH-时
     * MM-分
     * SS-秒
     *
     * @param  日期
     * @param  时间
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(String sDate,String sTime) ...{
        if(sDate.indexOf("/") != -1)...{
            sDate = Tools.replace(sDate,"/","");
        };
        calendar.setFirstDayOfWeek(2);
        if (sDate.length() == 17)...{
            nDate = Integer.parseInt(sDate.substring(0,);
        }else...{
            nDate = Integer.parseInt(sDate);
        }
        if (sTime.length() == 17)...{
            nTime = Integer.parseInt(sTime.substring(0, 6));
        }else...{
            nTime = Integer.parseInt(sTime);
        }
        validDate(nDate);
        validTime(nTime);
        setCalendarProperty(calendar, nDate,nTime);
    }

    /** *//**
     * 日期时间构造函数
     * 使用字符串对象构造日期
     * 此构造函数可以处理两种类型的日期字符串
     * 日期型字符串,
     * 格式是"YYYYMMDD","HHMMSS"
     * 其中YYYY-年(4位);
     * MM-月(取值1至12, 不足两位时左补0);
     * DD-日(不足两位时, 左补0)
     *
     * @param  日期
     * @param
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(String sDate) ...{
        //"2004/01/05"
        if(sDate.indexOf("/") != -1)...{
            sDate = Tools.replace(sDate,"/","");
        };
        calendar.setFirstDayOfWeek(2);
        if (sDate.length() == 17)...{
            nDate = Integer.parseInt(sDate.substring(0,);
        }else...{
            nDate = Integer.parseInt(sDate);
        }

        nTime = 0;

        validDate(nDate);
        validTime(nTime);
        setCalendarProperty(calendar, nDate,nTime);
    }

    /** *//**
     * 日期时间构造函数
     * 使用java.util.Calendar对象构造日期时间
     * 注意: 如果提供的日期为空(null), 则取当前系统时间
     *
     * @param  日历类
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public SitDateTime(Calendar calendar) ...{
        if (calendar != null)
            this.calendar = calendar;
        setIntegerProperty();
    }

    /** *//**
     * 日期时间构造函数
     *
     * 取当前系统时间
     *
     * @see    参考类#类方法或类属性
     */
    public SitDateTime() ...{
        setIntegerProperty();
    }
    /** *//**
     * 在当前日期的基础上加指定天数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  天数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addDay(int i) ...{
        calendar.add(5, i);
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 在当前日期的基础上加指定半年数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  半年数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addHalfYear(int i) ...{
        calendar.add(2, i * 6);
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 在当前日期的基础上加指定月数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  月数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addMonth(int i) ...{
        calendar.add(2, i);
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 在当前日期的基础上加指定季数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  季数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addQuarter(int i) ...{
        calendar.add(2, i * 3);
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 在当前日期的基础上加指定旬数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  旬数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addTenDay(int i) ...{
        calendar.add(2, i / 3);
        i %= 3;
        //System.out.println("addTenDay() amount=" + i);
        if (i > 0) ...{
            if (nDate + i * 10 > getEndOfMonth()) ...{
                calendar.add(2, 1);
                calendar.add(5, (i - 3) * 10);
            } else
                calendar.add(5, i * 10);
        } else if (i < 0) ...{
            if (getDay() + i * 10 <= 0) ...{
                calendar.add(2, -1);
                calendar.add(5, (3 - i) * 10);
            } else
                calendar.add(5, i * 10);
        }
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 在当前日期的基础上加指定周数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  周数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addWeek(int i) ...{
        calendar.add(5, i * 7);
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 在当前日期的基础上加指定年数
     * 注意, 此方法改变了对象的当前日期
     *
     * @param  年数
     * @return 改变后的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int addYear(int i) ...{
        calendar.add(1, i);
        setIntegerProperty();
        return nDate;
    }
    /** *//**
     * 比较两个日期, 返回两个日期之间的天数
     *
     * @param  数值型日期
     * @return 两日期之间的天数
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int compareTo(int iDate) ...{
        long l = this.calendar.getTime().getTime();
        Calendar calendar = Calendar.getInstance();
        setCalendarProperty(calendar, iDate);
        long l_12_ = calendar.getTime().getTime();
        return (int) ((l - l_12_) / 86400000L);
    }
    /** *//**
     * 比较两个日期, 返回两个日期之间的天数
     *
     * @param  日期之间的天数
     * @return 要比较的日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int compareTo(SitDateTime utdate_13_) ...{
        long l = this.calendar.getTime().getTime();
        Calendar calendar = Calendar.getInstance();
        setCalendarProperty(calendar, utdate_13_.getDate());
        long l_14_ = calendar.getTime().getTime();
        return (int) ((l - l_14_) / 86400000L);
    }
    /** *//**
     * 生成日期时间型字符串
     * 使用当前对象的日期与给定时间戳的时间
     * (忽略时间戳的日期)拼出新的日期时间型字符串
     * 生成的日期时间型字符串格式为"YYYYMMDDhhmmssiii",
     * 其中: YYYY - 年 MM - 月 DD - 日 hh - 时 mm - 分 ss - 秒 iii - 毫秒
     *
     * @param  时间戳
     * @return 格式为"YYYYMMDDhhmmssiii"的日期时间型字符串
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public String createDatetime(long l) ...{
        DecimalFormat decimalformat = new DecimalFormat("000");
        DecimalFormat decimalformat_15_ = new DecimalFormat("00");
        Date date = new Date(l);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return ("" + getDate()
                + decimalformat_15_.format((long) calendar.get(11))
                + decimalformat_15_.format((long) calendar.get(12))
                + decimalformat_15_.format((long) calendar.get(13))
                + decimalformat.format((long) calendar.get(14)));
    }
    /** *//**
     * 取整数型日期
     * 注意: 整数型日期可以直接用于比较两个日期的大小,
     * 但不能通过直接相减整数型日期计算两个日期的间隔天数
     *
     * @return 日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getDate() ...{
        return nDate;
    }
    /** *//**
     * 取日
     *
     * @return 日
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getDay() ...{
        return nDate - nDate / 100 * 100;
    }
    /** *//**
     * 取当
     * 前日期是星期几
     *
     * @param
     * @return 星期几
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public char getDayOfWeek() ...{
        int i = calendar.get(7);
        switch (i) ...{
            case 1:
                return '7';
            case 2:
                return '1';
            case 3:
                return '2';
            case 4:
                return '3';
            case 5:
                return '4';
            case 6:
                return '5';
            default:
                return '6';
        }
    }

    public String getWeekString()...{

        int i = calendar.get(7);
        switch (i) ...{
            case 1:
                return "星期天";
            case 2:
                return "星期一";
            case 3:
                return "星期二";
            case 4:
                return "星期三";
            case 5:
                return "星期四";
            case 6:
                return "星期五";
            default:
                return "星期六";
        }
        //return weekString;
    }
    /** *//**
     * 取当前日期所在半年的最后一天的日期
     *
     * @param
     * @return 日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getEndOfHalfYear() ...{
        if (getMonth() <= 6)
            return getYear() * 10000 + 630;
        return getYear() * 10000 + 1231;
    }
    /** *//**
     * 取当前日期所在月份的最后一天的日期
     *
     * @param
     * @return 日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getEndOfMonth() ...{
        int i = getYear();
        int i_11_ = getMonth();
        switch (i_11_) ...{
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return i * 10000 + i_11_ * 100 + 31;
            case 2:
                if (isLeapYear(i))
                    return i * 10000 + i_11_ * 100 + 29;
                return i * 10000 + i_11_ * 100 + 28;
            case 4:
            case 6:
            case 9:
            case 11:
                return i * 10000 + i_11_ * 100 + 30;
            default:
                throw new IllegalStateException
            ("错误的月份:" + i_11_);
        }
    }
    /** *//**
     * 取当前日期所在季度的最后一天的日
     *
     * @param
     * @return 日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getEndOfQuarter() ...{
        if (getMonth() <= 3)
            return getYear() * 10000 + 331;
        if (getMonth() <= 6)
            return getYear() * 10000 + 630;
        if (getMonth() <= 9)
            return getYear() * 10000 + 930;
        return getYear() * 10000 + 1231;
    }
    /** *//**
     * 取当前日期所在旬的最后一天的日期
     *
     * @param
     * @return 参数说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getEndOfTenDays() ...{
        if (getDay() <= 10)
            return getYear() * 10000 + getMonth() * 100 + 10;
        if (getDay() <= 20)
            return getYear() * 10000 + getMonth() * 100 + 20;
        return getEndOfMonth();
    }
    /** *//**
     * 取当前日期所在周的最后一天(周日)的日期
     *
     * @param
     * @return 日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getEndOfWeek() ...{
        if (49 != 49)
            throw new IllegalStateException
            ("本系统要求每周的第一天必须是周一,当前系统中每周的第一天设置为"
            + '1');
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setFirstDayOfWeek(2);
        calendar.set(getYear(), getMonth() - 1, getDay());
        switch (calendar.get(7)) ...{
            case 2:
                calendar.add(5, 6);
                break;
            case 3:
                calendar.add(5, 5);
                break;
            case 4:
                calendar.add(5, 4);
                break;
            case 5:
                calendar.add(5, 3);
                break;
            case 6:
                calendar.add(5, 2);
                break;
            case 7:
                calendar.add(5, 1);
                break;
        }
        return (calendar.get(1) * 10000 + (calendar.get(2) + 1) * 100
                + calendar.get(5));
    }
    /** *//**
     * 取当前日期所在年的最后一天的日期
     *
     * @param
     * @return 日期
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getEndOfYear() ...{
        return getYear() * 10000 + 1231;
    }
    /** *//**
     * 取月
     *
     * @param
     * @return 月
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getMonth() ...{
        return (nDate - nDate / 10000 * 10000) / 100;
    }
    /** *//**
     * 取分钟
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getMinute() ...{
        return (nTime - nTime/ 10000*10000)/100;
    }
    /** *//**
     * 取秒
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getSecond() ...{
        return nTime - nTime / 100 * 100;
    }
    /** *//**
     * 取年
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getYear() ...{
        return nDate / 10000;
    }
    /** *//**
     * 取小时
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public int getHour() ...{
        return nTime / 10000;
    }
    /** *//**
     * 检查当前日期的年份是否是闰年
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public boolean isLeapYear() ...{
        return isLeapYear(getYear());
    }
    /** *//**
     * 检查所给的年份是否是闰年
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public static boolean isLeapYear(int i) ...{
        if (i / 4 * 4 != i)
            return false;
        if (i / 100 * 100 != i)
            return true;
        if (i / 400 * 400 != i)
            return false;
        return true;
    }
    /** *//**
     * 把当前日期改到所在半年的最后一天的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void movetoEndOfHalfYear() ...{
        nDate = getEndOfHalfYear();
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 把当前日期改到所在月份的最后一天的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void movetoEndOfMonth() ...{
        nDate = getEndOfMonth();
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 把当前日期改到所在季度的最后一天的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void movetoEndOfQuarter() ...{
        nDate = getEndOfQuarter();
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 把当前日期改到所在旬的最后一天的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void movetoEndOfTenDays() ...{
        nDate = getEndOfTenDays();
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 把当前日期改到所在周的最后一天(周日)的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void movetoEndOfWeek() ...{
        nDate = getEndOfWeek();
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 把当前日期改到所在年的最后一天的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void movetoEndOfYear() ...{
        nDate = getEndOfYear();
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     *
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    private void setCalendarProperty(Calendar calendar, int iDate,int iTime) ...{
        int year = iDate / 10000;
        int month = (iDate - year * 10000) / 100;
        int day = iDate - year * 10000 - month * 100;

        int hour = iTime/10000;
        int minute = (iTime - hour*10000)/100;
        int second = iTime - hour * 10000 - minute * 100;

        calendar.clear();
        calendar.setFirstDayOfWeek(2);
        calendar.set(year, month - 1, day,hour,minute,second);
    }
    /** *//**
     *
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    private void setCalendarProperty(Calendar calendar, int iDate) ...{
        setCalendarProperty(calendar,iDate,0);
    }
    /** *//**
     * 日期设置函数 使用日期整数设置日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void setDate(int i) ...{
        nDate = i;
        validDate(nDate);
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 日期设置函数 使用年、月、日设置日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void setDate(int i, int i_5_, int i_6_) ...{
        calendar.clear();
        calendar.setFirstDayOfWeek(2);
        calendar.set(i, i_5_ - 1, i_6_);
        setIntegerProperty();
        validDate(nDate);
    }
    /** *//**
     * 日期设置函数
     * 使用字符串对象设置日期
     * 字符串内的日期格式必须是 YYYYMMDD 格式,
     * 说明如下 YYYY 四位数字, 表示年
     * MM 两位数字, 表示月, 不足两位时, 左补0
     * DD 两位数字, 表示日, 不足两位时, 左补0
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void setDate(String string) ...{
        nDate = Integer.parseInt(string);
        validDate(nDate);
        setCalendarProperty(calendar, nDate);
    }
    /** *//**
     * 日期设置函数 使用java.util.Date对象设置日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public void setDate(Date date) ...{
        calendar.clear();
        calendar.setTime(date);
        setIntegerProperty();
        validDate(nDate);
    }
    /** *//**
     *
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    private void setIntegerProperty() ...{
        nDate = (calendar.get(1) * 10000 + (calendar.get(2) + 1) * 100
                 + calendar.get(5));
        nTime = (calendar.get(11) * 10000 + calendar.get(12) * 100
                 + calendar.get(13));
    }
    /** *//**
     * 取符合SQL日期格式的字符串型日期
     * 取到的SQL格式日期,
     * 仅可用于数据库操作的SQL语句中,
     * 不要用于其它用途;
     * 此方法返回的字符串已经用单引号括起
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public String sqlDateFormat() ...{
        return "'" + nDate + "'";
    }
    /** *//**
     * 转换成字符串, 格式是"9999年99月99日"
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
     * @see    参考类#类方法或类属性
     */
    public String toString() ...{
        StringBuffer stringbuffer = new StringBuffer(11);
        stringbuffer.append(getYear());
        stringbuffer.append("年");
        if (getMonth() < 10)
            stringbuffer.append(' ');
        stringbuffer.append(getMonth());
        stringbuffer.append("月");
        if (getDay() < 10)
            stringbuffer.append(' ');
        stringbuffer.append(getDay());
        stringbuffer.append("日");
        return stringbuffer.toString();
    }
    /** *//**
     *检查所给日期是否是有效的日期
     *
     * @param  参数说明
     * @return 返回值说明
     * @throws SitException 是系统异常的根类
分享到:
评论

相关推荐

    Java 之 Date 和 Calendar 实例

    Java 之 Date 和 Calendar 实例

    Java Date类常用示例_动力节点Java学院整理

    在JDK1.0中,Date类是唯一的一个代表时间的类,但是由于Date类不便于实现国际化,所以从JDK1.1版本开始,推荐使用Calendar类进行时间和日期处理。这里简单介绍一下Date类的使用。

    java上传文件实例

    &lt;%@page import="java.io.File,java.util.Date;"%&gt; String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %&gt; ...

    Java Date Time教程-java.util.Date

    该类的大部分方法已不推荐使用,取而代之的是java.util.Calendar类。不过你仍然可以使用java.util.Date类去表示某个时间。下面是一个如何实例化java.util.Date的例子:  java.util.Date date = new java.util.Date...

    Java中Date类和Calendar类的常用实例小结

    主要介绍了Java中Date类和Calendar类的常用实例小结,是Java入门学习中的基础知识的运用,需要的朋友可以参考下

    java范例开发大全

    实例191 使用Date类获取系统的当前时间 324 实例192 使用DateFormat类获取系统的当前时间 325 实例193 使用GregorianCalendar类获取系统的当前时间 326 实例194 使用SimpleDateFormat类获取系统的当前时间 329 实例...

    JavaSE基础篇 -- System,Math,Date,CalendarAPI实例

    本压缩包详尽列举了JavaAPI当中的System、Runtime、Math、Date、Calendar类的实例,特别是日期格式与字符串格式的各种灵活转换。更多内容请参见:http://blog.csdn.net/zhongkelee

    Java 中 Date 与 Calendar 之间的编辑与转换实例详解

    主要介绍了Java 中 Date 与 Calendar 之间的编辑与转换 ,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下

    java范例开发大全源代码

     实例191 使用Date类获取系统的当前时间 324  实例192 使用DateFormat类获取系统的当前时间 325  实例193 使用GregorianCalendar类获取系统的当前时间 326  实例194 使用SimpleDateFormat类获取系统的当前...

    Java范例开发大全(全书源程序)

    实例191 使用Date类获取系统的当前时间 324 实例192 使用DateFormat类获取系统的当前时间 325 实例193 使用GregorianCalendar类获取系统的当前时间 326 实例194 使用SimpleDateFormat类获取系统的当前时间 329 ...

    Java范例开发大全 (源程序)

     实例191 使用Date类获取系统的当前时间 324  实例192 使用DateFormat类获取系统的当前时间 325  实例193 使用GregorianCalendar类获取系统的当前时间 326  实例194 使用SimpleDateFormat类获取系统的当前...

    java范例开发大全(pdf&源码)

    实例191 使用Date类获取系统的当前时间 324 实例192 使用DateFormat类获取系统的当前时间 325 实例193 使用GregorianCalendar类获取系统的当前时间 326 实例194 使用SimpleDateFormat类获取系统的当前时间 329 实例...

    Java BigInteger类,BigDecimal类,Date类,DateFormat类及Calendar类用法示例

    主要介绍了Java BigInteger类,BigDecimal类,Date类,DateFormat类及Calendar类用法,结合实例形式详细分析了Java使用BigInteger类,BigDecimal类,Date类,DateFormat类及Calendar类进行数值运算与日期运算相关操作...

    Java开发技术大全(500个源代码).

    HelloWorldApp.java 第一个用Java开发的应用程序。 firstApplet.java 第一个用Java开发的Applet小程序。 firstApplet.htm 用来装载Applet的网页文件 第2章 示例描述:本章介绍开发Java的基础语法知识。 ...

    Java开发详解.zip

    031105_【第11章:Java常用类库】_日期操作类(Date、Calendar)笔记.pdf 031106_【第11章:Java常用类库】_日期操作类(DateFormat、SimpleDateFormat)笔记.pdf 031107_〖第11章:Java常用类库〗_实例操作:取得...

    java时间处理工具类--DateUtils

    * 日历字段, 使用Calendar类定义的日历字段常量 * @param offset * 偏移量 * @return Date */ public Date add(int field, int offset) { cal.setTime(this.fiducialDate); cal.add(field, offset); ...

    Java2实用教程.rar

    1 5一个简单的Java应用程序的开发过程 1 6一个简单的Java小应用程序 1 7什么是JSP 习题 第2章标识符 关键字和数据类型 2 1标识符和关键字 2 2Java的基本数据类型 2 3Java的数组 习题 第3章运算符 表达式和语句 3 1...

    java常用工具类的使用

    而Date的其他构造方法和普通方法的API都不容易实现国际化,因此目前Date类的大多数方法都被标识为过时,表示更灵活的时间类请参考java.util.Calendar。 Date的输出结果是按照国际通用格式输出的,而中国更习惯于...

    Python Datetime模块和Calendar模块用法实例分析

    本文实例讲述了Python Datetime模块和Calendar模块用法。分享给大家供大家参考,具体如下: datetime模块 1.1 概述 datetime比time高级了不少,可以理解为datetime基于time进行了封装,提供了更多的实用的函数,...

    手机话费计费系统Java

    三,就是Java类库里面的date和Calendar的使用和相互转换(还要考虑实际日期的情况,比如闰年,平年,2月份,30/31日的月,月末与月初的交接,还有时间格式SimpleDateFormat 以及异常处理),这些问题相信很多同学都...

Global site tag (gtag.js) - Google Analytics