java 使用Date类、Calendar类,实现增加日期

Date date= new Date();
System.out.println("Date date= new Date()中的date:   "+date);

输出对象date:

 Thu Jul 27 15:37:53 CST 2017

 

 
Calendar c =Calendar.getInstance();
System.out.println("日历c:   "+c);

 日历c:   java.util.GregorianCalendar[time=1501141073691,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="Asia/Shanghai",offset=28800000,dstSavings=0,useDaylight=false,transitions=19,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2017,MONTH=6,WEEK_OF_YEAR=30,WEEK_OF_MONTH=5,DAY_OF_MONTH=27,DAY_OF_YEAR=208,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=3,HOUR_OF_DAY=15,MINUTE=37,SECOND=53,MILLISECOND=691,ZONE_OFFSET=28800000,DST_OFFSET=0]

 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Test {

    public static void main(String[] args) throws ParseException {
//        输出当前系统时间 yy-mm-dd hh:mm:ss
        SimpleDateFormat df= new SimpleDateFormat("YYYY-M-d hh:mm:ss");
        Date date= new Date();
        System.out.println("Date date= new Date()中的date:   "+date);
        String nowTime=df.format(date);
        System.out.println("当前时间:   "+nowTime);
//        输出指定时间
        date=new SimpleDateFormat("yy-MM-dd").parse("2017-07-27");
        System.out.println("指定的时间date:   "+date);
        
//        增加时间
        Calendar c =Calendar.getInstance();//创建日历对象,Calendar.getInstance()固定写法,表示创建日历对象
        System.out.println("日历c:   "+c);
        c.setTime(date);//给日历设置时间
        int d=c.get(Calendar.DATE);//获取日历中的日期(DATE)
        System.out.println("日历c中的日期"+d);
        c.set(Calendar.DATE,d+7);//设置日历中的日期
        String ss=new SimpleDateFormat("yy-MM-dd").format(c.getTime());//时间对象转换字符串
        System.out.println(ss);
    }
}

具体参考:

更多增减时间操作:

http://blog.csdn.net/kyfg27_niujin/article/details/8077828

http://blog.csdn.net/joyous/article/details/9630893

posted @ 2017-07-31 11:23  龙心呢  阅读(929)  评论(0编辑  收藏  举报