How to parse a date in Java
In this tutorial we will show you how to parse a date in Java. To parse a date the JDK provides a SimpleDateFormat
class. You can either define your own custom date format strings or use the built in date formats. You can also provide a java.util.Locale
when parsing a date.
Parse a date
Here is a concrete example how to parse a date in java using different date formats. the SimpleDateFormat#parse(date)
method is used to parse a string into a java.util.Date
. First we use custom date format strings and afterwards we use the default date formats. We can also provide a java.util.Locale
when parsing a date in a different locale.
package com.memorynotfound.date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class ParseDate {
public static void main(String... args) throws ParseException {
System.out.println("Parse a date using custom format");
String date = "24/12/15";
DateFormat df = new SimpleDateFormat("dd/MM/yy");
Date parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "24/12/2015";
df = new SimpleDateFormat("dd/MM/yyyy");
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "2015/12/24";
df = new SimpleDateFormat("yyyy/MM/dd");
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "24 Dec, 2015";
df = new SimpleDateFormat("d MMM, yyyy");
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "24 December, 2015";
df = new SimpleDateFormat("d MMMM, yyyy");
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "24 十二月, 2015";
df = new SimpleDateFormat("d MMMM, yyyy", Locale.CHINESE);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "24 dicembre, 2015";
df = new SimpleDateFormat("d MMMM, yyyy", Locale.ITALIAN);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
System.out.println("\nParse a date using built in date formats");
date = "Dec 24, 2015";
df = DateFormat.getDateInstance(DateFormat.DEFAULT);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "12/24/15";
df = DateFormat.getDateInstance(DateFormat.SHORT);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "Dec 24, 2015";
df = DateFormat.getDateInstance(DateFormat.MEDIUM);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "December 24, 2015";
df = DateFormat.getDateInstance(DateFormat.LONG);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "Thursday, December 24, 2015";
df = DateFormat.getDateInstance(DateFormat.FULL);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "2015年12月24日 星期四";
df = DateFormat.getDateInstance(DateFormat.FULL, Locale.CHINESE);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
date = "giovedì 24 dicembre 2015";
df = DateFormat.getDateInstance(DateFormat.FULL, Locale.ITALIAN);
parsedDate = df.parse(date);
System.out.println("parsed date " + "(" + date + "): " + parsedDate);
}
}
Generated output
The previous code generates the following output.
Parsed date using custom format
parsed date (24/12/15): Thu Dec 24 00:00:00 CET 2015
parsed date (24/12/2015): Thu Dec 24 00:00:00 CET 2015
parsed date (2015/12/24): Thu Dec 24 00:00:00 CET 2015
parsed date (24 Dec, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (24 December, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (24 十二月, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (24 dicembre, 2015): Thu Dec 24 00:00:00 CET 2015
Parsed date using built in date formats
parsed date (Dec 24, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (12/24/15): Thu Dec 24 00:00:00 CET 2015
parsed date (Dec 24, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (December 24, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (Thursday, December 24, 2015): Thu Dec 24 00:00:00 CET 2015
parsed date (2015年12月24日 星期四): Thu Dec 24 00:00:00 CET 2015
parsed date (giovedì 24 dicembre 2015): Thu Dec 24 00:00:00 CET 2015