How do I implement the Java string to date method

  • 2020-04-01 01:09:22
  • OfStack

Returns a String type for JSON

Format it twice, for example:
Java code

 
String s = "2012-08-25"; 
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); 
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy years M month d day "); 
try { 
System.out.println(sdf2.format(sdf1.parse(s))); 
} catch (ParseException e) { 
// TODO Auto-generated catch block 
//www.heatpress123.net 
e.printStackTrace(); 
} 

Output: August 25, 2012
Java code
 
String str = "2012/08/25"; 
SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy/MM/dd"); 
SimpleDateFormat sdf4 = new SimpleDateFormat("yyyy years MM month dd day "); 
try { 
System.out.println(sdf4.format(sdf3.parse(str))); 
} catch (ParseException e) { 
// TODO Auto-generated catch block 
e.printStackTrace(); 
} 

Output: August 25, 2012


Related articles: