Reputation: 103
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
Upvotes: 0
Views: 5982
Reputation: 86323
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();
Upvotes: 4
Reputation: 1
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)
Upvotes: 0
Reputation: 16
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
Upvotes: 0