LocalDate – 如何删除LocalDate中的字符’T’

如何在我的localDate中删除T?

我需要删除’T’以匹配我的数据库中的数据。

这是我的代码

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.US); String strLocalDate = patientDiagnosisByDoctor.getDiagnosisDateTime().toLocalDateTime().toString(); LocalDateTime localDate = LocalDateTime.parse(strLocalDate, formatter); System.out.println(localDate); 

我得到了这个输出:

 2015-10-23T03:34:40 

删除“T”字符的最佳方法是什么? 任何想法的家伙?

删除“T”字符的最佳方法是什么? 任何想法的家伙?

使用DateTimeFormatter以您希望的方式格式化LocalDateTime的值…

 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.US); String strLocalDate = "2015-10-23T03:34:40"; LocalDateTime localDate = LocalDateTime.parse(strLocalDate, formatter); System.out.println(localDate); System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(localDate)); System.out.println(DateTimeFormatter.ofPattern("HH:mm:ss yyyy-MM-dd ").format(localDate)); 

哪个打印……

 2015-10-23T03:34:40 2015-10-23 03:34:40 03:34:40 2015-10-23 

请记住,日期/时间对象只是自固定时间点(如Unix时代)以来已经过的时间量的容器,它们没有自己的内部/可配置格式,它们倾向于使用当前locale的格式。

相反,当您想要显示日期/时间值时,应首先使用DateTimeFormatter将日期/时间值格式化为您想要的格式并显示

我需要删除’T’以匹配我的数据库中的数据。

奥普斯,错过了这一部分。

在这种情况下,您应该转换日期/时间值以使用java.sql.Timestamp并使用PreparedStatement插入/更新它们

  String localTime = "2018-09-13 00:00:00"; DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); LocalDateTime date = LocalDateTime.parse(localTime, formatter); String replace = date.toString().replace("T", " "); System.out.println(replace); 

2018-09-13 00:00

使用Joda-Time库的简单选项。

 new LocalDateTime(DateTimeZone.forID("Europe/London")).toString().replace("T", " ");