Java日期時間類介紹及實戰

Java日期時間類:LocalDate、LocalTime、LocalDateTime

方法 描述
now()/ now(ZoneId zone) 靜態方法,根據當前時間創建對象/指定時區的對象
of(xx,xx,xx,xx,xx,xxx) 靜態方法,根據指定日期/時間創建對象
getDayOfMonth()/getDayOfYear() 獲得月份天數(1-31) /獲得年份天數(1-366)
getDayOfWeek() 獲得星期幾(返回一個 DayOfWeek 枚舉值)
getMonth() 獲得月份, 返回一個 Month 枚舉值
getMonthValue() / getYear() 獲得月份(1-12) /獲得年份
getHours()/getMinute()/getSecond() 獲得當前對象對應的小時、分鐘、秒
withDayOfMonth()/withDayOfYear()/withMonth()/withYear() 將月份天數、年份天數、月份、年份修改為指定的值并返回新的對象
with(TemporalAdjuster t) 將當前日期時間設置為校對器指定的日期時間
plusDays(), plusWeeks(), plusMonths(), plusYears(),plusHours() 向當前對象添加幾天、幾周、幾個月、幾年、幾小時
minusMonths() / minusWeeks()/minusDays()/minusYears()/minusHours() 從當前對象減去幾月、幾周、幾天、幾年、幾小時
plus(TemporalAmount t)/minus(TemporalAmount t) 添加或減少一個 Duration 或 Period
isBefore()/isAfter() 比較兩個 LocalDate
isLeapYear() 判斷是否是閏年(在LocalDate類中聲明)
format(DateTimeFormatter t) 格式化本地日期、時間,返回一個字符串
parse(Charsequence text) 將指定格式的字符串解析為日期、時間

Demo

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class LocalDateDemo {
    public static void main(String[] args) {
        LocalDate now = LocalDate.now();
        System.out.println(now);
        //字符串轉時間
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String format = now.format(dtf);
        System.out.println(format);
        String format1 = dtf.format(now);
        System.out.println(format1);
        //時間轉字符串
        LocalDateTime ld = LocalDateTime.parse("2022-01-29T13:20:30");
        System.out.println(ld.getDayOfMonth());
        System.out.println(ld.getHour());

        LocalDateTime ldNew = ld.withDayOfMonth(1);
        System.out.println(ldNew);

        LocalDateTime ldNew2 = ld.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(ldNew2);

        LocalDateTime ldNew3 = ld.plusDays(3);
        System.out.println(ldNew3);

        System.out.println(ldNew3.isAfter(ld));
    }
}

練習:打印日歷


import java.text.ParseException;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

public class Cal {
    public static void main(String ... args) throws ParseException {
        String date_str = "2024-02-28";
        getMyCalendar(date_str);
    }
    private static void getMyCalendar(String date_str) throws ParseException {
        LocalDate localDate = LocalDate.parse(date_str);
        //獲取給定的時間是幾號
        System.out.println(localDate);
        int day = localDate.getDayOfMonth();
        //獲取該月1號是本周第幾天
        int firstDayOfWeek = localDate.with(TemporalAdjusters.firstDayOfMonth()).getDayOfWeek().getValue();
        System.out.println(firstDayOfWeek);
        System.out.println(localDate);
        //獲取該月的最后一天是幾號
        int lastDay = localDate.with(TemporalAdjusters.lastDayOfMonth()).getDayOfMonth();
        //每個月多需要6行7列即可顯示完整
        int [] days = new int[6*7];
        //為數組填充值
        for(int i=1 ; i <= lastDay ; i++){
            days[firstDayOfWeek-1] = i;
            firstDayOfWeek++;
        }
        //打印日歷
        System.out.println("一\t二\t三\t四\t五\t六\t日");
        for(int i = 0 ; i < days.length ; i++){
            if(days[i]!=0){
                if(days[i]==day){
                    System.out.print("*");
                }
                System.out.print(days[i]);
            }
            System.out.print("\t");
            if((i+1)%7==0){
                System.out.println();
            }
        }
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容