Pages

Thursday, May 27, 2010

Converting String to a date object

When date is input in a form, at the server end it is received as a String, so numerous times it happens that the Date String has to be converted into the Date Object. Here is the code:


01.package org.kodejava.example.java.util;
02. 
03.import java.text.DateFormat;
04.import java.text.SimpleDateFormat;
05.import java.text.ParseException;
06.import java.util.Date;
07. 
08.public class StringToDate
09.{
10.public static void main(String[] args)
11.{
12.DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
13. 
14.try
15.{
16.Date today = df.parse("20/12/2005");           
17.System.out.println("Today = " + df.format(today));
18.} catch (ParseException e)
19.{
20.e.printStackTrace();
21.}
22.}
23.}

No comments:

Post a Comment