Java基础/时间戳与Date相互转化

本文介绍了如何将时间戳转换为日期或字符串格式,同时也展示了如何将日期或字符串转换回时间戳的方法。通过示例代码,读者可以了解到具体的实现步骤。

1、时间戳转化为Date(or String)

  //时间戳转化为StingDate

      SimpleDateFormat format =  newSimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

      Long time=newLong(445555555);

      String d = format.format(time);

      Date date=format.parse(d);

      System.out.println("Format ToString(Date):"+d);

      System.out.println("Format To Date:"+date);

 

  运行结果:

       Format To String(Date):1970-01-0611:45:55

Format To Date:Tue Jan 06 11:45:55CST 1970

 

2、Date(or String)转化为时间戳

  //Date或者String转化为时间戳

      SimpleDateFormat format =  newSimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

      String time="1970-01-0611:45:55";

      Date date = format.parse(time);

      System.out.print("Format To times:"+date.getTime());

 

运行结果:

     Format To times:445555000

### Java时间戳北京时间的互相换示例代码 在 Java 中,可以使用 `java.util.Date` 和 `java.text.SimpleDateFormat` 类来实现时间戳北京时间之间的互相换。以下提供完整的代码示例: #### 时间戳换为北京时间 通过将时间戳乘以 1000(因为时间戳是以秒为单位,而 `Date` 构造函数需要毫秒),然后使用 `SimpleDateFormat` 将其格式化为北京时间字符串[^1]。 ```java import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class TimestampToBeijingTime { public static void main(String[] args) { long timestamp = System.currentTimeMillis() / 1000; // 当前时间戳(秒) Date date = new Date(timestamp * 1000); // 换为 Date 对象 // 设置时区为北京时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); String beijingTime = sdf.format(date); // 格式化为北京时间字符串 System.out.println("北京时间: " + beijingTime); } } ``` #### 北京时间换为时间戳 通过解析北京时间字符串并将其换为时间戳。此过程涉及将字符串解析为 `Date` 对象,然后获取其毫秒值并除以 1000[^2]。 ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class BeijingTimeToTimestamp { public static void main(String[] args) throws ParseException { String beijingTimeStr = "2023-10-01 12:00:00"; // 北京时间字符串 // 设置时区为北京时间 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai")); Date date = sdf.parse(beijingTimeStr); // 解析为 Date 对象 long timestamp = date.getTime() / 1000; // 换为时间戳(秒) System.out.println("时间戳: " + timestamp); } } ``` #### 使用 `LocalDateTime` 和 `Instant` 进行换 从 Java 8 开始,推荐使用 `java.time` 包中的类进行日期和时间操作。以下示例展示了如何使用 `LocalDateTime` 和 `Instant` 进行时间戳北京时间的换[^3]。 ##### 时间戳换为北京时间 ```java import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; public class TimestampToLocalDateTime { public static void main(String[] args) { long timestamp = System.currentTimeMillis() / 1000; // 当前时间戳(秒) Instant instant = Instant.ofEpochSecond(timestamp); // 换为 Instant LocalDateTime localDateTime = instant.atZone(ZoneId.of("Asia/Shanghai")).toLocalDateTime(); // 换为 LocalDateTime DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String beijingTime = localDateTime.format(formatter); // 格式化为字符串 System.out.println("北京时间: " + beijingTime); } } ``` ##### 北京时间换为时间戳 ```java import java.time.LocalDateTime; import java.time.ZoneId; import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeToTimestamp { public static void main(String[] args) { String beijingTimeStr = "2023-10-01 12:00:00"; // 北京时间字符串 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime localDateTime = LocalDateTime.parse(beijingTimeStr, formatter); // 解析为 LocalDateTime ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("Asia/Shanghai")); // 换为 ZonedDateTime long timestamp = zonedDateTime.toInstant().getEpochSecond(); // 换为时间戳(秒) System.out.println("时间戳: " + timestamp); } } ``` ### 注意事项 - 在处理时间时,确保正确设置时区,否则可能导致时间偏差。 - 如果使用 `SimpleDateFormat`,请确保线程安全,建议在多线程环境中使用 `ThreadLocal` 或 `java.time` 包中的类[^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值