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

Date Format Example


import java.util.*;
import java.text.*;
class DateFormatDemo
{
public static void main(String A[])
{
//String dob="23/12/1998";
//String dob="DEC/23/1998";
String dob="1998-23-12";
//SimpleDateFormat sdf=new SimpleDateFormat("yyyy'/'dd'/'MM");
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
// Date d=null;
try{

Date d=sdf.parse(dob);//01/31/98
System.out.println("Formated Date="+d);
System.out.println("Formated Date="+sdf.format(d));
}
catch(ParseException pe)
{
System.out.println("Parse Exception Is"+pe);
}

}
}