java 得到以后的日期

详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt222


import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

 

 

public class DateCompareDemo {

     //得到以后的日期

     public Date dateCompare(String str, int month,int day) throws ParseException {

          Date date = null;

          SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd" );

          date = df.parse(str);

          Calendar c = Calendar. getInstance();

          c.setTime(date);

          c.add(Calendar. MONTH, month);

          c.add(Calendar. DAY_OF_MONTH, day);

          return c.getTime();

     }

     public static void main(String[] args) throws ParseException {

          DateCompareDemo dcd = new DateCompareDemo();

          String A = "2010-01-01";

          Date B = dcd.dateCompare( "2010-05-03", 0, 0);

          Date A1 = dcd.dateCompare(A, 3, 0);  //得到A三个月以后的日期

          Date A2 = dcd.dateCompare(A, 0, 5);   //得到A五天后的日期

          int result1 = A1.compareTo(B);

          int result2 = A2.compareTo(B);

          if(result1 < 0) {

               System. out.println("A三个月以后的日期比B小" );

          } else {

               System. out.println("A三个月以后的日期比B大" );

          }

          if(result2 <0) {

               System. out.println("A五天后的日期比B小" );

          } else {

               System. out.println("A五天后的日期比B大" );

          }

     }

}

 

posted on 2016-11-23 19:11  grefr  阅读(217)  评论(0编辑  收藏  举报

导航