日历类 Calendar
package com.sdut.k3;
import java.util.Calendar;
public class MyTest6 {
public static void main(String[] args) {
Calendar cal = Calendar.getInstance();
System.out.println(cal);
System.out.println("年份:"+cal.get(Calendar.YEAR));
System.out.println("月:"+(cal.get(Calendar.MONTH)+1));
System.out.println("日:"+cal.get(Calendar.DATE));
System.out.println("日:"+cal.get(Calendar.DAY_OF_MONTH));
System.out.println("时:"+cal.get(Calendar.HOUR_OF_DAY));
System.out.println("时:"+cal.get(Calendar.HOUR));
System.out.println("分:"+cal.get(Calendar.MINUTE));
System.out.println("秒:"+cal.get(Calendar.SECOND));
System.out.println("星期几:" + (cal.get(Calendar.DAY_OF_WEEK)+1));
Calendar cal1 = Calendar.getInstance();
cal1.set(Calendar.YEAR, 2018);
cal1.set(Calendar.MONTH, 11);
cal1.set(Calendar.DAY_OF_MONTH, 1);
System.out.println("星期几:" + cal1.get(Calendar.DAY_OF_WEEK));
cal1.set(2019, 0, 1);
System.out.println("星期几:" + cal1.get(Calendar.DAY_OF_WEEK));
}
}
Java8新特性 java.time
LocalDate()
LocalTime()
LocalDateTime()
package com.sdut.k3;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
public class MyTest7 {
public static void main(String[] args) {
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
LocalDateTime localDateTime1 = LocalDateTime.of(2021, 10, 6, 13, 23, 43);
System.out.println(localDateTime1);
System.out.println(localDateTime.getDayOfMonth());
System.out.println(localDateTime.getDayOfWeek());
System.out.println(localDateTime.getMonth());
System.out.println(localDateTime.getMonthValue());
System.out.println(localDateTime.getMinute());
LocalDate localDate1 = localDate.withDayOfMonth(22);
System.out.println(localDate);
System.out.println(localDate1);
LocalDateTime localDateTime2 = localDateTime.withHour(4);
System.out.println(localDateTime);
System.out.println(localDateTime2);
LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
System.out.println(localDateTime);
System.out.println(localDateTime3);
LocalDateTime localDateTime4 = localDateTime.minusDays(6);
System.out.println(localDateTime);
System.out.println(localDateTime4);
}
}
以LacalDateTime为例 和特定格式字符串的转换
package com.sdut.k3;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class MyTest8 {
public static void main(String[] args) {
LocalDateTime dt1 = LocalDateTime.now();
String str1 = dt1.format(DateTimeFormatter.BASIC_ISO_DATE);
System.out.println(str1);
String s1 = dt1.format(DateTimeFormatter.ISO_DATE_TIME);
System.out.println(s1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
String str2 = dt1.format(formatter);
System.out.println(str2);
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss a");
String str3 = dt1.format(formatter1);
System.out.println(str3);
LocalDateTime localDateTime1 = LocalDateTime.parse("2022-11-16T11:13:06.415");
System.out.println(localDateTime1);
LocalDateTime localDateTime2 = LocalDateTime.parse("2022年11月16日 11:13:06 上午",formatter1);
System.out.println(localDateTime2);
}
}