原文: https://beginnersbook.com/2013/04/java-string-to-date-conversion/

在本教程中,我们将了解如何在 Java 中将String转换为Date

将字符串转换为日期:函数

在本节之后,我分享了一个完整的示例,以各种日期格式演示StringDate的转换。对于那些只想要这个转换函数的人来说,这里是函数代码:

  1. public Date convertStringToDate(String dateString)
  2. {
  3. Date date = null;
  4. Date formatteddate = null;
  5. DateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
  6. try{
  7. date = df.parse(dateString);
  8. formatteddate = df.format(date);
  9. }
  10. catch ( Exception ex ){
  11. System.out.println(ex);
  12. }
  13. return formatteddate;
  14. }

字符串到日期转换的示例程序

  1. package beginnersbook.com;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. public class StringToDateDemo{
  6. public static void main(String args[])
  7. {
  8. String testDateString = "02/04/2014";
  9. String testDateString2 = "02-04-2014 23:37:50";
  10. String testDateString3 = "02-Apr-2014";
  11. String testDateString4 = "04 02, 2014";
  12. String testDateString5 = "Thu, Apr 02 2014";
  13. String testDateString6 = "Thu, Apr 02 2014 23:37:50";
  14. DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
  15. DateFormat df2 = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
  16. DateFormat df3 = new SimpleDateFormat("dd-MMM-yyyy");
  17. DateFormat df4 = new SimpleDateFormat("MM dd, yyyy");
  18. DateFormat df5 = new SimpleDateFormat("E, MMM dd yyyy");
  19. DateFormat df6 = new SimpleDateFormat("E, MMM dd yyyy HH:mm:ss");
  20. try
  21. {
  22. //format() method Formats a Date into a date/time string.
  23. Date d1 = df.parse(testDateString);
  24. System.out.println("Date: " + d1);
  25. System.out.println("Date in dd/MM/yyyy format is: "+df.format(d1));
  26. Date d2 = df2.parse(testDateString2);
  27. System.out.println("Date: " + d2);
  28. System.out.println("Date in dd-MM-yyyy HH:mm:ss format is: "+df2.format(d2));
  29. Date d3 = df3.parse(testDateString3);
  30. System.out.println("Date: " + d3);
  31. System.out.println("Date in dd-MMM-yyyy format is: "+df3.format(d3));
  32. Date d4 = df4.parse(testDateString4);
  33. System.out.println("Date: " + d4);
  34. System.out.println("Date in MM dd, yyyy format is: "+df4.format(d4));
  35. Date d5 = df5.parse(testDateString5);
  36. System.out.println("Date: " + d5);
  37. System.out.println("Date in E, MMM dd yyyy format is: "+df5.format(d5));
  38. Date d6 = df6.parse(testDateString6);
  39. System.out.println("Date: " + d6);
  40. System.out.println("Date in E, E, MMM dd yyyy HH:mm:ss format is: "+df6.format(d6));
  41. }
  42. catch (Exception ex ){
  43. System.out.println(ex);
  44. }
  45. }
  46. }

输出:

  1. Date: Wed Apr 02 00:00:00 IST 2014
  2. Date in dd/MM/yyyy format is: 02/04/2014
  3. Date: Wed Apr 02 23:37:50 IST 2014
  4. Date in dd-MM-yyyy HH:mm:ss format is: 02-04-2014 23:37:50
  5. Date: Wed Apr 02 00:00:00 IST 2014
  6. Date in dd-MMM-yyyy format is: 02-Apr-2014
  7. Date: Wed Apr 02 00:00:00 IST 2014
  8. Date in MM dd, yyyy format is: 04 02, 2014
  9. Date: Wed Apr 02 00:00:00 IST 2014
  10. Date in E, MMM dd yyyy format is: Wed, Apr 02 2014
  11. Date: Wed Apr 02 23:37:50 IST 2014
  12. Date in E, E, MMM dd yyyy HH:mm:ss format is: Wed, Apr 02 2014 23:37:50

参考: