calendar类,日期加减

calendar类,日期加减


Java代码
  1. public class test1 {   
  2.   
  3.     public static void main(String[] args) {   
  4.           Calendar c = Calendar.getInstance();   
  5.           int year=c.get(Calendar.YEAR);   
  6.              
  7.   
  8.             int month=c.get(Calendar.MONTH)+1;   
  9.           int date=c.get(Calendar.DATE);   
  10.           System.out.println("今天是"+year+"年"+month+"月"+date+"日");   
  11.           System.out.println("是今年的第"+c.get(Calendar.DAY_OF_YEAR)+"天");   
  12.           System.out.println("c.getTime()的結果: "+c.getTime());   
  13.           System.out.println("new Date()的結果: "+new Date());   
  14.           c.set(Calendar.DAY_OF_YEAR, date + 30);   
  15.           System.out.println("17天后是"+c.getTime());   
  16.       }   
  17.   
  18. }  
[java]  view plain  copy
  1. public class test1 {  
  2.   
  3.     public static void main(String[] args) {  
  4.           Calendar c = Calendar.getInstance();  
  5.           int year=c.get(Calendar.YEAR);  
  6.             
  7.   
  8.             int month=c.get(Calendar.MONTH)+1;  
  9.           int date=c.get(Calendar.DATE);  
  10.           System.out.println("今天是"+year+"年"+month+"月"+date+"日");  
  11.           System.out.println("是今年的第"+c.get(Calendar.DAY_OF_YEAR)+"天");  
  12.           System.out.println("c.getTime()的結果: "+c.getTime());  
  13.           System.out.println("new Date()的結果: "+new Date());  
  14.           c.set(Calendar.DAY_OF_YEAR, date + 30);  
  15.           System.out.println("17天后是"+c.getTime());  
  16.       }  
  17.   
  18. }  


Java代码
  1. /**    
  2.      * 得到几天前的时间    
  3.       *     
  4.       * @param d    
  5.       * @param day    
  6.       * @return    
  7.       */     
  8.      public static Date getDateBefore(Date d, int day) {      
  9.          Calendar now = Calendar.getInstance();      
  10.          now.setTime(d);      
  11.          now.set(Calendar.DATE, now.get(Calendar.DATE) - day);      
  12.          return now.getTime();      
  13.      }     
  14.   /**    
  15.       * 得到几天后的时间    
  16.       *     
  17.       * @param d    
  18.       * @param day    
  19.       * @return    
  20.       */     
  21.      public static Date getDateAfter(Date d, int day) {      
  22.         Calendar now = Calendar.getInstance();      
  23.          now.setTime(d);      
  24.         now.set(Calendar.DATE, now.get(Calendar.DATE) + day);      
  25.          return now.getTime();      
  26.      }    
[java]  view plain  copy
  1. /**   
  2.      * 得到几天前的时间   
  3.       *    
  4.       * @param d   
  5.       * @param day   
  6.       * @return   
  7.       */    
  8.      public static Date getDateBefore(Date d, int day) {     
  9.          Calendar now = Calendar.getInstance();     
  10.          now.setTime(d);     
  11.          now.set(Calendar.DATE, now.get(Calendar.DATE) - day);     
  12.          return now.getTime();     
  13.      }    
  14.   /**   
  15.       * 得到几天后的时间   
  16.       *    
  17.       * @param d   
  18.       * @param day   
  19.       * @return   
  20.       */    
  21.      public static Date getDateAfter(Date d, int day) {     
  22.         Calendar now = Calendar.getInstance();     
  23.          now.setTime(d);     
  24.         now.set(Calendar.DATE, now.get(Calendar.DATE) + day);     
  25.          return now.getTime();     
  26.      }    



注意int month=c.get(Calendar.MONTH)+1哦,好像系统是从0开始计月份,到了12月就归零了。所以单独取月份时,要在后面加一才能得到当前的月份。 


calender日期加减后赋值给Date类型

Java代码
  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");   
  2. String time=sdf.format(new Date());    
  3. Calendar cd = Calendar.getInstance();   
  4.   
  5. try {   
  6.     cd.setTime(sdf.parse(time));   
  7. catch (ParseException e) {               
  8.     e.printStackTrace();   
  9. }   
  10.       cd.add(Calendar.DATE, 1);//增加一天        
  11.        //cal.add(Calendar.DATE, -1);      //减一天    
  12.        //cd.add(Calendar.MONTH, 1);//增加一月    
  13.       Date date=cd.getTime();    
  14.       System.out.println(sdf.format(date));  
[java]  view plain  copy
  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
  2. String time=sdf.format(new Date());   
  3. Calendar cd = Calendar.getInstance();  
  4.   
  5. try {  
  6.     cd.setTime(sdf.parse(time));  
  7. catch (ParseException e) {              
  8.     e.printStackTrace();  
  9. }  
  10.       cd.add(Calendar.DATE, 1);//增加一天       
  11.        //cal.add(Calendar.DATE, -1);      //减一天   
  12.        //cd.add(Calendar.MONTH, 1);//增加一月   
  13.       Date date=cd.getTime();   
  14.       System.out.println(sdf.format(date));  




将yyyy//MM/dd的字符串类型转为Date类型 

Java代码
SimpleDateFormat format =  new SimpleDateFormat( "yyyy/MM/dd");   
  1. str12 = format.parse(str12_1);  
[java]  view plain  copy
  1. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");  
  2. str12 = format.parse(str12_1);  



在excel导入数据时,日期类型的数据直接获取

Java代码
  1. CellType t1 = st.getCell(11, row).getType();   
  2. Date regDate = null;   
  3. Date str12=null;//出生年月,不能为空   
  4. if (t1 == CellType.DATE)   
  5. {   
  6.     DateCell regCell = (DateCell) st.getCell(11, row);     
  7.     str12 = regCell.getDate();    
  8. }  

猜你喜欢

转载自blog.csdn.net/a11121112111a/article/details/80084793