java.time.format.DateTimeParseException: Text '13/11/2020' could not be parsed at index 2
Multiple issues:
- Your String is look like a
LocalDate
andLocalDateTime
, it doesn't contain the time part - The pattern you are using is not the same format as your String it should be
dd/MM/yyyy
ordd/MM/uuuu
and notdd-MM-yyyy
To parse your String you need:
LocalDate date = LocalDate.parse(str, DateTimeFormatter.ofPattern("dd/MM/uuuu"));
dateTime = dateTime.plusMonths(3);
Or if you want LocalDateTime
, then you can use DateTimeFormatterBuilder
with 0 for hours, minutes and seconds:
DateTimeFormatter formatter = new DateTimeFormatterBuilder()
.appendPattern("dd/MM/uuuu")
.parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
.parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
.parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
.toFormatter();
LocalDateTime dateTime = LocalDateTime.parse(str, formatter);
dateTime = dateTime.plusMonths(3);
and the result will be like this: 2021-02-13T00:00
Note: dateTime.plusMonths(3)
is immutable
so to change the value you have to assign the new value to the variable.
Caused by: java.time.format.DateTimeParseException: Text '11 2 AM' could not be parsed at index 3
try {
String time = "11 2 AM";
String parsePatter = time.contains(":") ? "hh:mm a" : "hh m a";
SimpleDateFormat parser = new SimpleDateFormat(parsePatter);
Date date = parser.parse(time);
// Result
SimpleDateFormat resultParse = new SimpleDateFormat("hh:mm a");
String result = resultParse.format(date);
System.out.println(result); // 11:02 AM
} catch (Exception ex) {
ex.printStackTrace();
}
Java LocalDateTime Text could not be parsed at index 0 exception
You need to use your format inside LocalDateTime::parse as follows:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
class Main {
public static void main(String[] args) {
DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
String caseStartDate = dateFormat.format(LocalDateTime.now());
System.out.println(caseStartDate);
LocalDateTime localdatetime = LocalDateTime.parse(caseStartDate, dateFormat);
System.out.println(localdatetime);
}
}
Output:
01/05/2020 09:13
2020-05-01T09:13
Also, see how the toString()
method of LocalDateTime
has been overridden:
@Override
public String toString() {
return date.toString() + 'T' + time.toString();
}
Related Topics
How to Call an Excel Vba Macro from Java Code
Android Room Persistent: Appdatabase_Impl Does Not Exist
How to Junit Test That Two List<E> Contain the Same Elements in the Same Order
Determine the Number of Pages in a Pdf File
How to Efficiently Read Multiple Json Files into a Dataframe or Javardd
Passing Empty List as Parameter to JPA Query Throws Error
Read Huge Excel File(500K Rows) in Java
Name a File in Java to Include Date and Time Stamp
How to Apply Spring Boot Filter Based on Url Pattern
Kafka: Failed to Update Metadata After 60000 Ms
Placing a Jlabel At a Specific X,Y Coordinate on a Jpanel
Status Expected:<200> But Was:<404> in Spring Test
How to Clean Project Cache in Intellij Idea Like Eclipse'S Clean
In Junit 5, How to Run Code Before All Tests
Intellij Compilation Error Zip End Header Not Found
Batch Inserts Using JPA Entitymanager
Type Safety: Unchecked Cast from Object to Arraylist<Myvariable>