Java SimpleDateFormat(String pattern) Constructor

Syntax

SimpleDateFormat(String pattern) constructor from SimpleDateFormat has the following syntax.

public SimpleDateFormat(String pattern)

Example

In the following code shows how to use SimpleDateFormat.SimpleDateFormat(String pattern) constructor.


/*from   ww  w  .j  av  a 2s . c  om*/
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
  public static void main(String[] args) throws Exception {
    String today = "21/12/2007";

    DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
    Date date = formatter.parse(today);
    long dateInLong = date.getTime();

    System.out.println("date = " + date);
    System.out.println("dateInLong = " + dateInLong);
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    java.text »




DateFormat
DecimalFormat
NumberFormat
SimpleDateFormat