cancel
Showing results for 
Search instead for 
Did you mean: 

Format string like Date

Former Member
0 Kudos

All,

I have a string with value say 20061030, I want to convert the value into 10/30/2006 (MM/DD/YYYY)

How can I do that.

Thanks...

BM

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Bharathi,

You can try this code...

//Convert String to Date object

 import java.text.SimpleDateFormat;  
 import java.util.Date;  
  
  
 public class DateFormatTest  
  {   
   public static void main (  String [  ]  cmdArgs  )   
                             throws Exception  
    {   
     SimpleDateFormat sdfInput =  
        new SimpleDateFormat (  "yyyy-MM-dd"  ) ;  
     SimpleDateFormat sdfOutput =  
        new SimpleDateFormat  (  "MM/dd/yyyy"  ) ;  
     String textDate = "2001-01-04";  
  
  
     Date date = sdfInput.parse (  textDate  ) ;  
     System.out.println (  sdfOutput.format (  date  )   ) ;  
    }  // main  
  }  // class DateFormatTest 

Thanks and Regards

Avijit

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Bahrati,

Since your string is in the format yyyyMMdd (20061030)

In the above code use the below line

SimpleDateFormat sdfInput =

new SimpleDateFormat ("yyyyMMdd" ) ;

instead

SimpleDateFormat sdfInput =

new SimpleDateFormat ( "yyyy-MM-dd" ) ;

Former Member
0 Kudos

Hi,

please try to replace String textDate = "2001-01-04" by 20010104 and try the above sample code.

Thanks and Regards

Avijit