zoukankan      html  css  js  c++  java
  • Java对日期Date类进行加减运算,年份加减,月份加减

    转自:http://blog.sina.com.cn/s/blog_a2614218010188lb.html

    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Date;
     
    public class DateTestUtil {
     
        
        public static void main(String[] args) throws Exception {
            
            SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
            String str="20110823";
            Date dt=sdf.parse(str);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(dt);
            rightNow.add(Calendar.YEAR,-1);//日期减1年
            rightNow.add(Calendar.MONTH,3);//日期加3个月
            rightNow.add(Calendar.DAY_OF_YEAR,10);//日期加10天
            Date dt1=rightNow.getTime();
            String reStr = sdf.format(dt1);
            System.out.println(reStr);
     
        }
     
    }
     
    注:在Calendar对象的add方法中,第二个参数为正数表示“加”,负数表示“减”
     
     
    获取系统当前时间(String类型)
    public static String getStringDateShort() {
         Date currentTime = new Date();
         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
         String dateString = formatter.format(currentTime);
         return dateString;
        } 
     
  • 相关阅读:
    EasyUI
    EasyUI
    EasyUI
    django MTV架构下的网站开发步骤
    Centos7下安装python3
    python 线程间事件通知
    hadoop MapReduce
    hadoop文件配置
    DataFrame 数据去重
    用selenium获取cookies
  • 原文地址:https://www.cnblogs.com/sharpest/p/8880517.html
Copyright © 2011-2022 走看看