Java String to Date toDate(final String dateString, final String format)

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

Description

to Date

License

Open Source License

Declaration

public static Date toDate(final String dateString, final String format) throws ParseException 

Method Source Code

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

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Map;

import java.util.concurrent.ConcurrentHashMap;

public class Main {
    private static final Map<String, SimpleDateFormat> sdfs = new ConcurrentHashMap<String, SimpleDateFormat>();

    public static Date toDate(final String dateString, final String format) throws ParseException {
        if (dateString == null || dateString.isEmpty()) {
            return null;
        }//  w  w  w .j  a va2 s  . co m

        final SimpleDateFormat sdf = getSimpleDateFormat(format);

        Date date = null;
        if (dateString != null) {
            date = sdf.parse(dateString);
        }

        return date;
    }

    public static boolean isEmpty(final String string) {
        return string == null || string.trim().length() == 0;
    }

    private static SimpleDateFormat getSimpleDateFormat(final String format) {
        SimpleDateFormat sdf = sdfs.get(format);
        if (sdf == null) {
            sdf = new SimpleDateFormat(format);
            sdfs.put(format, sdf);
        }
        return sdf;
    }
}

Related

  1. timeToDate(String time)
  2. toDate(DateFormat format, String value)
  3. toDate(final Long date, final String pattern)
  4. toDate(final String _text, final String _dateFormat)
  5. toDate(final String date, final String time)
  6. toDate(final String dateString, final String pattern)
  7. toDate(int date, String timeFormat)
  8. toDate(long value, String format)
  9. toDate(Map filter, String field)