将String Date转换为String date不同的格式

我是Java新手。 Postgres db包含日期格式为yyyy-MM-dd 。 我需要转换为dd-MM-yyyy

我试过这个,但显示错误的结果

  public static void main(String[] args) throws ParseException { String strDate = "2013-02-21"; DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy"); Date da = (Date)formatter.parse(strDate); System.out.println("==Date is ==" + da); String strDateTime = formatter.format(da); System.out.println("==String date is : " + strDateTime); } 

 SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy"); Date date = format1.parse("2013-02-21"); System.out.println(format2.format(date)); 

您需要使用两个DateFormat实例。 一个包含输入字符串的格式,另一个包含输出字符串的所需格式。

 public static void main(String[] args) throws ParseException { String strDate = "2013-02-21"; DateFormat inputFormatter = new SimpleDateFormat("yyyy-MM-dd"); Date da = (Date)inputFormatter.parse(strDate); System.out.println("==Date is ==" + da); DateFormat outputFormatter = new SimpleDateFormat("dd-MM-yyyy"); String strDateTime = outputFormatter.format(da); System.out.println("==String date is : " + strDateTime); } 

您需要一种格式来解析当前状态和格式以生成所需的输出。

 String strDate = "2013-02-21"; DateFormat to = new SimpleDateFormat("dd-MM-yyyy"); // wanted format DateFormat from = new SimpleDateFormat("yyyy-MM-dd"); // current format System.out.println(to.format(from.parse(strDate))); 

参考这些格式Java日期格式文档 :

日期时间的格式

试试这个代码:

 String myDate= "2013-02-21"; DateFormat iFormatter = new SimpleDateFormat("yyyy-MM-dd"); DateFormat oFormatter = new SimpleDateFormat("dd-MM-yyyy"); String strDateTime = oFormatter.format(iFormatter.parse(myDate)); 

尝试以下实用function。

 // convert String date to another string date public String convertStringDateToAnotherStringDate(String stringdate, String stringdateformat, String returndateformat){ try { Date date = new SimpleDateFormat(stringdateformat).parse(stringdate); String returndate = new SimpleDateFormat(returndateformat).format(date); return returndate; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); return ""; } } // call function with required parameter arguments String resultDate = convertStringDateToAnotherStringDate("2017-06-22", "yyyy-MM-dd", "dd-MM-yyyy") System.out.println(resultDate); 

结果: 22-06-2017

您使用相同的格式化程序来处理不同的格式。

你需要提供两个格式化的new SimpleDateFormat("dd-MM-yyyy"); 用于将2013-02-21转换为日期对象和new SimpleDateFormat("dd-MM-yyyy"); 用于将日期对象格式化为字符串21-02-2013

首先,您不应该从Postgres数据库中获取您的日期作为String 。 您应该将其作为表示日期的类型的对象。

其次,在2013年使用DateSimpleDateFormat是司空见惯和合理的,几天过去了,新的和更好的日期和时间类已经出现并且已经普遍使用。 恕我直言,没有人应该再使用旧课程,我认为它们已经过时了。

要从数据库中ResultSetLocalDate对象:

  LocalDate da = yourResultSet.getObject(3, LocalDate.class); 

(我假设日期在查询的第3列)。

要将其格式化为dd-MM-yyyy

  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-uuuu"); String strDateTime = da.format(formatter); System.out.println("==String date is : " + strDateTime); 

这会产生类似的输出

 ==String date is : 21-02-2013 

最后,如果您有问题中的字符串,并希望将其重新格式化为您想要的格式:

  String strDate = "2013-02-21"; LocalDate da = LocalDate.parse(strDate); System.out.println("==Date is ==" + da); 

这打印

 ==Date is ==2013-02-21 

我正在利用字符串匹配ISO 8601标准日期格式这一事实,这是现代日期和时间类的默认值。 所以在这种情况下,我们不需要一个明确的格式化器来解析,就像我们解析其他格式一样。

格式化为您想要的格式与以前完全一样。

旧类的问题的一个例子

在我的计算机上,问题代码的输出是

 ==Date is ==Tue Aug 06 00:00:00 CET 26 ==String date is : 06-08-0026 

你显然得到了错误的结果,你不知道出了什么问题。 错误的是您正在使用您希望用于字符串dd-MM-yyyy的格式化程序解析您的数据库日期字符串, 2013-02-21 。 所以它被解析为2013年2月21日公元。 二月有没有2013天? 对于具有默认设置的SimpleDateFormat没有问题,它只是继续计算几天到几个月和几年,并在五年半后的8月6日结束,但仍然很早。

如果我们尝试与现代课程类似:

  LocalDate.parse(strDate, formatter); 

– 我们得到java.time.format.DateTimeParseException: Text '2013-02-21' could not be parsed at index 2 。 这是因为格式化程序指示了2位数的日期,所以当解析两个数字后,输入字符串中有更多的数字,这是一个错误并报告为这样。 我认为这种行为更正确,更有帮助。

您需要使用SimpleDateFormat实例并传递您喜欢的日期模式

目前的模式: yyyy-MM-dd

需要的模式: dd-MM-yyyy

现在尝试以下方法。 它生成所需的日期格式模式。

 public class App { public static void main(String[] args) { SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat format2 = new SimpleDateFormat("dd-MM-yyyy"); Date date = null; try { date = format1.parse("2013-02-21"); System.out.println(format1.format(date)); //current format: 2013-02-21 } catch (ParseException e) { e.printStackTrace(); } System.out.println(format2.format(date)); //the format that is needed : 21-02-2013 } }