Convert String to LocalDate

16.5k Views Asked by At

I want to convert 21022019 to 2019-02-21, but some reason I am not able to convert.

import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormat;

public class StringToLocalDate {
    public static void main(String[] args) {
        System.out.println(convert("21022019"));
    }

    static LocalDate convert(String date) {
        LocalDateTime ldt;
        ldt = LocalDateTime.parse(date, DateTimeFormat.forPattern("YYYY-MM-dd"));
        return LocalDateTime.now().toLocalDate();
    }
} 
5

There are 5 best solutions below

0
On BEST ANSWER

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"));
    }
1
On

100% working code

   LocalDate localDate = LocalDate.parse("Wed, 21 Jan 2021 10:24 AM", DateTimeFormatter.ofPattern("EE, d MMM yyyy hh:mm a"));
2
On

Seems 21022019 is 2019 year, Febrary, 21nd day, try:

return LocalDate.parse(date, DateTimeFormat.forPattern("ddMMyyyy"))
2
On

You can use SimpleDateFormat like below:

import java.text.*;

...//your class

String pattern = "yyyy-MM-dd";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

String date = simpleDateFormat.format(new Date());
System.out.println(date);

Reference: http://tutorials.jenkov.com/java-internationalization/simpledateformat.html

0
On

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));
    }
}