SkyvrawleR
SkyvrawleR

Reputation: 412

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?

Upvotes: 19

Views: 34508

Answers (3)

Michael Cropper
Michael Cropper

Reputation: 1032

Simple option using the Joda-Time library.

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

Upvotes: 3

More Al'
More Al'

Reputation: 103

    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

Upvotes: 6

MadProgrammer
MadProgrammer

Reputation: 347314

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

Upvotes: 25

Related Questions