Java Date Parse convertStringToDate(String dateAsString)

Here you can find the source of convertStringToDate(String dateAsString)

Description

convert String To Date

License

Apache License

Declaration

public static Date convertStringToDate(String dateAsString) throws ParseException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static final SimpleDateFormat dateFormatBulgarian = new SimpleDateFormat("dd.MM.yyyy");
    private static final SimpleDateFormat dateFormatWestern = new SimpleDateFormat("yyyy-MM-dd");

    public static Date convertStringToDate(String dateAsString) throws ParseException {
        SimpleDateFormat dateFormat = dateFormatBulgarian;

        String dateFormatWesternPattern = "\\d{4}-\\d{2}-\\d{2}";

        if (dateAsString.matches(dateFormatWesternPattern)) {
            dateFormat = dateFormatWestern;
        }/*from   w ww.jav  a  2 s .c o  m*/

        Date date = dateFormat.parse(dateAsString);

        return date;
    }
}

Related

  1. convertStringToDate(String dateString, String dateFormat)
  2. convertStringToDateTimeNotException(String date)
  3. convertStringToDate(String dt)
  4. convertStringToDate(String date)
  5. convertStringToDate(String str, String pattern)
  6. convertStringToDate(String date)
  7. convertStringToDate(String strDate)
  8. convertStringToDate(String str_date)
  9. convertStringToDate(String aMask, String strDate)