Date Format Example2


/*
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 DateFormatDemo2 {

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);

}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s