Kotlin DateTimeParseException
You do not need any formatter to parse your input string
You input string, 2006-03-25T10:30:00+12:00
is already in the default format used by OffsetDateTime#parse
and therefore, you do not need to use a formatter explicitly in order to parse your input date-time string.
m
specifies the minute while M
specifies the month
You have wrongly used m
for the month for which the symbol is M
. Check the documentation page to learn more about these symbols.
Prefer u
to y
y
specifies the year-of-era (era is specified as AD
or BC
) and is always a positive number whereas u
specifies the year which is a signed (+/-) number. Normally, we do not use +
sign to write a positive number but we always specify a negative number with a -
sign. The same rule applies for a year. As long as you are going to use a year of the era, AD
(which is mostly the case), both, y
and u
will give you the same number. However, the difference occurs when you use a year of the era, BC
e.g. the year-of-era, 1 BC
is specified as year, 0
; the year-of-era, 2 BC
is specified as year, -1
and so on. You can understand it better with the following demo:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class Testing {
public static void main(String[] args) {
System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
System.out.println(LocalDate.of(-1, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
System.out.println();
System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
System.out.println(LocalDate.of(0, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
System.out.println();
System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("u M d")));
System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("y M d")));
System.out.println(LocalDate.of(1, 1, 1).format(DateTimeFormatter.ofPattern("yG M d")));
}
}
Output:
-1 1 1
2 1 1
2BC 1 1
0 1 1
1 1 1
1BC 1 1
1 1 1
1 1 1
1AD 1 1
Note: I've used Java to demonstrate the solution but it will also work in Kotlin.
The final solution:
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
OffsetDateTime odt = OffsetDateTime.parse("2006-03-25T10:30:00+12:00");
System.out.println(odt);
// Format it into the desired pattern
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd, MM, uuuu", Locale.US);
String formatted = dtf.format(odt);
System.out.println(formatted);
}
}
Output:
2006-03-25T10:30+12:00
25, 03, 2006
Learn more about the modern date-time API from Trail: Date Time.
- Your formatter does not match the input format. Basically you need two formatters: one for input and one for output.
- The format "dd, mm, yyyy" is wrong: mm stands for minute of hour, not for month. You should use "dd, MM, yyyy".
val launchDate = "2006-03-25T10:30:00+12:00"
val inputFormatter = DateTimeFormatter.ISO_DATE_TIME
val myDate = LocalDate.parse(launchDate, inputFormatter)
val outputFormatter = DateTimeFormatter.ofPattern("dd, MM, yyyy", Locale.US)
println(outputFormatter.format(myDate))