Java 8以前采用:1.java.util.Date类
Date date=new Date();
System.out.println(date);//打印系统当前时间 Fri Aug 31 16:49:34 CST 2018
2.java.util.Calender类
Calendar calendar=Calendar.getInstance();
int year=calendar.get(Calendar.YEAR);
int month=calendar.get(Calendar.MONTH);
int day=calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(year + "-" + (month + 1) + "-" + day);//打印系统当前时间 2018-8-31
缺点:Date类起始年份是1900年,月份从0开始;Calendar月份依旧是从0开始计算;Date和Calendar类都是可变的
Java 8以后采用:1.日期采用 java.time.LocalDate;时间采用java.time.LocalTime;合并时间与日期采用LocalDateTime
LocalDate ofDate1=LocalDate.of(2018, 8, 31);//创建
LocalTime ofTime=LocalTime.of(16, 56,30);//创建
LocalDate parseDate=LocalDate.parse("2018-08-31");//创建
LocalTime parseTime=LocalTime.parse("16:56:30");//创建
LocalDateTime dt1 = LocalDateTime.of(2018, Month.MARCH, 31, 13, 45, 20);//创建
LocalDateTime dt2 = LocalDateTime.of(ofDate1, ofTime);//创建
2.格式化日期采用java.time.format.DateTimeFormatter
String s1 = ofDate1.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(s1);//20180831
String s2 = ofDate1.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(s2);//2018-08-31
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");//设置特定格式
String s3 = ofDate1.format(formatter);
System.out.println(s3);//31/08/2018