时间戳互转"/>
Java时间戳互转
企微会议接口时间戳
Java时间戳转日期和日期转时间戳,10位长度,uint32类型,日期格式自定义,只到日,不到小时,在企微的会议接口会用到,运行结果如下:
当前时间戳:1698026308
10 天后的时间戳:1698883200
转换后的日期:2023-10-23
转换后的日期:2023/11/02
详细代码如下:
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;public class TimeTransfer {public static void main(String[] args) {// 获取当前时间的时间戳Instant currentTimestamp = Instant.now();long currentTimestampInSeconds = currentTimestamp.getEpochSecond();System.out.println("当前时间戳:" + currentTimestampInSeconds);// 获取 10 天后的时间戳LocalDate currentDate = LocalDate.now();LocalDate futureDate = currentDate.plusDays(10);Instant futureTimestamp = futureDate.atStartOfDay().toInstant(ZoneOffset.UTC);long futureTimestampInSeconds = futureTimestamp.getEpochSecond();System.out.println("10 天后的时间戳:" + futureTimestampInSeconds);// 时间戳转日期Instant instantCurr = Instant.ofEpochSecond(currentTimestampInSeconds);Instant instantFuture = Instant.ofEpochSecond(futureTimestampInSeconds);LocalDate dateCurr = instantCurr.atZone(ZoneOffset.UTC).toLocalDate();LocalDate dateFuture = instantFuture.atZone(ZoneOffset.UTC).toLocalDate();// 将 LocalDate 格式化为指定的日期字符串DateTimeFormatter formatterCurr = DateTimeFormatter.ofPattern("yyyy-MM-dd");DateTimeFormatter formatterFuture = DateTimeFormatter.ofPattern("yyyy/MM/dd");String formattedDateCurr = dateCurr.format(formatterCurr);String formattedDateFuture = dateFuture.format(formatterFuture);System.out.println("转换后的日期:" + formattedDateCurr);System.out.println("转换后的日期:" + formattedDateFuture);}
}
更多推荐
Java时间戳互转
发布评论