第一代日期类
Date date = new Date();
System.out.printf("当前时间" + date);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss E);
System.out.printf("格式化后时间" + simpleDateFormat(date)")
String s = "1996-01-01 8:20:56 星期一";
Date date1 = simpleDateFormat.parse(s);
System.out.println("s = " + simpleDateFormat.format(date1));
第二代日期类(抽象类),构造器是私有化的,因此需要getInstance()来获取实例
Calendar instance = Calendar.getInstance();
System.out.println("年: " + instance.get(Calendar.YEAR));
System.out.println("月: " + (instance.get(Calendar.MONTH)+1));
System.out.println("日: " + instance.get(Calendar.DAY_OF_MONTH));
System.out.println("时: " + instance.get(Calendar.HOUR_OF_DAY));
System.out.println("时: " + instance.get(Calendar.HOUR));
System.out.println("分: " + instance.get(Calendar.MINUTE));
System.out.println("秒: " + instance.get(Calendar.SECOND));
第三代日期类
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt);
System.out.println("年 = " + ldt.getYear());
System.out.println("月 = " + ldt.getMonthValue());
System.out.println("月 = " + ldt.getMonth());
System.out.println("日 = " + ldt.getDayOfMonth());
System.out.println("时 = " + ldt.getHour());
System.out.println("分 = " + ldt.getMinute());
System.out.println("秒 = " + ldt.getSecond());
LocalDate now = LocalDate.now();
LocalTime now1 = LocalTime.now();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH小时mm分钟ss秒");
String format = dateTimeFormatter.format(ldt);
System.out.println("格式化后的日期为:" + format);