ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 在 Java 中将`String`转换为日期 > 原文: [https://beginnersbook.com/2013/04/java-string-to-date-conversion/](https://beginnersbook.com/2013/04/java-string-to-date-conversion/) 在本教程中,我们将了解如何在 Java 中将`String`转换为`Date`。 ### 将字符串转换为日期:函数 在本节之后,我分享了一个完整的示例,以各种[日期格式](https://beginnersbook.com/2013/04/java-date-format/)演示`String`到`Date`的转换。对于那些只想要这个转换函数的人来说,这里是函数代码: ```java public Date convertStringToDate(String dateString) { Date date = null; Date formatteddate = null; DateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); try{ date = df.parse(dateString); formatteddate = df.format(date); } catch ( Exception ex ){ System.out.println(ex); } return formatteddate; } ``` ### 字符串到日期转换的示例程序 ```java package beginnersbook.com; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class StringToDateDemo{ public static void main(String args[]) { String testDateString = "02/04/2014"; String testDateString2 = "02-04-2014 23:37:50"; String testDateString3 = "02-Apr-2014"; String testDateString4 = "04 02, 2014"; String testDateString5 = "Thu, Apr 02 2014"; String testDateString6 = "Thu, Apr 02 2014 23:37:50"; DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy"); DateFormat df4 = new SimpleDateFormat("MM dd, yyyy"); DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy"); DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss"); try { //format() method Formats a Date into a date/time string. Date d1 = df.parse(testDateString); System.out.println("Date: " + d1); System.out.println("Date in dd/MM/yyyy format is: "+df.format(d1)); Date d2 = df2.parse(testDateString2); System.out.println("Date: " + d2); System.out.println("Date in dd-MM-yyyy HH:mm:ss format is: "+df2.format(d2)); Date d3 = df3.parse(testDateString3); System.out.println("Date: " + d3); System.out.println("Date in dd-MMM-yyyy format is: "+df3.format(d3)); Date d4 = df4.parse(testDateString4); System.out.println("Date: " + d4); System.out.println("Date in MM dd, yyyy format is: "+df4.format(d4)); Date d5 = df5.parse(testDateString5); System.out.println("Date: " + d5); System.out.println("Date in E, MMM dd yyyy format is: "+df5.format(d5)); Date d6 = df6.parse(testDateString6); System.out.println("Date: " + d6); System.out.println("Date in E, E, MMM dd yyyy HH:mm:ss format is: "+df6.format(d6)); } catch (Exception ex ){ System.out.println(ex); } } } ``` **输出:** ```java Date: Wed Apr 02 00:00:00 IST 2014 Date in dd/MM/yyyy format is: 02/04/2014 Date: Wed Apr 02 23:37:50 IST 2014 Date in dd-MM-yyyy HH:mm:ss format is: 02-04-2014 23:37:50 Date: Wed Apr 02 00:00:00 IST 2014 Date in dd-MMM-yyyy format is: 02-Apr-2014 Date: Wed Apr 02 00:00:00 IST 2014 Date in MM dd, yyyy format is: 04 02, 2014 Date: Wed Apr 02 00:00:00 IST 2014 Date in E, MMM dd yyyy format is: Wed, Apr 02 2014 Date: Wed Apr 02 23:37:50 IST 2014 Date in E, E, MMM dd yyyy HH:mm:ss format is: Wed, Apr 02 2014 23:37:50 ``` #### 参考: * [`DateFormat parse()`方法](https://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#parse(java.lang.String)) * [`Date` - javadoc](https://docs.oracle.com/javase/7/docs/api/java/util/Date.html)