IllegalArgumentException when using joda-time to convert string to date - java

I have a code as below to convert a string to date:
DateTimeFormatter formatter = org.joda.time.format.DateTimeFormat.forPattern("MM/dd/yyyy");
LocalDate checkInDate = LocalDate.parse("08/25/2015");
But when i run the code there is an error:
java.lang.IllegalArgumentException: Invalid format: "08/25/2015" is malformed at "/25/2015"
Anyone can help me resolve this problem.

Try using the formatter:
LocalDate checkInDate = formatter.parseLocalDate("08/25/2015");

Related

Json LocalDateTIme issue

Exception during serialization - message:
Cannot deserialize value of type `java.time.LocalDateTime` from String "2021-05-04T09:06:27-05:00":
Failed to deserialize java.time.LocalDateTime:
(java.time.format.DateTimeParseException) Text '2021-05-04T09:06:27-05:00'
#JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss", shape = JsonFormat.Shape.STRING )
#JsonProperty("inprogress_ts")
val inProgressTs: LocalDateTime
Why can't this #JsonFormat pattern parse the String "2021-05-04T09:06:27-05:00"?
It does not work because you are using a non suitable data type / class:
A LocalDateTime does not consider offsets, it simply has none and cannot parse Strings containing an offset.
Your String contains -05:00, which will cause the DateTimeParseException being thrown.
Use a suitable class: An OffsetDateTime instead.
The string your are passing hasnĀ“t got the same format than JsonFormat:
2021-05-04 T 09:06:27 -05:00
yyyy-MM-dd 'T' HH:mm:ss ??????
If you include zone then it should work. I have written a sample extension function in kotlin may help
fun String.convertDateString(): String {
val dateFormat = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ", Locale.ENGLISH)
val date: Date = dateFormat.parse(this) ?: Date()
val outFormat = SimpleDateFormat("MMMM dd, yyyy", Locale.ENGLISH)
return outFormat.format(date)
}
Input: "2021-05-04T09:06:27-05:00"
Formatted Response: "May 04, 2021".
Actual date : 2021-05-04T16:06:27.000+0200 (GMT+2:00 time zone)
I have formatted like below and then converted manually
#JsonProperty("event_ts")
val packedCentralTime = eventTs.withZoneSameInstant(ZoneId.of("America/Chicago")).toLocalDateTime()

LGoodDatePicker issues with getDate()

I'm using the LGoodDatePicker with Apache NetbeansIDE 12.2 (https://github.com/LGoodDatePicker/LGoodDatePicker), and I need to get the date in the format YYYY-MM-DD. I'm using this code:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String date = sdf.format(datePicker1.getDate());
But I get this error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Cannot format given Object as a Date
Any suggestions? Thank you.
The method getDate() of this DatePicker returns a java.time.LocalDate, not a java.util.Date. That's actually what the error message tells you, it expects a java.util.Date but got something else.
That means you shouldn't try to format it using a java.text.SimpleDateFormat, use a java.time.format.DateTimeFormatter here:
String date = datePicker1.getDate().format(DateTimeFormatter.ISO_LOCAL_DATE);
or define a custom pattern using the method ofPattern(String pattern) of the DateTimeFormatter:
String date = datePicker1.getDate().format(DateTimeFormatter.ofPattern("uuuu-MM-dd");
In this very case, you can even use the toString() method of the LocalDate in order to get a String in the desired format:
String date = datePicker1.getDate().toString();

"How to format LocalDate with Custom Pattern" in Java

I am new to JodaTime and learning it to my own. Actually what I want is to just format the LocateDate in my own format. My format is "dd-mm-yyyy"
I have method that calculates the difference with two dates:
private void sampleDaysDifference() {
DateTime todayDate = getLocalTodayDate();
DateTime dateAfterTwoDays = getDateAfterTwoDays();
//get the days difference
int differenceOfDates = Days.daysBetween(todayDate, dateAfterTwoDays).getDays();
Log.e("logX","differenceOfDates: " + differenceOfDates);
}
To get the today date I am using:
private DateTime getLocalTodayDate() {
LocalDate now = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern(DATE_FORMAT);
return fmt.parseDateTime(now.toString());//return the today date
}
and to get the date after two days:
private DateTime getDateAfterTwoDays() {
LocalDate now = LocalDate.now();
DateTimeFormatter fmt = DateTimeFormat.forPattern(DATE_FORMAT);
return fmt.parseDateTime(now.plusDays(2).toString());//return date after two days
}
The problem is that I have no idea how to format date using JodaTime, can somebody please tell me how to format a JodaTime LocalDate!
Actually my app is crashing with the stacktrace:
Caused by: java.lang.IllegalArgumentException: Invalid format: "16 January, 2019" is malformed at " January, 2019"
You are almost there but just the pattern is wrong that you specified, change "dd-mm-yyyy" to this "dd-MM-yyyy" docs-for-patterns
Simple Example
System.out.println(LocalDate.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy"))); //16-01-2019
From Joda DateTimeFormatter joda-docs
The pattern syntax is mostly compatible with java.text.SimpleDateFormat

Joda DateTime Invalid format

I'm trying to get the current DateTime with my DateTimeFormat pattern, but i'm getting the exception...
//sets the current date
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
DateTime now = dtf.parseDateTime(currentDate.toString());
I'm getting this exception, I cannot understand who is giving the malformed format
java.lang.IllegalArgumentException: Invalid format: "2017-01-04T14:24:17.674+01:00" is malformed at "17-01-04T14:24:17.674+01:00"
This line DateTime now = dtf.parseDateTime(currentDate.toString()); isn't correct because you try parse date with default toSring format. You have to parse string which formatted the same way as pattern:
DateTime currentDate = new DateTime();
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/YYYY HH:mm").withLocale(locale);
String formatedDate = dtf.print(currentDate);
System.out.println(formatedDate);
DateTime now = dtf.parseDateTime(formatedDate);
System.out.println(now);
You are using the wrong format to parse the date. If you print out the date you are trying to parse after converting it to a String with toString you get:
2017-01-04T14:24:17.674+01:00
This date string does not conform to the pattern dd/MM/YYYY HH:mm. To parse the to a string converted currentDate to a DateTime object again, you have to use the following pattern:
DateTimeFormatter dtf = DateTimeFormat.forPattern("YYYY-MM-dd'T'HH:mm:ss.SSSZ")
.withLocale(locale);
Parsing with this DateTimeFormatter will get you another instance that represents the same time as the original currentDate.
For more details on the DateTimeFormatter and it's parsing options check out the JavaDoc

DateFormat from ''yyyy-MM-dd" to "dd-MM" using Joda-Time

I have following code:
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM");
LocalDate weekMon = new LocalDate().withWeekOfWeekyear(thisWeek).withDayOfWeek(DateTimeConstants.MONDAY);
System.out.println(weekMon.toString());
LocalDate weekFin = dtf.parseLocalDate(weekMon.toString());
and the output prints the date correctly: 2015-02-02. I'm trying to convert it to European format and ignoring the year as "dd-MM" -> "02-02" but the line:
LocalDate weekFin = dtf.parseLocalDate(weekMon.toString());
keeps throwing following exception:
java.lang.IllegalArgumentException: Invalid format: "2015-02-02" is malformed at "5-02-02"
What is wrong with the formation in this case ?.
Probably what you need is to print, and not to parse.
In other words:
String dateStr = dtf.print(weekMon);
Notice that with this code:
LocalDate weekFin = dtf.parseLocalDate(weekMon.toString());
you are trying to parse a string in "yyyy-MM-dd" format using a formatter that accepts strings in "dd/MM" format. Hence the exception...

Categories

Resources