calendar类的日期加减

calendar类的日期加减calendar 类 日期加减 Java 代码 public nbsp class nbsp test1 nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp public nbsp static nbsp void nbsp main String nbsp args nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp Calendar nbsp c nbsp nbsp Calendar getInstance nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp int nbsp year c get Calendar YEAR nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp nbsp

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.      }    
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));  
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);  
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. }  

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/231212.html原文链接:https://javaforall.net

(0)
上一篇 2026年1月21日 下午5:01
下一篇 2026年1月21日 下午5:22


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号