How to convert String to Date object in java?

String to Date conversion is very critical when we have String of date and we need to use this as a Date object. Java have DateFormat and SimpleDateFormat objects which are helpful in parsing and formatting date or date text. Parsing means parsing the text to Date and formatting consists of Date to text conversion. SimpleDateFormat allows user defined patterns for date and time formatting. Lets see how these patterns are specified and used .

Lets consider first simple pattern "dd/MM/yyyy" which corresponds to 30/12/2011 where 30 is day , 12 is month and 2011 is a year.






















Letter Date/Time Component Example Usage
d Day In Month 22
M Month in a year 8
y Year 2010



Following code snippet shows how we can use it.


DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
DateFormat dateFormat2 = new SimpleDateFormat("dd-MM-yyyy");
try{
Date date = dateFormat.parse("30/12/2011");
System.out.println( dateFormat2.format( date ));
}catch( Exception es ){

}

Output :

30-12-2011

Now lets see how we can use other options as well






















Letter Date/Time Component Example Usage
D Day In Year 277
F Day of a week in a month 5
E Day in a week 2010


Lets see how we use it ,


dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat2 = new SimpleDateFormat("D d F E");
try{
Date date = dateFormat.parse("30/12/2011");
System.out.println( dateFormat2.format( date ));
}catch( Exception es ){

}


Output :

364 30 5 Fri


We can use it for time settings as well , lets see how we can use it

























































Letter Date/Time Component Example Usage
a AM/PM display PM
h Hour in AM/PM ( 1-12 ) 5
H Hour in 24 hours ( 0-23 ) 0
k Hour in 24 hours ( 1-24 ) 24
K Hour in 12 hours ( 0-11 ) 0
m Minute in 1 hour 59
s Seconds in one minute 59
S Milliseconds 59
z Timezone Pacific Standard Time , PST
Z Timezone as per RFC 822 -0800


Following code snippet shows usage of patterns for time :


dateFormat = new SimpleDateFormat("hh:mm:ss a 'o''clock' MMMM dd yyyy");
dateFormat2 = new SimpleDateFormat("hh:mm:ss a 'o''clock' MMMM dd yyyy");
try{
Date date = dateFormat.parse("04:19:24 PM o'clock August 17 2011");
System.out.println( dateFormat.format( date ));
}catch( Exception es ){

}

dateFormat = new SimpleDateFormat("'The time is with milliseconds : 'hh:mm:ss SS a ");
try{

System.out.println( dateFormat.format( new Date() ));
}catch( Exception es ){

}


dateFormat = new SimpleDateFormat("'The timezone and Era : 'zzzz zz Z G ");
try{
System.out.println( dateFormat.format( new Date() ));
}catch( Exception es ){

}


Output :

04:19:24 PM o'clock August 17 2011
The time is with milliseconds : 06:59:52 50 PM
The timezone and Era : Pacific Standard Time PST -0800 AD
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment