Saturday, November 12, 2011

Play with String and Date

String m= "20111111";
String m2= "121222";
        
SimpleDateFormat mDateFormatFR = new SimpleDateFormat("yyyymmdd"); 
SimpleDateFormat mDate = new SimpleDateFormat("dd-MM-yy ");
       
 
SimpleDateFormat mDateFormatFR2 = new SimpleDateFormat("HHmmss");
SimpleDateFormat mTime = new SimpleDateFormat("HH:mm:ss");
       
//Convert String to Date
Date x = mDateFormatFR.parse(m);

//Convert Date to String with format dd-MM-yy
String x2 = mDate.format(x);
        
//Convert String to Date
Date x3 = mDateFormatFR2.parse(m2);

//Convert Date to String with format HH:mm:ss
String x4 = mTime.format(x3);
        
System.out.println("x :" + x);
System.out.println("x2 :" + x2);
System.out.println("x3 :" + x3);
System.out.println("x4 :" + x4);

Output:
x :Tue Jan 11 00:11:00 SGT 2011
x2 :11-01-11
x3 :Thu Jan 01 12:12:22 SGT 1970
x4 :12:12:22

Post a Comment
Related Posts Plugin for WordPress, Blogger...