19

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?

SkyvrawleR
  • 412
  • 2
  • 6
  • 18

3 Answers3

24

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Any idea on how to remove trailing '.0' ? 2015-10-23 03:34:40.0 <--- – SkyvrawleR Oct 26 '15 at 07:36
  • 2
    `substring()`, but where are you getting that value? Nothing shown so far would do that. – Andreas Oct 26 '15 at 07:37
  • from sql.timestamp, nevermind, MySQL does not detect that. Thank you sir – SkyvrawleR Oct 26 '15 at 07:39
  • 2
    FYI: you'd normally change the format, but in your case, you don't need it, you jays need to use Timestamp and let the database worry about it – MadProgrammer Oct 26 '15 at 08:03
  • 2
    @Andreas Why not just change the format specifier, mind you, for inserting values in the database, you just need to use Timestamp and let the database worry about :P – MadProgrammer Oct 26 '15 at 08:05
  • @MadProgrammer Preaching to the choir, man. Never use string concatenation to build SQL, always use `PreparedStatement`. No formatting issues, and no SQL Injection vulnerabilities. That's why my comment "answer" was shortened to one word. ;-) – Andreas Oct 26 '15 at 15:51
5
    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

More Al'
  • 93
  • 3
  • 11
3

Simple option using the Joda-Time library.

new LocalDateTime(DateTimeZone.forID("Europe/London")).toString().replace("T", " ");
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Michael Cropper
  • 872
  • 1
  • 10
  • 28