package cn.itcast.extendsTest;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class CalendarTest {
private final static Date DATE = new Date();
public static void main(String[] args) throws ParseException {
System.out.println("Date类输出的日期 : " + DATE);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("SimpleDateFormat格式化后的日期1 : " + simpleDateFormat.format(DATE));
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yy/MM/dd HH:mm");
System.out.println("SimpleDateFormat格式化后的日期2 : " + simpleDateFormat1.format(DATE));
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
String str = "2022年6月6日";
Date date1 = df.parse(str);
System.out.println("设定的指定日期:" + date1);
Calendar calendar = Calendar.getInstance();
System.out.println("日历类取得的日期:" + calendar.getTime());
int i = calendar.get(Calendar.MONTH) + 1;
System.out.println("当前的月份是 = " + i);
calendar.setTime(DATE);
System.out.println("从日期类获得的当前日期:" + calendar.getTime());
calendar.add(Calendar.DAY_OF_MONTH, 3);
System.out.println("从日期类获得的3天后日期:" + calendar.getTime());
}
}
|