Java 8 LocalDate won't parse valid date string [duplicate] - java

This question already has answers here:
Can't parse String to LocalDate (Java 8)
(2 answers)
Closed 3 years ago.
Java 8 here. I have the following code:
final String createdDateStr = "20110920";
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYYMMdd");
final LocalDate localDate = LocalDate.parse(createdDateStr, formatter);
At runtime I get the following exception:
java.time.format.DateTimeParseException: Text '20110920' could not be parsed at index 0
at java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1949)
at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
at java.
...being thrown from the LocalDate.parse(...) invocation. What is wrong with the parser?!

An example from the documentation:
LocalDate date = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy MM dd");
String text = date.format(formatter);
LocalDate parsedDate = LocalDate.parse(text, formatter);
You should use "yyyyMMdd" instead of "YYYYMMdd". The difference between Y and y was mentioned here.

No need to specify the format by hand. It’s already built-in.
final String createdDateStr = "20110920";
final DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
final LocalDate localDate = LocalDate.parse(createdDateStr, formatter);
System.out.println(localDate);
This outputs:
2011-09-20

Related

java.time.format.DateTimeParseException: Text '2023-01-25' could not be parsed at index 0 [duplicate]

This question already has answers here:
Why am I getting a parse exception when I try to parse the current LocalDateTime [duplicate]
(2 answers)
Change date format in a Java string
(22 answers)
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Parsing LocalDate but getting DateTimeParseException; dd-MMM-uuuu
(2 answers)
Closed 27 days ago.
I am getting error as "java.time.format.DateTimeParseException: Text '2023-01-25' could not be parsed at index 0" when passing string "2023-01-25" to parse method.
String date = "2023-01-25";
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("MMM dd, yyyyy");
LocalDate localDate = LocalDate.parse(date, dateTimeFormatter);
String dueDate = localDate.toString();
System.out.println(dueDate);
I want to display result as "Jan 25, 2023"
You need 2 different DateTimeFormatter one to parse your input String into a LocalDate. Then a second to format your LocalDate into the wanted String.
String inputString = "2023-01-25";
DateTimeFormatter parser = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse(inputString, parser);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyy");
String outputString = date.format(formatter);
System.out.println(outputString );
Your format that you created DateTimeFormatter.ofPattern("MMM dd, yyyyy") is how you want to display your date. Yet you attempt to parse the String "2023-01-25" to LocalDate with your format and this obviously fails since your format expects the String to start with 3-letter month name but it starts with "2023". So you need to create 2 formats - one for parsing a String to LocalDate and in your case the pattern should be "yyyy-MM-dd". Once you get your LocalDate you can format it to String with your formatter
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMM dd, yyyyy");
String outputString = date.format(formatter);

Convert a date to another format date using LocalDate [duplicate]

This question already has answers here:
How to format LocalDate object to MM/dd/yyyy and have format persist
(4 answers)
Java - How to get the correct date format with LocalDate
(2 answers)
want current date and time in "dd/MM/yyyy HH:mm:ss.SS" format
(11 answers)
Closed 3 months ago.
Can I know how can convert one date format to another date format.
public static LocalDate localDateToAnotherLocalDate(String oldPattern, String newPattern, String input) {
DateTimeFormatter oldFormat = DateTimeFormatter.ofPattern(oldPattern);
DateTimeFormatter newFormat = DateTimeFormatter.ofPattern(newPattern);
LocalDate localDate = LocalDate.parse(input, oldFormat);
String output = localDate.format(newFormat);
System.out.println();
return getLocalDate(output, newPattern);
}
public static LocalDate getLocalDate(String date, String datePattern) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(datePattern);
return LocalDate.parse(date, formatter);
}
#Test
void getLocalDateToAnotherLocalDateTest() {
LocalDate localDate = DateUtil.localDateToAnotherLocalDate("yyyy-MM-dd", "dd-MM-yyyy", "2022-11-20");
System.out.println("localDate" + localDate.toString());
assertEquals("20-11-2022", localDate.toString());
}
A LocalDate object can only ever be printed in ISO8601 format (yyyy-MM-dd). In order to print the object in some other format, you need to format it and save the LocalDate as a string.

DateTimeParseException; Text '2018-10-18 00:00:00' could not be parsed at index 4 [duplicate]

This question already has answers here:
Java - Converting yyyy-MM-dd'T'HH:mm:ssZ to readable dd-MM-yyyy [duplicate]
(2 answers)
Convert String Date to String date different format [duplicate]
(8 answers)
Closed 4 years ago.
I'm having some difficulty finding the right way to parse a date.
I receive the date as a String in the following format: '2018-10-18 00:00:00'
I need to convert it to 18/10/2018 and store in a variable startDate
I then need a new variable to hold an endDate variable so roll the date forward by a week.
My code:
public String getStartDate(String startDate){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localStartDate = LocalDate.parse(startDate, formatter);
String startDateFormatted = localStartDate.format(formatter);
return startDateFormatted;
}
public LocalDate getEndDate(String startDate) {
LocalDate localEndDate = LocalDate.parse(getStartDate(startDate)).plusDays(7);
return localEndDate;
}
My error is:
java.time.format.DateTimeParseException: Text '2018-10-18 00:00:00' could
not be parsed at index 4
Index 4 suggests the '-' char. Not sure the formatter pattern for removing the ISO time format that's in the original String
I'm wading through the Javadocs now but can anyone tell me how I can fix?
Your input format is wrong. Try this:
public String getStartDate(String startDate)
{
DateTimeFormatter inputFormat = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter outputFormat = DateTimeFormatter.ofPattern("dd/MM/yyyy");
return LocalDate.parse(startDate, inputFormat).format(outputFormat);
}
You need two formatters. One for the input and one for the output:
public String getStartDate(String startDate) {
DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
LocalDate localStartDate = LocalDate.parse(startDate, inputFormatter);
String startDateFormatted = localStartDate.format(outputFormatter);
return startDateFormatted;
}

Why does this date parsing fail? [duplicate]

This question already has answers here:
Is java.time failing to parse fraction-of-second?
(3 answers)
Closed 5 years ago.
I'm trying to convert a string into a LocalDateTime object.
#Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
LocalDateTime dt = LocalDateTime.parse(date, formatter);
}
I would expect this test to pass.
I get the following error:
java.time.format.DateTimeParseException: Text '20171205014657111' could not be parsed at index 0
Looks like I may have run across this bug: https://bugs.openjdk.java.net/browse/JDK-8031085 as it corresponds to the JVM version I'm using. The workaround in the comments fixes the issue for me:
#Test
public void testDateFormat() {
String date = "20171205014657111";
DateTimeFormatter dtf = new DateTimeFormatterBuilder()
.appendPattern("yyyyMMddHHmmss")
.appendValue(ChronoField.MILLI_OF_SECOND, 3).toFormatter();
LocalDateTime dt = LocalDateTime.parse(date, dtf);
}

Format the current date in java [duplicate]

This question already has answers here:
Joda-Time Formatter with a dateStyle and a timeStyle
(2 answers)
Closed 8 years ago.
I want format the current date to a format, I can do that in the following lines
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd:HH:mm:ss");
Date currentDate = new Date();
String currentDateString = dateFormat.format(currentDate);
try {
currentDate = dateFormat.parse(currentDateString);
} catch (ParseException e) {
}
But is there a way more clear and tidy to achieve that ?
Use Java 8 and the new classes LocalDateTime and DateTimeFormatter to format to a String. Don't use Date from Java 7 and lower. That old API is a mess.
LocalDateTime ldt = LocalDateTime.now();
System.out.println(ldt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd:HH:mm:ss")));
Output:
2014-06-08:14:43:01
You don't have to migrate to Java 8. If use java 6 or 7, I can use Joda-Time library.
JSR-310 form Java 8 is started from scratch, but with an API 'inspired by Joda-Time'.
Here you have example:
LocalDate date = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern("yyyy-MM-dd:HH:mm:ss");
String str = date.toString(fmt);
// might output "6 October, 2013"

Categories

Resources