new SimpleDateFormat('yyyy-MM-dd') : SimpleDateFormat « java.text « Java by API






new SimpleDateFormat('yyyy-MM-dd')

 
       
/*
 Output: 
 
 on Tue Oct 01 00:00:00 PDT 2013 in  Vancouver, B.C.
 on Sun Mar 01 00:00:00 PST 1248 in  Ottawa, ON
 on Mon Jun 06 00:00:00 PST 1323 in  Toronto, ON

*/       
       

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainClass {

  public static void main(String[] a) {
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    String input[] = { "2013-10-01 Vancouver, B.C.", 
                       "1248-03-01 Ottawa, ON",
                       "1323-06-06 Toronto, ON" };
    for (int i = 0; i < input.length; i++) {
      ParsePosition pp = new ParsePosition(0);
      Date d = formatter.parse(input[i], pp);
      if (d == null) {
        System.err.println("Invalid date in " + input[i]);
        continue;
      }
      String location = input[i].substring(pp.getIndex());
      System.out.println(" on " + d + " in " + location);

    }
  }
}

           
         
  








Related examples in the same category

1.new SimpleDateFormat('hh:mm:ss')
2.new SimpleDateFormat('dd MMM yyyy hh:mm:ss zzz')
3.new SimpleDateFormat('E MMM dd yyyy')
4.new SimpleDateFormat(String pattern, Locale locale)
5.SimpleDateFormat: applyPattern(String pattern)
6.SimpleDateFormat: format(Date d)
7.SimpleDateFormat: parse(String text, ParsePosition pos)
8.SimpleDateFormat: setDateFormatSymbols(DateFormatSymbols newFormatSymbols)