Parsing a Date Using a Custom Format

A pattern of special characters is used to specify the format of the date to parse. The same set of pattern characters are used to format and to parse dates. See Formatting a Date Using a Custom Format for a listing of some pattern characters.

Note: This example parses dates using the default locale (which, in the author's case, is Locale.ENGLISH). If the example is run in a different locale, the text (e.g., month names) will not be the same.

try {
    // Some examples
    DateFormat formatter = new SimpleDateFormat("MM/dd/yy");
    Date date = (Date)formatter.parse("01/29/02");

    formatter = new SimpleDateFormat("dd-MMM-yy");
    date = (Date)formatter.parse("29-Jan-02");

    // Parse a date and time; see also
    // Parsing the Time Using a Custom Format
    formatter = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss");
    date = (Date)formatter.parse("2002.01.29.08.36.33");

    formatter = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss Z");
    date = (Date)formatter.parse("Tue, 29 Jan 2002 22:14:02 -0500");
} catch (ParseException e) {
}

Comments

6 Apr 2011 - 6:31am by Anonymous (not verified)

Penis.

12 May 2011 - 5:18pm by Anonymous (not verified)

Vagina

23 May 2011 - 9:26pm by Anonymous (not verified)

Cu

3 Jun 2011 - 3:05pm by Anonymous (not verified)

nt

12 Jul 2011 - 3:35am by John B (not verified)

Thanks. This is only place I have seen using a non-trivial date format. It pointed out that each of the month characters needed to be represented by M.

29 Aug 2011 - 10:44am by Anonymous (not verified)

Menino Cabaço

12 Sep 2011 - 8:48pm by HotCupOfJava (not verified)

Sort, concise, but still VERY informative. More people's code should be like this. Much thanks.

27 Oct 2011 - 6:08pm by Anonymous (not verified)

Error:
java.text.ParseException: Unparseable date: "29-Jan-02"
at java.text.DateFormat.parse(Unknown Source)

11 Nov 2011 - 2:49am by asiya (not verified)

how to perform date parsing in java program by passing position and arguments as parameters

12 Dec 2011 - 6:25am by Anonymous (not verified)

Jeeeej

12 Dec 2011 - 6:27am by Anonymous (not verified)

<marquee>

24 Feb 2012 - 4:49am by arjun adhikari (not verified)

i am trying the following code can any one tell me where am i going wrong

String day=request.getParameter("day");
String month=request.getParameter("month");
String year=request.getParameter("year");
String d=request.getParameter("d");
String m=request.getParameter("m");
String y=request.getParameter("y");
String start = year+month+day;
String end = y+m+d;
String header=request.getParameter("header");
String description=request.getParameter("desc");
String connectionURL = "jdbc:oracle:thin:@localhost:1521:XE";
Connection connection = null;
PreparedStatement pre=null;
Class.forName("oracle.jdbc.OracleDriver").newInstance();
connection = DriverManager.getConnection(connectionURL, "arjun", "arjun");

try{
String pattern = "yyyy-MM-dd";
DateFormat format = new SimpleDateFormat(pattern);
Date date = (Date) format.parse(start);
Date enddate= (Date) format.parse(end);

java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now);

pre = connection.prepareStatement("INSERT INTO NROCALENDER(HEADER,DESCRIPTION,UPLOADED_TIME,ENDING_TIME,START_TIME)VALUES(?,?,?,?,?)");
pre.setString(1,header);
pre.setString(2,description);
pre.setString(3,strDateNew);
pre.setDate(4,date);
pre.setDate(5,enddate);
pre.executeUpdate();
}
catch(Exception e)
{
}
finally
{
pre.close();
connection.close();
}

24 Feb 2012 - 4:51am by arjun adhikari (not verified)

i want that my application should have a starting date and an ending date and when i query only date tht range between the end and start dates are shown

20 Apr 2012 - 12:39am by Anonymous (not verified)

How to parse the date 201404120000(yyyyMMdd) to correct date(04/12/2012).any help?

9 May 2012 - 3:02am by SUFIYAN (not verified)

AWESOME EXAMPLE I SOLVED MY PROBLEM WITH THE HELP OF THIS EXAMPLE.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.