public class DateDemo {
? ? public static void main(String[] args) {
//? ? ? ? JDK8之前Date
// public Date()? ? ? ? ? ? ? ? ? ? 創建一個Date對象,代表的是系統當前此刻日期時間。
? ? ? ? Date date =new Date();
System.out.println(date);
// public Date(long time)? ? ? ? ? 把時間毫秒值轉換成Date日期對象。
? ? ? ? long l =System.currentTimeMillis();
Date date1 =new Date(l);
System.out.println(date1);
// public long getTime()? ? ? ? ? ? 獲取Date對象中的毫秒值
? ? ? ? long time =date1.getTime();
System.out.println(time);
// public void setTime(long time)? ? 修改日期對象的時間為當前時間毫秒值對應的時間
? ? ? ? SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time1 ="2023-11-10 23:44:23";
try {
? ? ? ? ? ? Date parse =sdf.parse(time1);
System.out.println(parse);
} catch (ParseException e) {
? ? ? ? ? ? e.printStackTrace();
}
//? ? ? ? SimpleDateFormat
// public SimpleDateFormat(String? pattern)? ? ? ? 創建簡單日期格式化對象,并封裝時間的格式
? ? ? ? SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒");
// public final String format(Date date)? ? ? ? ? ? 將日期格式化成日期/時間字符串
? ? ? ? String format =sdf1.format(new Date());
System.out.println(format);
// public final String format(Object time)? ? ? ? ? 將時間毫秒值式化成日期/時間字符串
? ? ? ? long time2 =date.getTime();
String format1 =sdf1.format(time2);
System.out.println(format1);
// public Date parse(String source)? ? ? ? ? ? ? ? 把字符串時間解析成日期對象
? ? ? ? Date format2 =null;
try {
? ? ? ? ? ? format2 =sdf1.parse("2012年6月20日 13時24分30秒");
} catch (ParseException e) {
? ? ? ? ? ? e.printStackTrace();
}
? ? ? ? System.out.println(format2);
//? ? ? ? ? ? ? ? Calendar
// public? static Calendar getInstance()? ? 獲取當前日歷對象
? ? ? ? Calendar calendar =Calendar.getInstance();
// public int get(int field)? ? 獲取日歷中的某個信息。
? ? ? ? int YEAR =calendar.get(Calendar.YEAR);
int MONTH =calendar.get(Calendar.MONTH);
int DAY_OF_MONTH =calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(YEAR+"--"+MONTH+"---"+DAY_OF_MONTH);
// public final Date getTime()? ? ? 獲取日期對象。
? ? ? ? Date time3 =calendar.getTime();
// public long getTimeInMillis()? ? 獲取時間毫秒值
? ? ? ? long timeInMillis =calendar.getTimeInMillis();
// public void set(int field,int value)? ? ? 修改日歷的某個信息。
? ? ? ? calendar.set(Calendar.YEAR,2024);
System.out.println(calendar.get(Calendar.YEAR));
// public void add(int field,int amount)? ? 為某個信息增加/減少指定的值
? ? ? ? calendar.add(Calendar.YEAR,2);
System.out.println(calendar.get(Calendar.YEAR));
//? ? ? ? JDK8
//? ? ? ? LocalDate,LocalTime,LocalDateTime 對象的獲取
// public static Xxxx now(): 獲取系統當前時間對應的該對象
? ? ? ? LocalDate localDate =LocalDate.now();
LocalTime localTime =LocalTime.now();
LocalDateTime localDateTime =LocalDateTime.now();
// public static Xxxx of(…):獲取指定時間的對象
? ? ? ? LocalDate localDate1 =LocalDate.of(2021,10,10);
LocalTime localTime1 =LocalTime.of(14,54,21);
LocalDateTime localDateTime1 =LocalDateTime.of(2021,10,10,14,54,21);
//? ? ? ? ? ? ? ? LocalDate
// public int geYear()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取年
? ? ? ? LocalDate localDate2 =LocalDate.now();
int year =localDate2.getYear();
System.out.println(year);
// public int getMonthValue()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取月份(1-12)
? ? ? ? int monthValue =localDate.getMonthValue();
System.out.println(monthValue);
// public int getDayOfMonth()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取日
? ? ? ? int dayOfYear =localDate.getDayOfMonth();
System.out.println(dayOfYear);
// public int getDayOfYear()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取當前是一年中的第幾天
? ? ? ? System.out.println(localDate.getDayOfYear());
// public DayOfWeek getDayOfWeek()? ? 獲取星期幾:ld.getDayOfWeek().getValue()
? ? ? ? int value =localDate.getDayOfWeek().getValue();
System.out.println(value);
// withYear、withMonth、withDayOfMonth、withDayOfYear? ? ? ? 直接修改某個信息,返回新日期對象
? ? ? ? LocalDate localDate3 =localDate.withYear(1998).withMonth(11).withDayOfMonth(28).withDayOfYear(220);
System.out.println(localDate3);
int year1 =localDate3.getYear();
System.out.println(year1);
// plusYears、plusMonths、plusDays、plusWeeks? ? ? ? ? ? ? ? 把某個信息加多少,返回新日期對象
? ? ? ? LocalDate localDate4 =localDate.plusYears(2).plusMonths(4).plusDays(10).plusWeeks(2);
System.out.println(localDate4);
// minusYears、minusMonths、minusDays,minusWeeks? ? ? ? ? ? 把某個信息減多少,返回新日期對象
? ? ? ? LocalDate localDate5 =
localDate1.minusYears(1).minusMonths(1).minusDays(1).minusWeeks(2);
System.out.println(localDate5);
// equals isBefore isAfter
? ? ? ? boolean equals =localDate1.equals(localDate2);
System.out.println(equals);
//? ? ? ? LocalTime
? ? ? ? LocalTime lTime2 =LocalTime.now();
// public int getHour()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取小時
? ? ? ? int hour =lTime2.getHour();
System.out.println(hour);
// public int getMinute()? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取分
? ? ? ? int minute =lTime2.getMinute();
System.out.println(minute);
// public int getSecond()? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取秒
? ? ? ? int second =lTime2.getSecond();
System.out.println(second);
// public int getNano()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 獲取納秒
? ? ? ? int nano =lTime2.getNano();
System.out.println(nano);
// withHour、withMinute、withSecond、withNano? ? ? ? ? 修改時間,返回新時間對象
? ? ? ? LocalTime localTime2 =lTime2.withHour(2).withMinute(10).withSecond(5).withNano(20);
System.out.println(localTime2);
// plusHours、plusMinutes、plusSeconds、plusNanos? ? ? 把某個信息加多少,返回新時間對象
? ? ? ? LocalTime localTime3 =localTime2.plusHours(4).plusMinutes(20).plusSeconds(13).withNano(11);
System.out.println(localTime3);
// minusHours、minusMinutes、minusSeconds、minusNanos? 把某個信息減多少,返回新時間對象
? ? ? ? LocalTime localTime4 =localTime3.minusHours(4).minusMinutes(2).minusSeconds(10).minusNanos(10);
System.out.println(localTime4);
// equals isBefore isAfter? ? ? ? ? ? ? ? ? ? ? ? ? ? 判斷2個時間對象,是否相等,在前還是在后
? ? ? ? boolean equals1 =lTime2.equals(localTime);
System.out.println(equals1);
//? ? ? ? ? ? ? ? LocalDateTime
// getYear、getMonthValue、getDayOfMonth、getDayOfYear? getDayOfWeek、getHour、getMinute、getSecond、getNano獲取年月日、時分秒、納秒等
// withYear、withMonth、withDayOfMonth、withDayOfYear? withHour、withMinute、withSecond、withNano修改某個信息,返回新日期時間對象
// plusYears、plusMonths、plusDays、plusWeeks? plusHours、plusMinutes、plusSeconds、plusNanos把某個信息加多少,返回新日期時間對象
// minusYears、minusMonths、minusDays、minusWeeks? minusHours、minusMinutes、minusSeconds、minusNanos把某個信息減多少,返回新日期時間對象
// equals isBefore isAfter判斷2個時間對象,是否相等,在前還是在后
? ? ? ? /**
? ? ? ? * LocalDateTime轉換
? ? ? ? ? */
// public LocalDate toLocalDate()轉換成一個LocalDate對象
// public LocalTime toLocalTime()轉換成一個LocalTime對象
// public static LocalDateTime of(LocalDate date, LocalTime time)將LocalDate對象和LocalTime對象轉換為LocalDateTime
? ? ? ? ? ? ? /*
* DateTimeFormatter
* */
// public static DateTimeFormatter ofPattern(時間格式yyyy....)? ? ? 獲取格式化器對象
// public String format(時間對象)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 格式化時間
? ? ? ? /*
? ? ? ? *LocalDateTime,LocalTime,LocalDate提供的使用DateTimeFormatter的方法* */
// public String format(DateTimeFormatter formatter)? ? ? ? ? ? ? ? ? ? ? ? 格式化時間
// public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)解析
? ? ? ? /*
Period
*/
// public static Period between(LocalDate start, LocalDate end) 傳入2個日期對象,得到Period對象
// public? int getYears()? ? ? 計算隔幾年,并返回
// public? int getMonths()? ? ? 計算隔幾個月,年返回
// public? int getDays()? ? ? ? 計算隔多少天,并返回
? ? ? ? ? ? ? ? /*
Duration
*/
// public? static Duration between(開始時間對象1,截止時間對象2)? ? 傳入2個時間對象,得到Duration對象
// public? long toDays()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算隔多少天,并返回
// public? long toHours()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算隔多少小時,并返回
// public? long toMinutes()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算隔多少分,并返回
// public? long toSeconds()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算隔多少秒,并返回
// public? long toMillis()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算隔多少毫秒,并返回
// public? long toNanos()? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 計算隔多少納秒,并返回
? ? ? ? /*
* ZoneId
* */
// public static Set<String>? getAvailableZoneIds() 獲取Java中支持的所有時區
// public static ZoneId systemDefault()? ? ? ? ? ? 獲取系統默認時區
// public static ZoneId of(String zoneId)? ? ? ? ? 獲取一個指定時區
? ? ? ? ? ? ? ? /*
* ZoneDateTime
* */
// public static ZonedDateTime now()? ? ? ? ? ? 獲取當前系統時區的ZonedDateTime對象
// public static ZonedDateTime now(ZoneId zone) 獲取指定時區的ZonedDateTime對象
// getYear、getMonthValue、getDayOfMonth、getDayOfYeargetDayOfWeek、getHour、getMinute、getSecond、getNano獲取年月日、時分秒、納秒等
// public ZonedDateTime withXxx(時間)? ? ? ? ? ? ? ? 修改時間系列的方法
// public ZonedDateTime minusXxx(時間)? ? ? ? ? ? ? ? 減少時間系列的方法
// public ZonedDateTime plusXxx(時間)? ? ? ? ? ? ? ? 增加時間系列的方法
? ? }
}