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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Struts2 漏洞信息汇总

    Struts2 漏洞信息汇总官方链接如下:https://cwiki.apache.org/confluence/display/WW/Security+Bulletins最近不出以外Struts2又又又一次被爆出RCE漏洞【S2-061Struts远程代码执行漏洞(CVE-2020-17530)】每次Struts2RCE漏洞爆发的时候都在想,如果有个地方能统一看一下Struts2的历史漏洞就好了,网上搜索了下居然没有,翻了下Struts2官网,终于找到了需要的内容截至本文发布时,所以已经爆出的Strut.

    2022年7月19日
    19
  • 拓扑排序~C语言完整代码

    拓扑排序~C语言完整代码对一个有向无环图 DirectedAcyc 简称 DAG G 进行拓扑排序 是将 G 中所有顶点排成一个线性序列 使得图中任意一对顶点 u 和 v 若边 u v E G 则 u 在线性序列中出现在 v 之前 通常 这样的线性序列称为满足拓扑次序 TopologicalO 的序列 简称拓扑序列 简单的说 由某个集合上的一个偏序得到该集合上的一个全序 这个操作称之为拓扑排序 拿个例子来说

    2025年7月16日
    7
  • ipset基本用法和保存配置「建议收藏」

    ipset基本用法和保存配置「建议收藏」ipset基本用法ipset基本用法需要保存配置,不然重启会失效ipset基本用法1.创建ipset集合创建一个新的ipset集合:ipsetcreateSETNAMETYPENAMEipsetcreatebbhash:ip2.向集合中添加条目ipsetaddbb2.2.2.2ipsetaddbb192.168.10.21-192.168.10.313.查询条目ipsetlistipsetlistaa4.检查目标ip是否在ipset集合中ipsette

    2022年9月28日
    4
  • nginx重启命令windows(nginx前台启动命令)

    nginx-sreload:修改配置后重新加载生效nginx-sreopen:重新打开日志文件nginx-t-c/path/to/nginx.conf测试nginx配置文件是否正确关闭nginx:nginx-sstop:快速停止nginxquit:完整有序的停止nginx其他的停止nginx方式:ps-ef|grepn…

    2022年4月13日
    485
  • pyCharm全局搜索不能正常使用的解決方法

    pyCharm全局搜索不能正常使用的解決方法解决pyCharm全局搜索不能使用的方法

    2022年5月31日
    50
  • Linux 配置Java环境(图文详细)

    Linux 配置Java环境(图文详细)Linux配置Java环境1、查看系统是否有java环境2、卸载系统自带的jdk1.找到具体的jdk2.然后分别一个一个删除3.验证是否删除成功3、创建一个文件夹用于存放java的压缩包4、包下载好的jdk拖到java文件夹5、安装jdk6、配置环境变量7、让配置生效8、验证是否配置成功1、查看系统是否有java环境输入指令java-version回车即可如图表示Linux系统有自带的jdk,但是这并不是我们想要的,所以得卸载2、卸载系统自带的jdk1.找到具体的jdkrpm-qa

    2022年5月7日
    52

发表回复

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

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