eclipse LocalDate - 如何删除 LocalDate 中的字符“T”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33337487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 22:55:39  来源:igfitidea点击:

LocalDate - How to remove character 'T' in LocalDate

javaeclipsedatejava-8

提问by SkyvrawleR

How to remove T in my localDate?

如何在我的 localDate 中删除 T?

I need to remove the 'T' to match data in my database.

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

This is my code

这是我的代码

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);

I got this output:

我得到了这个输出:

2015-10-23T03:34:40

What is the best way to remove the 'T' character? Any idea guys?

删除“T”字符的最佳方法是什么?有什么想法吗?

回答by MadProgrammer

What is the best way to remove the 'T' character? Any idea guys?

删除“T”字符的最佳方法是什么?有什么想法吗?

Use a DateTimeFormatterto format the value of LocalDateTimethe way you want it...

使用 aDateTimeFormatterLocalDateTime您想要的方式格式化值...

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));

Which prints...

哪个打印...

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

Remember, date/time objects are just a container for amount of time which has passed since a fixed point in time (like the Unix epoch), they don't have a internal/configurable format of their own, they tend to use the current locale's format.

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

Instead, when you want to present the date/time value, you should first use a DateTimeFormatterto format the date/time value to what ever format you want and display that

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

I need to remove the 'T' to match data in my database.

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

Opps, missed that part.

哎呀,错过了那部分。

In this case, you should be converting your Date/Time values to use java.sql.Timestampand using a PreparedStatementto insert/update them

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

回答by More Al'

    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

2018-09-13 00:00

回答by Michael Cropper

Simple option using the Joda-Timelibrary.

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

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