I am trying to convert from string to localdate(JODA TIME) but its giving me error
String theDate = w.getPSDate(); == 6/03/2013
LocalDate ld = new LocalDate(theDate);
System.out.println(ld);
for some reason, I have to use string instead of date. I want to print the date as (06/03/2013). what is the error in the code?
error
Exception in thread "main" java.lang.IllegalArgumentException: Invalid format: "06/03/2013" is malformed at "/03/2013"
at org.joda.time.format.DateTimeFormatter.parseMillis(DateTimeFormatter.java:747)
at org.joda.time.convert.StringConverter.getPartialValues(StringConverter.java:87)
at org.joda.time.LocalDate.<init>(LocalDate.java:406)
at org.joda.time.LocalDate.<init>(LocalDate.java:354)
at Date.GetDate.main(GetDate.java:94)
Java Result: 1
LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd. We can use now() method to get the current date. We can also provide input arguments for year, month and date to create LocalDate instance.
parse(CharSequence text) parse() method of a LocalDateTime class used to get an instance of LocalDateTime from a string such as '2018-10-23T17:19:33' passed as parameter. The string must have a valid date-time and is parsed using DateTimeFormatter. ISO_LOCAL_DATE_TIME.
Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.
Yes, it is: DateTimeFormat is thread-safe and immutable, and the formatters it returns are as well. Implementation Requirements: This class is immutable and thread-safe.
Use a DateTimeFormatter
instead:
// Are you sure it's 6/03/2013 rather than 06/03/2013? dd would be nicer...
DateTimeFormatter formatter = DateTimeFormat.forPattern("d/MM/yyyy");
LocalDate date = formatter.parseLocalDate(text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With