If you are using Java 8, you can use the native Java Time library that was developed by the same guy (Stephen Colebourne) who created Joda time. It's pretty easy to parse and display dates in various formats.
Your main issue seems to be that you are treating your expected object as a LocalDateTime, but there is no time present. This is essentially throwing your code through a runtime error that states that you need to include time, so you should use a LocalDate instead.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
public static String DATE_FORMAT_INPUT = "ddMMyyyy";
public static String DATE_FORMAT_OUTPUT = "yyyy-MM-dd";
public static void main(String[] args) {
System.out.println(formatted(convert("21022019")));
}
public static String formatted(LocalDate date) {
return date.format(DateTimeFormatter.ofPattern(DATE_FORMAT_OUTPUT));
}
public static LocalDate convert(String dateStr) {
return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(DATE_FORMAT_INPUT));
}
}
If you need to use a Java version before 1.8, you can use the following. It is very similar.
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
public class StringToLocalDate {
public static String DATE_FORMAT_INPUT = "ddMMyyyy";
public static String DATE_FORMAT_OUTPUT = "yyyy-MM-dd";
public static void main(String[] args) {
System.out.println(formatted(convert("21022019")));
}
public static String formatted(LocalDate date) {
return date.toString(DateTimeFormat.forPattern(DATE_FORMAT_OUTPUT));
}
public static LocalDate convert(String dateStr) {
return LocalDate.parse(dateStr, DateTimeFormat.forPattern(DATE_FORMAT_INPUT));
}
}
Answer from Mr. Polywhirl on Stack OverflowI am importing from a csv file using a Scanner object and using split to get an array of strings for each row. So far, so good, so easy. I need the date in column 2 to be cast to LocalDate . I have been reading and goggling for hours but I keeping errors from LocalDate.parse I have tried using simple date formating, I have tired using DateTimeFormatter and lots of other methods I have read about. The parse keeps failing at index 0. The date is in format dd/MM/yyyy
How can I convert this to LocalDate?
****edit 1 to show code examples as people have correctly pointed out that I didnt add any (sorry)
Data sample
1,14/05/2022,James 2,21/06/2022,Iona 5,02/05/2022,Pippa
the date is coming from here
public void readFromCSV(String path) {
try (Scanner lineIn = new Scanner(Paths.get(path))) {
while (lineIn.hasNextLine()) {
String row = lineIn.nextLine();
String[] words = row.split(",");I have tried the following different methods
//Method 1
LocalDate date = LocalDate.parse(words[0]);
//Method 2
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(("YYYY/MM/DD"));
LocalDate date = LocalDate.parse(words[1],formatter);
//Method 3
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH)
LocalDate date = LocalDate.parse(words[1],formatter);
//Method 4
Date formattedDate = formattedDate.toLocalDate(words[1]);
LocalDate date = LocalDate.parse(formattedDate);
//Method 5
LocalDate date = LocalDate.parse(words[1], DateTimeFormatter.ofPattern("yyyy-MM-dd").withLocale(Locale.ENGLISH));The error I get from the catch is
error - java.time.format.DateTimeParseException: Text '14/05/2022' could not be parsed at index 0
*****Solved
words[1] = words[1].replace("/" , "-");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
LocalDate date = LocalDate.parse(words[1],formatter);Videos
If you are using Java 8, you can use the native Java Time library that was developed by the same guy (Stephen Colebourne) who created Joda time. It's pretty easy to parse and display dates in various formats.
Your main issue seems to be that you are treating your expected object as a LocalDateTime, but there is no time present. This is essentially throwing your code through a runtime error that states that you need to include time, so you should use a LocalDate instead.
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class StringToLocalDate {
public static String DATE_FORMAT_INPUT = "ddMMyyyy";
public static String DATE_FORMAT_OUTPUT = "yyyy-MM-dd";
public static void main(String[] args) {
System.out.println(formatted(convert("21022019")));
}
public static String formatted(LocalDate date) {
return date.format(DateTimeFormatter.ofPattern(DATE_FORMAT_OUTPUT));
}
public static LocalDate convert(String dateStr) {
return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern(DATE_FORMAT_INPUT));
}
}
If you need to use a Java version before 1.8, you can use the following. It is very similar.
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
public class StringToLocalDate {
public static String DATE_FORMAT_INPUT = "ddMMyyyy";
public static String DATE_FORMAT_OUTPUT = "yyyy-MM-dd";
public static void main(String[] args) {
System.out.println(formatted(convert("21022019")));
}
public static String formatted(LocalDate date) {
return date.toString(DateTimeFormat.forPattern(DATE_FORMAT_OUTPUT));
}
public static LocalDate convert(String dateStr) {
return LocalDate.parse(dateStr, DateTimeFormat.forPattern(DATE_FORMAT_INPUT));
}
}
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"));
}
SimpleDateFormat will not work if he/she is starting with LocalDate, which is new in Java 8. From what I can see, you will have to use DateTimeFormatter.
LocalDate localDate = LocalDate.now(); // For reference
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd LLLL yyyy");
String formattedString = localDate.format(formatter);
That should print 05 May 1988. To get the period (AKA full stop) after the day and before the month, you might have to use "dd'.LLLL yyyy".
It could be short as:
LocalDate.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));