获取时间和时间格式化

package com.ob;
 
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
 
public class DateTest {
 
	public static void main(String[] args) throws ParseException {
		Calendar now = Calendar.getInstance();
		System.out.println("年: " + now.get(Calendar.YEAR));
		System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
		System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
		System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
		System.out.println("分: " + now.get(Calendar.MINUTE));
		System.out.println("秒: " + now.get(Calendar.SECOND));
		System.out.println("当前时间毫秒数:" + now.getTimeInMillis());
		System.out.println(now.getTime());
		**********************************************
	  	Calendar cal = Calendar.getInstance();
	    cal.setTime(date);//设置起时间
	    //System.out.println("111111111::::"+cal.getTime());
	    cal.add(Calendar.YEAR, 1);//增加一年
	    //cd.add(Calendar.DATE, 1);//增加一天  
	   //cd.add(Calendar.DATE, -10);//减10天  
	    //cd.add(Calendar.MONTH, 1);//增加一个月   
	    System.out.println("输出::"+cal.getTime());

		Date d = new Date();
		System.out.println(d);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateNowStr = sdf.format(d);
		System.out.println("格式化后的日期:" + dateNowStr);
		
		String str = "2021-6-15 17:26:33";	//要跟上面sdf定义的格式一样
		Date today = sdf.parse(str);
		System.out.println("字符串转成日期:" + today);
	}
}
上一篇:Java基础-日期


下一篇:Date,Calendar,SimpleDataFormat和System类