java实现String,date,timestamp相互转换

本文介绍如何在Java中进行Timestamp、Date及String之间的相互转换,并提供了详细的代码示例。
一、Timestamp转化为String:
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//定义格式,不显示毫秒

Timestamp nowdate = new Timestamp(System.currentTimeMillis());//获取系统当前时间

//方法一

String str = df.format(nowdate);

System.out.println(str);

//方法二

String str1 = nowdate.toString().

System.out.println(str1);


二、Timestamp转化为Date:

Date和Timesta是父子类关系

Timestamp nowdate = new Timestamp(System.currentTimeMillis());

Date date = new Date(nowdate);

System.out.println(date);


三、Date转化timestamp

父类不能直接向子类转化。。。但是可以通过String中间转换

Timestamp time = new Timestamp(date.getTime())



四、Date转化为String

Date date = new Date();//获取系统当前时间

 DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); 

 DateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH/mm/ss");

try {  

            dateStr = sdf.format(date);  

           System.out.println(dateStr);  

            dateStr = sdf2.format(date);  

            System.out.println(dateStr);  

        } catch (Exception e) {  

            e.printStackTrace();  

        }  


五、String转化为Date

String dateStr = "2010/05/04 12:34:23";  

        Date date = new Date();  

        //注意format的格式要与日期String的格式相匹配  

        DateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");  

       try {  

            date = sdf.parse(dateStr);  

            System.out.println(date.toString());  

        } catch (Exception e) {  

            e.printStackTrace();  

        } 


Java后端开发中,处理 `Date`、`String` 和 `Timestamp` 类型之间的转换是常见的需求,尤其在数据库交互或处理前端传入的时间数据时。以下是这些类型之间的常用转换方法。 ### Date String转换 使用 `SimpleDateFormat` 可以实现 `Date` 和 `String` 之间的相互转换。需要注意的是,`SimpleDateFormat` 不是线程安全的,建议在多线程环境下使用 `ThreadLocal` 封装或者使用 `DateTimeFormatter`(Java 8+)。 #### Date 转换String ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateToStringExample { public static void main(String[] args) { Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedDate = sdf.format(date); System.out.println("Formatted Date: " + formattedDate); } } ``` #### String 转换Date ```java import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateExample { public static void main(String[] args) { String dateStr = "2023-10-01 12:30:45"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = sdf.parse(dateStr); System.out.println("Parsed Date: " + date); } catch (ParseException e) { e.printStackTrace(); } } } ``` ### Date Timestamp转换 `java.util.Date` 和 `java.sql.Timestamp` 之间可以通过构造函数或静态方法进行转换。 #### Date 转换Timestamp ```java import java.sql.Timestamp; import java.util.Date; public class DateToTimestampExample { public static void main(String[] args) { Date date = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); System.out.println("Timestamp: " + timestamp); } } ``` #### Timestamp 转换Date ```java import java.sql.Timestamp; import java.util.Date; public class TimestampToDateExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); Date date = new Date(timestamp.getTime()); System.out.println("Date: " + date); } } ``` ### String Timestamp转换 由于 `Timestamp` 本质上是 `Date` 的子类,因此 `String` 到 `Timestamp` 的转换通常需要先将 `String` 转换为 `Date`,然后再转换为 `Timestamp`。 #### String 转换Timestamp ```java import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class StringToTimestampExample { public static void main(String[] args) { String dateStr = "2023-10-01 12:30:45"; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date date = sdf.parse(dateStr); Timestamp timestamp = new Timestamp(date.getTime()); System.out.println("Timestamp: " + timestamp); } catch (ParseException e) { e.printStackTrace(); } } } ``` ### Timestamp String转换 #### Timestamp 转换String ```java import java.sql.Timestamp; import java.text.SimpleDateFormat; public class TimestampToStringExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String formattedTimestamp = sdf.format(timestamp); System.out.println("Formatted Timestamp: " + formattedTimestamp); } } ``` ### Java 8 及以上版本的日期时间 API 从 Java 8 开始,引入了新的日期时间 API(`java.time` 包),包括 `LocalDateTime`、`LocalDate`、`LocalTime` 等类,提供了更强大的功能和更好的线程安全性。 #### LocalDateTime 转换String ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class LocalDateTimeToStringExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String formattedNow = now.format(formatter); System.out.println("Formatted LocalDateTime: " + formattedNow); } } ``` #### String 转换为 LocalDateTime ```java import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; public class StringToLocalDateTimeExample { public static void main(String[] args) { String dateStr = "2023-10-01 12:30:45"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); LocalDateTime dateTime = LocalDateTime.parse(dateStr, formatter); System.out.println("Parsed LocalDateTime: " + dateTime); } } ``` #### LocalDateTime 转换Timestamp ```java import java.sql.Timestamp; import java.time.LocalDateTime; public class LocalDateTimeToTimestampExample { public static void main(String[] args) { LocalDateTime now = LocalDateTime.now(); Timestamp timestamp = Timestamp.valueOf(now); System.out.println("Timestamp: " + timestamp); } } ``` #### Timestamp 转换为 LocalDateTime ```java import java.sql.Timestamp; import java.time.LocalDateTime; public class TimestampToLocalDateTimeExample { public static void main(String[] args) { Timestamp timestamp = new Timestamp(System.currentTimeMillis()); LocalDateTime dateTime = timestamp.toLocalDateTime(); System.out.println("LocalDateTime: " + dateTime); } } ``` ### 总结 - 使用 `SimpleDateFormat` 进行 `Date` 和 `String` 之间的转换,但在多线程环境中需注意线程安全问题。 - `Date` 和 `Timestamp` 之间的转换可以通过构造函数或 `getTime()` 方法实现。 - `String` 和 `Timestamp` 之间的转换通常需要通过 `Date` 作为中间桥梁。 - 在 Java 8 及以上版本中,推荐使用 `java.time` 包中的类进行日期时间处理,它们提供了更好的线程安全性和更直观的操作方式。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值