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


相关推荐

  • 执行游戏时出现0xc000007b错误的解决方法

    执行游戏时出现0xc000007b错误的解决方法

    2021年12月7日
    59
  • FindWindowEX的实例

    FindWindowEX的实例 FindWindowEX的实例  [日期:2004-12-24]  [来自:本站原创]函数功能:该函数获得一个窗口的句柄,该窗口的类名和窗口名与给定的字符串相匹配。这个函数查找子窗口,从排在给定的子窗口后面的下一个子窗口开始。在查找时不区分大小写。   函数原型:HWNDFindWindowEx(HWNDhwndParent,HWNDhwndChildAfter,LPCT

    2022年5月29日
    31
  • LVS+Keepalived实现高可用负载均衡

    LVS+Keepalived实现高可用负载均衡

    2021年8月26日
    56
  • PyCharm 支持中文和代理方法[通俗易懂]

    PyCharm 支持中文和代理方法[通俗易懂]在代码的开头(import语句之前)添加#coding:utf-8这样就可在代码及注释中包含中文了,并且输出也可以是中文

    2022年8月26日
    8
  • ansible安装部署步骤

    目录Ansible概述1、关闭防火墙2、安装相关软件3、修改主机清单4、创建密钥对,进行远程连接5、将公钥上传到被监控端6、设置ssh免交互登录Ansible概述Ansible可以同时管理Redhat系的Linux,Debian系的Linux,以及Windows主机。管理节点只在执行脚本时与远程主机连接,没有特别的同步机制,所以断电等异常一般不会影响ansbile。ansible是新出现的自动化运维工具,基于Python开发,集合了众多运维工具(puppet、cfengine、chef、fun

    2022年4月6日
    65
  • 疫情数据可视化_全国疫情数据可视化项目

    疫情数据可视化_全国疫情数据可视化项目###1.作业描述这个作业属于哪个课程课程结对学号221701225,221701208这个作业要求在哪里作业要求这个作业的目标完成需求分析,原型设计等开发前期任务作业正文本文其他参考文献echarts官方文档、Axure中使用echarts图表、Axure使用教程2.客户需求用户需求在主界面—全国地图中在全国地图上使用不同的…

    2026年4月15日
    7

发表回复

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

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