how-to

Conversion between types

How to convert string to date

For this example you need to use:

import java.text.*;

For conversion is used SimpleDateFormat class, that specifies used date mask.

/* - value for conversion */

String s = "20.10.2012";

/* - 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 ) {}
The output is:
20.10.2012