buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

java.time.LocalDate格式化 及 LocalDate转Date


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class TestMain {
    public static void main(String[] args) {
    
        LocalDate localDate = LocalDate.now();//.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
        // 格式化LocalDate或LocalTime
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        System.out.println(localDate.format(formatter));
        // LocalDate转Date
        System.out.println(Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));

        LocalDateTime localTime = LocalDateTime.now();
        // LocalDateTime转Date
        System.out.println(Date.from(localTime.atZone(ZoneId.systemDefault()).toInstant()));

    }
}

 

posted on 2022-07-12 22:38  buguge  阅读(682)  评论(0编辑  收藏  举报