String to LocalDate
As you use Joda Time, you should use DateTimeFormatter
:
final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MMM-dd");
final LocalDate dt = dtf.parseLocalDate(yourinput);
If using Java 8 or later, then refer to hertzi's answer
Convert String to LocalDate
You should use another pattern to parse input date
public static void main(String[] args) {
System.out.println(convert("21022019"));
}
static LocalDate convert(String date) {
return LocalDate.parse(date, DateTimeFormat.forPattern("ddMMyyyy"));
}
How to convert String to LocalDate
When you are printing LocalDate instance, it is actually calling toString() method of LocalDate class. And according to javadoc 11, this method
Outputs this date as a String, such as 2007-12-03.
The output will be in the ISO-8601 format uuuu-MM-dd.
So you are seeing, it will always print in uuuu-MM-dd format.
Think LocalDate as a DATE just, from where you can get the info of that particular date. When you are printing LocalDate itself, it has no meaning actually. If you need to show the date in any particular format, convert it into String, which you can do already.
Unable to Convert String to localDate with custom pattern
A date has no format. Therefore when you write
//expected output is 14.06.2020 but getting a LocalDate formatted 2020-06-14
your expectation is simply wrong. The date is parsed according to the formatter but how the date represents the parsed values and how it chooses to display them afterwards no longer has any connection to the formatter.
The only way to get your format back is to write parsedDate.format(formatter)
once again and you are back where you started, which is what formattedDate
was already.
Format date from String to LocalDate
You don't use the specified format for parsing, you use it to format the parsed date.
LocalDate.parse(mydate)
… uses the default ISO_LOCAL_DATE
format. You are looking for this overload:
LocalDate.parse(mydate, DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"))
This method uses the specified format for parsing string to date. See this code run at Ideone.com.
Note that you are using LocalDate
, meaning it will throw away the time part, keeping only the date after parsing. You probably meant to use LocalDateTime
.
When does Spring parse String to LocalDate?
and I have a service that takes LocalDate as an input.
If by "service" you mean a Spring Boot application with and endpoint mapping for the given URL, I would instead register a converter to convert the String value to a LocalDate:
public class CustomStringToLocalDateConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String from) {
return "today".equalsIgnoreCase(from) ? LocalDate.now() : LocalDate.parse(from);
}
}
and to register the converter:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new CustomStringToLocalDateConverter());
}
}
String Date to LocalDate conversion
Your date time format does not match the input date:
- Your format expects a date in the
dd/MM/yyyy hh:mm
pattern:26/08/2019 09:46
. - Your date string is a date in the
dd MMM yyyy hh:mm:ss,SSS
pattern:26 Aug 2019 09:46:09,469
The Java DateTimeFormatter
documentation has more information on the patterns you can use: https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Edit: If you then want to output the local date in a different format, create a second DateTimeFormatter
and pass it to the format
method.
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DateStringUtil {
public static void main(String[] args) {
DateTimeFormatter inputDtf = DateTimeFormatter.ofPattern("dd MMM yyyy HH:mm:ss,SSS");
String date = "26 Aug 2019 09:46:09,469";
LocalDateTime ld = LocalDateTime.parse(date, inputDtf);
DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
String outputDate = ld.format(outputDtf);
}
}
How to parse string (such as Sunday, July 4, 2021) to LocalDate?
Try it like this.
String s = "Sunday, July 4, 2021";
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("EEEE, LLLL d, yyyy");
LocalDate ld = LocalDate.parse(s, dtf);
System.out.println(ld);
prints
2021-07-04
Read up on DateTimeFormatter if you want to change the output format.
Note: If the day of the week is wrong for the numeric day of the month, you will get a parsing error. To avoid this, just skip over or otherwise ignore the day of the week.
Related Topics
Differencebetween Iterator and Iterable and How to Use Them
Why Can Final Object Be Modified
What Is the Best/Simplest Way to Read in an Xml File in Java Application
How to Specify the Root Context in Your <Web-App> Tags in Web.Xml
How to Format Localdate to String
Build Project into a Jar Automatically in Eclipse
How to Stop Selenium from Creating Temporary Firefox Profiles Using Web Driver
Why Isn't This Code Causing a Concurrentmodificationexception
Java.Lang.Verifyerror: Expecting a Stackmap Frame at Branch Target Jdk 1.7
Generate Uml Class Diagram from Java Project
How to Get a Spring Bean in a Servlet Filter
Output an Image File from a Servlet
Sessions in Struts2 Application
Using Locales with Java's Tolowercase() and Touppercase()
Maven: How to Change Path to Target Directory from Command Line