如何删除 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”字符的最佳方法是什么?伙计们有什么想法吗?
最佳答案
What is the best way to remove the 'T' character? Any idea guys?
使用 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 纪元)以来耗时量的容器,它们没有自己的内部/可配置格式,它们倾向于使用当前语言环境的格式。
相反,当您想要显示日期/时间值时,您应该首先使用 DateTimeFormatter
将日期/时间值格式化为您想要的任何格式并显示该格式
I need to remove the 'T' to match data in my database.
糟糕,错过了那部分。
在这种情况下,您应该将日期/时间值转换为使用 java.sql.Timestamp
并使用 PreparedStatement
来插入/更新它们
关于java - LocalDate - 如何删除 LocalDate 中的字符 'T',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33337487/