• Whenever are having a requirement of converting string (date as string ) in to date we need to use SimpledateFormat class which is present in java.text package.
  • To convert string to date in java we need to follow two steps.
  • First we need to create SimpledateFormat class object by passing pattern of date which we want as a string.
  • Ex: SimpleDateFormat  formatter = new SimpleDateFormat("dd/MM/yyyy");
  • And next we need to call parse() method on the object of  SimpleDateFormat then it will returns Date object.
  • parse() method  throws ParseException so need to handle this exception.
  • By using format method of the object will get string format of given date
  • formatter.format(date).
  • How to convert java string to date object 
  • Java SimpleDateFormat String to Date conversion and parsing examples
Letter Date or Time Component Presentation Examples
G Era designator Text AD
y Year Year 2015; 15
Y Week year Year 2009; 09
M Month in year Month July; Jul;07
w Week in year Number 25
W Week in month Number 3
D Day in year Number 143
d Day in month Number 37
F Day of week in month Number 1
E Day name in week Text Tuesday; Tue
u Day number of week (1 = Monday, ..., 7 = Sunday) Number 1
a Am/pm marker Text PM
H Hour in day (0-23) Number 0
k Hour in day (1-24) Number 12
K Hour in am/pm (0-11) Number 0
h Hour in am/pm (1-12) Number 12
m Minute in hour Number 30
s Second in minute Number 30
S Millisecond Number 778
z Time zone General time zone Pacific Standard Time; PST; GMT-08:00
Z Time zone RFC 822 time zone -0800
X Time zone ISO 8601 time zone -08; -0800; -08:00
(Ref: Oracle DOCS)

Example program on SimpleDateFormat :

1. Convert string to date in java in MMM dd, yyyy format

Input : Jan 1, 2015 (MMM dd, yyyy):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6. SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy");
  7. String dateInString = "Jan 1, 2015"; 
  8.  
  9. try{
  10.  
  11. Date date = formatter.parse(dateInString);
  12. System.out.println(date);
  13. System.out.println(formatter.format(date));  
  14.  
  15. }catch(ParseException e){
  16. e.printStackTrace();
  17. }
  18. }

  19. Output: 
  20. Thu Jan 01 00:00:00 IST 2015
  21. Jan 01, 2015




2.How to convert string into date format in java day MMM dd, yyyy format example

 Input : Sat, Feb 14 2015 (E, MMM dd yyyy):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6.  
  7. SimpleDateFormat formatter = new SimpleDateFormat("E, MMM dd yyyy");
  8. String dateInString = "Sat, February 14 2015"; 
  9.  
  10. try{
  11.  
  12. Date date = formatter.parse(dateInString);
  13. System.out.println(date);
  14. System.out.println(formatter.format(date));  
  15.  
  16. }catch(ParseException e){
  17. e.printStackTrace();
  18. }
  19. }
  20. }
  21. Output: 
  22. Sat Feb 14 00:00:00 IST 2015
  23. Sat, Feb 14 2015



3. Convert string to date in java in dd/MM/ yyyy format example program

Input : 01/01/2015 (dd/MM/yyyy):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6.  
  7. SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy);
  8. String dateInString = "01/01/2015";
  9.  
  10. try{
  11.  
  12. Date date = formatter.parse(dateInString);
  13. System.out.println(date);
  14. System.out.println(formatter.format(date)); 
  15.  
  16. }catch(ParseException e){
  17. e.printStackTrace();
  18. }
  19. }

  20. Output: 
  21. Thu Jan 01 00:00:00 IST 2015
  22. 01/01/2015


4. Convert string to date in java in dd-MMM-yyyy format example program

Input : 1-Jan-2015 (dd-MMM-yyyy):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6. SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy");
  7. String dateInString = "1-Jan-2015"; 
  8.  
  9. try{
  10.  
  11. Date date = formatter.parse(dateInString);
  12. System.out.println(date);
  13. System.out.println(formatter.format(date));  
  14.  
  15. }catch(ParseException e){
  16. e.printStackTrace();
  17. }
  18. }

  19. Output: 
  20. Thu Jan 01 00:00:00 IST 2015
  21. 01-Jan-2015


  5. Convert string to date in java in yyyyMMdd format example program

Input : 20150101 (yyyyMMdd):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6.  
  7. SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
  8. String dateInString = "20150101"; 
  9.  
  10. try{
  11.  
  12. Date date = formatter.parse(dateInString);
  13. System.out.println(date);
  14. System.out.println(formatter.format(date));  
  15.  
  16. }catch(ParseException e){
  17. e.printStackTrace();
  18. }
  19. }

  20. Output: 
  21. Thu Jan 01 00:00:00 IST 2015
  22. 20150101


6.Convert string to date in java in ddMMyyyy format example program

Input : 01012015 (ddMMyyyy):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6.  
  7. SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyy");
  8. String dateInString = "01012015"; 
  9.  
  10. try{
  11.  
  12. Date date = formatter.parse(dateInString);
  13. System.out.println(date);
  14. System.out.println(formatter.format(date));  
  15.  
  16. }catch(ParseException e){
  17. e.printStackTrace();
  18. }
  19. }

  20. Output: 
  21. Thu Jan 01 00:00:00 IST 2015
  22. 01012015


7.Convert string to date in java in dd-MMM-yy format example program

Input : 01-January-15 (dd-MMM-yy):

  1. package com.instanceofjava;
  2.  
  3. public Class DateFormatDemo{ 
  4.  
  5. public static void main (String args[]) {
  6.  
  7. SimpleDateFormat formatter = new SimpleDateFormat("dd-MMMM-yy");
  8. String dateInString = "01-January-2015"; 
  9.  
  10. try{
  11.  
  12. Date date = formatter.parse(dateInString);
  13. System.out.println(date);
  14. System.out.println(formatter.format(date));  
  15.  
  16. }catch(ParseException e){
  17. e.printStackTrace();
  18. }
  19. }

  20. Output: 
  21. Thu Jan 01 00:00:00 IST 2015
  22. 01012015


8.Convert string to date in java in dd-M-yyyy HH:mm:SS format example program

 
convert string to date in java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu