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);
    }
}

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

L-960

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值