Tuesday, January 16, 2018

JAVA - How to convent string to date

For conversion use SimpleDateFormat class where you can specify date mask.
import java.text.*;
...
/* - value for conversion */

String s = "20.10.2017";

/* - specify format mask */

DateFormat df = new SimpleDateFormat( "dd.MM.yyyy" );

try {
  /* - try to convert */

  Date d = df.parse( s );

  /* - print result */

  System.out.println( df.format( d ) );
}
catch ( ParseException ex ) {}
Output:
20.10.2017

No comments:

Post a Comment