Can't Rid of 'T' in Localdatetime

LocalDate - How to remove character 'T' in LocalDate

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

Java 8 LocalDateTime

From the JAVA Oracle docs,

  • static LocalDateTime parse(CharSequence text)
    Obtains an instance of LocalDateTime from a text string such as 2007-12-03T10:15:30.

  • static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
    Obtains an instance of LocalDateTime from a text string using a specific formatter.

DateTimeFormatter object needs to be formatted.

How can I parse/format dates with LocalDateTime? (Java 8)

Parsing date and time

To create a LocalDateTime object from a string you can use the static LocalDateTime.parse() method. It takes a string and a DateTimeFormatter as parameter. The DateTimeFormatter is used to specify the date/time pattern.

String str = "1986-04-08 12:30";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);

Formatting date and time

To create a formatted string out a LocalDateTime object you can use the format() method.

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
LocalDateTime dateTime = LocalDateTime.of(1986, Month.APRIL, 8, 12, 30);
String formattedDateTime = dateTime.format(formatter); // "1986-04-08 12:30"

Note that there are some commonly used date/time formats predefined as constants in DateTimeFormatter. For example: Using DateTimeFormatter.ISO_DATE_TIME to format the LocalDateTime instance from above would result in the string "1986-04-08T12:30:00".

The parse() and format() methods are available for all date/time related objects (e.g. LocalDate or ZonedDateTime)

Unable to obtain LocalDateTime from TemporalAccessor when parsing LocalDateTime (Java 8)

It turns out Java does not accept a bare Date value as DateTime. Using LocalDate instead of LocalDateTime solves the issue:

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
LocalDate dt = LocalDate.parse("20140218", formatter);

Can't use LocalDateTime RequestParam in Spring, I keep getting bad request

In your request

http://localhost:8180/gateway/api/v1.0.0/getInfo?page=1&size=1&startTime=2022-07-12

You are sending query parameters startTime and endTime in form YYYY-MM-dd but on controller side you are trying to parse as LocalDateTime object, but it should be parsed as LocalDate object, since you are sending it in that form. LocalDateTime object has a form YYYY-MM-ddTHH:mm:ss.

So change this:

@GetMapping(path = "/getInfo", produces = { MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE })
public MultipleOutputSchema<CaricamentoMassivoNOBlob> get(@Nullable @RequestParam String nomeUtente,
@Nullable @RequestParam String stato,
@Nullable @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime startTime,
@Nullable @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDateTime endTime,
Optional<Integer> page, @Nullable @RequestParam Optional<Integer> size,
@Nullable @RequestParam String orderBy) {
//METHOD BODY
}

to this:

   @GetMapping(path = "/getInfo", produces = { MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE })
public MultipleOutputSchema<CaricamentoMassivoNOBlob> get(@Nullable @RequestParam String nomeUtente,
@Nullable @RequestParam String stato,
@Nullable @RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate startTime,
@Nullable @RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate endTime,
Optional<Integer> page, @Nullable @RequestParam Optional<Integer> size,
@Nullable @RequestParam String orderBy) {
//METHOD BODY
}

or this :

@GetMapping(path = "/getInfo", produces = {MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE})
public void get(
@RequestParam("startTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
@RequestParam("endTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime
) {
//METHOD BODY

System.out.println(startTime);
System.out.println(endTime);
}

If you really want to parse your query params to localDateTime objects:

http://localhost:8081/getInfo?startTime=2022-07-12T14:13:12&endTime=2022-07-12T14:13:12

Java LocalDateTime Formatter Issue

After the request from @deHaar I tryed different Java- Versions. With zulu-8 this issue is appearing, with higher Versions like zulu-17 the code works just fine. So I'll have to use a higher version or find a workaround. Thank you for your replys.

Unable to Convert Formatted String to LocalDateTime

You should not have single quotes in your input String, and your pattern is off. You wanted yyyy (not YYYY). Like,

DateTimeFormatter formatter = DateTimeFormatter.ofPattern(
"EEE MMMM d yyyy hh:mm:ss a", Locale.US);
String timestamp = "Fri August 16 2019 12:08:55 AM";
LocalDateTime localDateTime = LocalDateTime.parse(timestamp, formatter);
System.out.println(localDateTime);

Outputs (here)

2019-08-16T00:08:55


Related Topics



Leave a reply



Submit