Uday Kiran
Uday Kiran

Reputation: 612

Convert String to LocalDate

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

Upvotes: 5

Views: 19927

Answers (5)

sarjeet singh
sarjeet singh

Reputation: 541

100% working code

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

Upvotes: 0

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

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

Upvotes: 6

xingbin
xingbin

Reputation: 28269

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

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

Upvotes: 3

Eray Balkanli
Eray Balkanli

Reputation: 7960

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

Upvotes: 1

Naya
Naya

Reputation: 870

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

Upvotes: 5

Related Questions