0

Here is the problem:

    @GetMapping("/main/search")
    public String search (@RequestParam String departure,
                          @RequestParam String arrival,
                          @RequestParam String departureTime,
                          Model model) {

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        departureTime+=" 00:00:00"; 
        LocalDateTime date = LocalDateTime.parse(departureTime, formatter);

        List<BusFlight> busflights = busFlightService.search(departure, arrival, date);

Format comes like 2015-10-23T03:34:40

When I try to solve problem this way:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss", Locale.US);
        departureTime+=" 00:00:00";  
        LocalDateTime date = LocalDateTime.parse(departureTime, formatter);
        String currentDate = date.format(formatter);

        List<BusFlight> busflights = busFlightService.search(departure, arrival, currentDate);

I get problem in another place. Java requires to change type LocalDateTime to String type in my service calass:

public List<BusFlight> search(String departure, String arrival, **LocalDateTime** departureTime)* 
{
    *LocalDateTime departureTimeTho = departureTime.**plusDays(1)**;*

And if I change LocalDateTime to String, a can't use method plusDays(1) :(((

And I also tried this way:

        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss", Locale.US);
        departureTime+="T00:00:00";
        LocalDateTime date = LocalDateTime.parse(departureTime, formatter);

format comes the same with charater 'T' 2018-09-13T05:42:28

This way also not working for me:

 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", " ");

becuse a cann't change type to String

And this way not working becouse of exception: Unsupported field: OffsetSeconds

String localdatetime = "2011-05-01";
        localdatetime+="T00:00:00";
        DateTimeFormatter date = DateTimeFormatter.ofPattern("yyyy-MM-ddXXX:mm:ss", Locale.US);
        LocalDateTime mzt = LocalDateTime.parse(localdatetime);
        System.out.println(mzt.format(date));

Please help! How can I solve the problem?

Please CHECK my SCREENSHOTS for undestanding problem

MY CONTROLLER

MY SERVICE

MY REPOSITORY

MY DATAPICKER FORMAT

MY VIEW

More Al'
  • 93
  • 3
  • 11
  • 1
    I didn’t understand your exact problem. As far as possible pass `LocalDateTime` or another date-time type around rather than strings. You are correct that a `DateTimeFormatter` can convert between strings without `T` and `LocalDateTime` (for parsing a string *with* a `T` in it just use the one-arg `LocalDateTime.parse` and no formatter). – Ole V.V. Sep 13 '18 at 10:48
  • 3
    There is no `T` in a `LocalDateTime`. It’s `toString` method produces a string with a `T` in it, and there is no way you can avoid that. Also why would you? For presenting the `LocalDateTime` to a user you should of course format it using a `DateTimeFormatter`, for example `DateTimeFormatter.ofLocalizedDateTime(FormatSyle.MEDIUM)`. – Ole V.V. Sep 13 '18 at 10:54
  • I need LocalDateTime to use method plusDays(1) i can't parse to anther DateTime Or I Can ? – More Al' Sep 13 '18 at 10:56
  • Your question already shows how to parse into a `LocalDateTime` and use `plusDays`. I am sorry, I cannot see that you have any issue. – Ole V.V. Sep 13 '18 at 10:57
  • Possibly related: [String to ZonedDateTime is changing format](https://stackoverflow.com/questions/50120213/string-to-zoneddatetime-is-changing-format) – Ole V.V. Sep 13 '18 at 10:57
  • Ole V.V. sorry didn't get it. Can give me some more info? – More Al' Sep 13 '18 at 11:06
  • The linked question is about wanting a `ZonedDateTime` to look in a specific way (with seconds and fraction of second). If I understood your question correctly, you are asking to have a `LocalDateTime` look in a specific way (without `T`). In that way I thought the questions were similar. If they aren’t to you, just ignore my comment. – Ole V.V. Sep 13 '18 at 11:39
  • I am downvoting because you still don’t seem to have made any attempt to explain what harm you think that `T` does. If you will explain that, I’m sure we’ll understand your situation better and that we can suggest something. – Ole V.V. Sep 13 '18 at 17:31
  • The offset "X" is present in ZonedDateTime not in LocalDatetime :( – More Al' Sep 14 '18 at 06:10
  • and i get exception : Unsupported field: OffsetSeconds – More Al' Sep 14 '18 at 07:41

3 Answers3

4

There is no T in a LocalDateTime. Date-time objects do not have a “format”.

On the other hand its toString method invariably produces a string with a T in it, per the ISO 8601 standard for text representing date-time values. You cannot change the toString method nor its behaviour.

The way to avoid getting the T is avoiding calling the toString method directly or indirectly. This also means: don’t print the LocalDateTime object and don’t use it in string concatenations.

The facts mentioned so far do no harm in the code you have posted, so I suggest you learn to live it.

EDIT: If I read the first two screen shots linked to from your comments correctly, they show that your debugger shows your LocalDateTime object with a T in it. Your debugger too calls LocalDateTime.toString. There’s no way I could keep my debugger from doing that and hence showing the T, so I don’t expect there is in yours either. You are fighting the wrong problem. I recommend you stop doing that and learn to live with it.

You are correct that you should pass a LocalDateTime as the last argument to busFlightService.search (if departure and arrival are also dates and/or times, I suggest you use an appropriate date/time type for them too rather than strings). So that you can use plusDays inside the method (and similar advantages).

If at some point you need to present the LocalDateTime to a user, you are correct that the user does not want to see the T. You can generate text in any format by using a DateTimeFormatter. That class can even localize the text being generated to represent your LocalDateTime object.

    LocalDateTime dt = LocalDateTime.parse("2015-10-23T03:34:40");
    DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM)
            .withLocale(Locale.forLanguageTag("ru"));
    System.out.println(dt.format(formatter));

Output:

23 окт. 2015 г., 3:34:40

Now there’s no T (except the т in окт.). Internally in your program always use LocalDateTime or another appropriate date-time class. Only for presentation use a string.

This separation is widely used and recommended in computing: the one between your model and business logic on one side and user interface and presentation on the other.

PS: As an aside, if you receive the departure time as a string like 2015-10-23 (yyyy-MM-dd), you don’t need to modify the string in order to convert it to LocalDateTime. Either use LocalDate instead or convert like this:

    String departureTime = "2015-10-23";
    LocalDateTime dt = LocalDate.parse(departureTime).atStartOfDay();
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • public String search (@RequestParam String departure, @RequestParam String arrival, @RequestParam String departureTime, Model model) { LocalDateTime date = LocalDateTime.parse(departureTime + "T00:00:00"); DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM) .withLocale(Locale.forLanguageTag("ru")); System.out.println(date.format(formatter)); List busflights = busFlightService.search(departure, arrival, date); ` – More Al' Sep 14 '18 at 21:58
  • **date inde buger 2015-10-23T03:34:40** – More Al' Sep 14 '18 at 22:01
  • “*date inde buger 2015-10-23T03:34:40*” Yes, there’s no way to avoid that. See my edit. I cannot read the code in your comment since comments don’t allow code formatting and indentation. – Ole V.V. Sep 15 '18 at 05:38
0

Just remove that T character which you passed as an argument.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd**'T'**HH:mm:ss", Locale.US);

For more help you can take help from this link https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html

Danon
  • 2,771
  • 27
  • 37
0

As far as I know, you have solved your problem in your own way.

I suggest:

You can take another parameter when you request the "search" method. The another parameter is DateTimeFormatter formatter, so that you can use your way:

LocalDateTime departureTimeTho = departureTime.plusDays(1);
String date = departureTimeTho.format(formatter)
Winby Gao
  • 1
  • 4