Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 881
SkyvrawleR Avatar asked Oct 26 '15 02:10

SkyvrawleR


2 Answers

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

like image 148
MadProgrammer Avatar answered Sep 17 '22 02:09

MadProgrammer


    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

like image 45
More Al' Avatar answered Sep 18 '22 02:09

More Al'