5

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();
    }
} 
xingbin
  • 27,410
  • 9
  • 53
  • 103
Uday Kiran
  • 487
  • 2
  • 9
  • 29
  • 1
    You need to tell it how to parse – Mr. Polywhirl Jan 28 '19 at 14:30
  • Do you actually not want a String back, and use LocalDate only to convert between the two formats? – Siavas Jan 28 '19 at 14:33
  • convert String to LocalDate – Uday Kiran Jan 28 '19 at 14:36
  • What goes wrong? What result do you get instead? If you get any error message, please quote it verbatim, it will tell us a lot. “Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers” (quoted from [What topics can I ask about here?](https://stackoverflow.com/help/on-topic)) – Ole V.V. Jan 28 '19 at 14:46
  • Possible duplicate of [Convert String Date to String date different format](https://stackoverflow.com/questions/14999506/convert-string-date-to-string-date-different-format) (I know that that question is about the old-fashioned `SimpleDateFormat`, but the problem/bug is very similar) – Ole V.V. Jan 28 '19 at 14:48

5 Answers5

6

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));
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
5

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"));
    }
Naya
  • 850
  • 6
  • 19
3

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

return LocalDate.parse(date, DateTimeFormat.forPattern("ddMMyyyy"))
xingbin
  • 27,410
  • 9
  • 53
  • 103
1

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

Eray Balkanli
  • 7,752
  • 11
  • 48
  • 82
  • Thank You for the Tutorial. – Uday Kiran Jan 28 '19 at 14:38
  • Please don’t teach the young ones to use the long outdated and notoriously troublesome `SimpleDateFormat` class. At least not as the first option. And not without any reservation. And especially not when the asker is already using the more modern and superior Joda-Time. – Ole V.V. Jan 28 '19 at 14:40
0

100% working code

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