0

As in title I have a question. I need to parse LocalDataTime yyyy-MM-ssThh-mm-ss to LocalDataTime yyyy-MM-ss hh-mm-ss but when I do

String beforeConversionStartDate = "2020-01-24T00:06:56";
 private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDataTime parsedDate = LocalDateTime.parse(beforeConversionStartDate,formatter);

Then my output is still yyyy-MM-ddTHH:mm:ss

So my question is how to remove this "T" from LocalDataTime if parser doesn't work properly?

Snooze Snoze
  • 109
  • 1
  • 10

2 Answers2

3

You are confusing between parse and format method.

  1. First, you want to parse your input to a LocalDateTime instance
  2. Then, you format this instance according to your formatter

    String beforeConversionStartDate = "2020-01-24T00:06:56";
    LocalDateTime parsedDate = LocalDateTime.parse(beforeConversionStartDate);
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String formattedDate = parsedDate.format(formatter);
    
Rocherlee
  • 2,536
  • 1
  • 20
  • 27
  • The point is that the `parsedDate` doesn't have an innate format. So if you just naively try to print it using `toString()` the *default* format will be used to print it. – Stephen C Jan 24 '20 at 00:08
0

LocalDateTime (Java Platform SE 8 ) This function

LocalDateTime.parse(CharSequence text, DateTimeFormatter formatter);

takes in a string with formatted in pattern defined by formatter and returns a LocalDateTime Object, however the returned object is unaware of what what formatter was used. Hence, you will have to format it after you have obtained the parsed your string:

    String str = "1986-04-08 12:30";
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
    String newDateTime = formatter.format(dateTime);
DavC
  • 36
  • 1
  • 5
  • the point is that my input string is with "1986-04-08T 12:30" and as you can see there is a "T" letter between day and hour and I need to remove this T and then parse without T to LocalDataTime with T inside – Snooze Snoze Jan 24 '20 at 13:16