日期处理

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
//日期处理
public class CalendarProcess {
    public static void main(String args[]) {
 
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        Date now = new Date();
        System.out.println("今天是:" + sdf.format(now));// 今天是:2011-07-22 16:13:02
        c.add(Calendar.YEAR, 8);
//       c.add(Calendar.MONTH, 12);   //2012-07-22 16:13:48
 
        System.out.println("8年后今天是:" + sdf.format(c.getTime()));// 8年后今天是:2019-07-22 16:13:02
        long muchTime=System.currentTimeMillis();
        System.out.println("毫秒数:"+muchTime);//1311322382218
        Date dNow=new Date(muchTime);
        System.out.println("日期类型:"+dNow); //Fri Jul 22 16:13:02 CST 2011
        System.out.println("当前时间:"+sdf.format(dNow));//2011-07-22 16:13:02
    }
}

 Calendar和Date相互转化 

         Calendar转换成Date

Calendar c1 = Calendar.getInstance();
Date date = c1.getTime();

 

         Date转化成Calendar

Date date=new Date();  
Calendar cal=Calendar.getInstance();  
cal.setTime(date); 

 

 

 

 

 

 

你可能感兴趣的:(日期)