Java LocalDate from String getLocalDateFromString(String sDate)

Here you can find the source of getLocalDateFromString(String sDate)

Description

get Local Date From String

License

Open Source License

Declaration

public static LocalDate getLocalDateFromString(String sDate) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static LocalDate getLocalDateFromString(String sDate) {

        if (sDate == null || sDate.trim().isEmpty()) {

            return null;
        }//from   ww w.j  a v  a  2  s .c  o  m

        DateTimeFormatter dateTimeFormatter;
        LocalDate lDate = null;

        try {

            dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            lDate = LocalDate.parse(sDate, dateTimeFormatter);
        } catch (Exception ex1) {

            try {

                dateTimeFormatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
                lDate = LocalDate.parse(sDate, dateTimeFormatter);
            } catch (Exception ex2) {

                try {

                    dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
                    lDate = LocalDate.parse(sDate, dateTimeFormatter);
                } catch (Exception ex3) {

                    try {

                        dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
                        lDate = LocalDate.parse(sDate, dateTimeFormatter);
                    } catch (Exception ex4) {

                        try {

                            dateTimeFormatter = DateTimeFormatter.ofPattern("ddMMyyyy");
                            lDate = LocalDate.parse(sDate, dateTimeFormatter);
                        } catch (Exception ex5) {

                        }
                    }
                }
            }
        }

        return lDate;
    }
}

Related

  1. getLocalDateForString(final String string)
  2. parseLocalDate(String dateString)
  3. parseLocalDate(String inPattern, String inDate)
  4. parseLocalDate(String localDate)
  5. parseStringToLocalDate(String strDate)