Java Data Type How to - Parse Java date from String format








Question

We would like to know how to parse Java date from String format.

Answer

"2015-1-1" or "2015-01-01" are both working for the parse action.

import java.text.*;
/*from w  ww . j  a va  2 s  . com*/
public class Test
{

    public static void main(String args[])
    {
      try
      {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-DD");  
        System.out.println(sdf.parse(args[0]).toString());
      }
                catch(Exception e)
      {
        e.printStackTrace();
      }
    }
}

The code above generates the following result.