java时间戳与LocalDateTime常用转换方式


1 Date、LocalDate、LocalTime、LocalDateTime与时间戳的转换
在时间转换中,ZoneOffset表示偏移量,北京时间是+8,一般写为:ZoneOffset.of("+8"),也可以写为ZoneOffset.ofHours(8)

认识一下Date、LocalDate、LocalDateTime与时间戳

public static void main(String[] args) {
    
    // Fri Jan 06 14:09:40 CST 2023
    System.out.println(new Date());
    
    // 2023-01-06
    System.out.println(LocalDate.now());
    
    // 14:09:40.628
    System.out.println(LocalTime.now());
    
    // 2023-01-06T14:09:40.628
    System.out.println(LocalDateTime.now());
    
    // 1672985380628
    System.out.println(System.currentTimeMillis());
    
}

1.1 获取系统当前时间戳
long millis = System.currentTimeMillis();


1.2 LocalDate转Date
LocalDate nowLocalDate = LocalDate.now();  
Date date = Date.from(nowLocalDate.atStartOfDay(ZoneId.systemDefault()).toInstant())


1.3 LocalTime转Date
public void LocalTimeToUdate() {  
    LocalTime localTime = LocalTime.now();  
    LocalDate localDate = LocalDate.now();  
    LocalDateTime localDateTime = LocalDateTime.of(localDate, localTime);  
    ZoneId zone = ZoneId.systemDefault();  
    Instant instant = localDateTime.atZone(zone).toInstant();  
    java.util.Date date = Date.from(instant);  
}


1.4 LocalDateTime转Date
LocalDateTime localDateTime = LocalDateTime.now();  
Date date = Date.from(localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant());

1.6 Date转LocalDate
Date date = new Date();  
LocalDate localDate = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDate();

1.7 Date转LocalDateTime
Date date = new Date();  
LocalDateTime localDateTime = date.toInstant().atZone(ZoneOffset.ofHours(8)).toLocalDateTime();  

1.8 LocalDate转时间戳
LocalDate localDate = LocalDate.now();  
long timestamp = localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();

1.9 LocalDateTime转时间戳
LocalDateTime localDateTime = LocalDateTime.now();  
long timestamp = localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();

2 使用LocalDate、LocalTime、LocalDateTime做时间偏移
2.1 获取当月最后一天23:59:59
public static void main(String[] args) {

    // 2023-01-31
    final LocalDate lastDate = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(lastDate);

    // 2023-01-31T13:46:56.177
    final LocalDateTime lastDateTime = LocalDateTime.now().with(TemporalAdjusters.lastDayOfMonth());
    System.out.println(lastDateTime);

    // 2023-01-31T23:59:59
    final LocalDateTime lastDateTime2 = LocalDateTime.parse(lastDate + "T23:59:59");
    System.out.println(lastDateTime2);


}

3 时间戳转和LocalDateTime和Date
3.1 时间戳转LocalDateTime
秒时间戳转为LocalDateTime

public static void main(String[] args) {
        //获得时间戳
        long second = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
        // 将时间戳转为当前时间
        LocalDateTime localDateTime = LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.ofHours(8));
        // 2020-02-03T13:30:44
        System.out.println(localDateTime);
 }

毫秒时间戳转为LocalDateTime

public static void main(String[] args) {
        //获得时间戳
        long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        // 将时间戳转为当前时间
        LocalDateTime localDateTime = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
        // 2020-02-03T13:38:35.799
        System.out.println(localDateTime);
 }

3.2 时间戳转LocalDate
秒时间戳转为LocalDate

public static void main(String[] args) {
        //获得时间戳
        long seconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).getEpochSecond();
        // 将时间戳转为当前时间
        LocalDate localDate = Instant.ofEpochSecond(seconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        // 2020-02-03
        System.out.println(localDate);
 }

毫秒时间戳转为LocalDate

public static void main(String[] args) {
         //获得时间戳
        long milliseconds = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        // 将时间戳转为当前时间
        LocalDate localDate = Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.ofHours(8)).toLocalDate();
        // 2020-02-03
        System.out.println(localDate);
 }

4 LocalDateTime做时间偏移
import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        System.out.println("当前时间: " + now);

        // 偏移三天后的时间
        LocalDateTime future = now.plusDays(3);
        System.out.println("三天后的时间: " + future);
    }
}

你可能感兴趣的:(Java,java,python,开发语言)