Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

java - LocalDate - How to remove character 'T' in LocalDate

How to remove T in my localDate?

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

Use a DateTimeFormatter to format the value of LocalDateTime the way you want it...

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.

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

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

Opps, missed that part.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...