Java Date Parse getDateFromString(String dateString, String format)

Here you can find the source of getDateFromString(String dateString, String format)

Description

get Date From String

License

Open Source License

Declaration

public static Date getDateFromString(String dateString, String format) throws ParseException 

Method Source Code

//package com.java2s;
/**//  www .java  2s  .c  om
* License: https://github.com/votingsystem/votingsystem/wiki/Licencia
*/

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class Main {
    private static final DateFormat isoDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
            Locale.getDefault());

    public static Date getDateFromString(String dateString) throws ParseException {
        if (dateString.endsWith("Z"))
            return isoDateFormat.parse(dateString);
        else {
            DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            return formatter.parse(dateString);
        }
    }

    public static Date getDateFromString(String dateString, Locale locale) throws ParseException {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", locale);
        formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
        return formatter.parse(dateString);
    }

    public static Date getDateFromString(String dateString, String format) throws ParseException {
        DateFormat formatter = new SimpleDateFormat(format);
        return formatter.parse(dateString);
    }
}

Related

  1. getDateFromString(String dateStr, String datePattern)
  2. getDateFromString(String dateString)
  3. getDateFromString(String dateString)
  4. getDateFromString(String dateString)
  5. getDateFromString(String dateString, String format)
  6. getDateFromString(String input, String format)
  7. getDateFromString(String iso, String format)
  8. getDateFromString(String string)
  9. getDateFromString(String stringDate, String format)