Showing posts with label Date. Show all posts

How to convert String to Date in Java?

In our day-to-day programming we deal with different types of date format. And converting String date to Java date is one of the regular task we all do. Sometimes its hard for us to find the exact date pattern for our date. So I thought of putting all possible date format and its pattern in one article.

Following example covers all major date format pattern. However If I missed any important date pattern then do comment in article and I'll include it. Here in example, there is Date in String format with its exact pattern to convert it into java.util.Date.

Note: There are N number of possible date formats and patterns. Here in example I gave you standard formats, you can make your own format from this example like (25-12-2015 12 - dd-MM-yyyy HH), etc...

Source code (StringToDateExample.java)
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Example: String to Date in Java
 * @author javaQuery
 * @date 9th December, 2015
 * @Github: https://github.com/javaquery/Examples
 */
public class StringToDateExample {

    public static void main(String[] args) throws ParseException {
        /**
         * parseDate(StringDate, PatternToParse) 
         */
        parseDate("20151225", "yyyyMMdd");
        parseDate("2015.12.25", "yyyy.MM.dd");
        parseDate("2015-12-25", "yyyy-MM-dd");
        parseDate("2015/12/25", "yyyy/MM/dd");
        
        parseDate("25122015", "ddMMyyyy");
        parseDate("25.12.2015", "dd.MM.yyyy");
        parseDate("25-12-2015", "dd-MM-yyyy");
        parseDate("25/12/2015", "dd/MM/yyyy");

        parseDate("25/12/2015 15:10:05", "dd/MM/yyyy HH:mm:ss");        
        parseDate("2015-12-25 15:10:05", "yyyy-MM-dd HH:mm:ss");
        parseDate("2015-12-25 15:10", "yyyy-MM-dd HH:mm");
        parseDate("2015-12-25T15:20:25Z", "yyyy-MM-dd'T'HH:mm:ss'Z'");
        parseDate("2015-12-25 15:10Z", "yyyy-MM-dd HH:mm'Z'");
        parseDate("2013-12-25T15:25:30-05:00", "yyyy-MM-dd'T'HH:mm:ssXXX");
        parseDate("2015-06-26T05:27:05.000Z", "yyyy-MM-dd'T'HH:mm:ss.'000Z'");
    }

    /**
     * Method to parse StringDate to {@link Date}.
     * @param strDate
     * @param pattern
     * @return {@link Date}
     */
    public static Date parseDate(String strDate, String pattern) {
        /* Return object of Date */
        Date date = null;
        /* Get DateFormatter for given pattern */
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            /* Parse String to java.util.Date */
            date = dateFormat.parse(strDate);
        } catch (ParseException ex) {
            ex.printStackTrace();
            date = null;
        }
        /* Print the StringDate, Pattern and converted java Date */
        System.out.println("Date: " + strDate + ", Pattern: " + pattern + " -> java.util.Date[" + date + "]");
        return date;
    }
}

Output
Date: 20151225, Pattern: yyyyMMdd -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 2015.12.25, Pattern: yyyy.MM.dd -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 2015-12-25, Pattern: yyyy-MM-dd -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 2015/12/25, Pattern: yyyy/MM/dd -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 25122015, Pattern: ddMMyyyy -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 25.12.2015, Pattern: dd.MM.yyyy -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 25-12-2015, Pattern: dd-MM-yyyy -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 25/12/2015, Pattern: dd/MM/yyyy -> java.util.Date[Fri Dec 25 00:00:00 IST 2015]
Date: 25/12/2015 15:10:05, Pattern: dd/MM/yyyy HH:mm:ss -> java.util.Date[Fri Dec 25 15:10:05 IST 2015]
Date: 2015-12-25 15:10:05, Pattern: yyyy-MM-dd HH:mm:ss -> java.util.Date[Fri Dec 25 15:10:05 IST 2015]
Date: 2015-12-25 15:10, Pattern: yyyy-MM-dd HH:mm -> java.util.Date[Fri Dec 25 15:10:00 IST 2015]
Date: 2015-12-25T15:20:25Z, Pattern: yyyy-MM-dd'T'HH:mm:ss'Z' -> java.util.Date[Fri Dec 25 15:20:25 IST 2015]
Date: 2015-12-25 15:10Z, Pattern: yyyy-MM-dd HH:mm'Z' -> java.util.Date[Fri Dec 25 15:10:00 IST 2015]
Date: 2013-12-25T15:25:30-05:00, Pattern: yyyy-MM-dd'T'HH:mm:ssXXX -> java.util.Date[Thu Dec 26 01:55:30 IST 2013]
Date: 2015-06-26T05:27:05.000Z, Pattern: yyyy-MM-dd'T'HH:mm:ss.'000Z' -> java.util.Date[Fri Jun 26 05:27:05 IST 2015]