Article View: borland.public.jbuilder.java.language
Article #18716Re: SimpleDateFormat
From: Conrad Hoesle-Kienzlen
Date: Sun, 03 May 1998 00:00
Date: Sun, 03 May 1998 00:00
69 lines
3229 bytes
3229 bytes
Gregory,
regard the following code snippet:
---------- snip ---------
String datestr = "23011998";
GregorianCalendar gcal = new
GregorianCalendar(Integer.parseInt(datestr.substring(4)),
Integer.parseInt(datestr.substring(2,4)),
Integer.parseInt(datestr.substring(0,2)));
System.out.println("String to parse is: "+datestr);
System.out.println("1. Date is: "+gcal.get(Calendar.DATE)+"."+
gcal.get(Calendar.MONTH)+"."+
gcal.get(Calendar.YEAR));
SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
System.out.println("SimpleDateFormat-Pattern: "+sdf.toPattern());
System.out.println("String to parse is: 25.06.1993");
System.out.println("2. Date is: "+sdf.parse("25.06.1993"));
sdf.applyPattern("yyyy MM dd");
System.out.println("SimpleDateFormat-Pattern: "+sdf.toPattern());
System.out.println("String to parse is: 1999 11 03");
System.out.println("3. Date is: "+sdf.parse("1999 11 03"));
sdf.applyPattern("yyyyMMdd");
System.out.println("SimpleDateFormat-Pattern: "+sdf.toPattern());
System.out.println("String to parse is: 20010312");
System.out.println("3. Date is: "+sdf.parse("20010312"));
---------- snap ---------------
If you look at the console output, only the first method, using a
GregorianCalendar object gives reliable results. The SimpleTimeFormat is
buggy as hell, the second date is at least correct if you take the time
zone into account, whereas the third one is completely wrong and the
fourth throws a NullPointerException because the Java geniusses never
dreamt of somebody being weird enough to use a timestamp that doesn't
have some kind of delimiter. That 3/4 of the IT industry uses such a
date format is immaterial to them.
Question is, to whom do we complain?
...Conrad
Gregory S. Lipman wrote:
> Hi
> This question probably was asked before but I missed it
> I am using Jbuilder 1 with patch
> I do not know if it's relevant though.
>
> I have a string like "01011996"
> I need to convert it to Date.
> In SimpleDateFormat documentation there is a code example :
>
> String str = new String();
> str = "01011960";
> SimpleDateFormat formatter = new SimpleDateFormat("MMddyyyy");
> ParsePosition pos = new ParsePosition(0);
> Date dt = formatter.parse(str,pos);
>
> It produces NullPointerException !!!
> What am I doing wrong.
> Is there other wat from "01011960" to Date in JDK 1.1
>
> Thanks a lot,
> Gregory
--
Conrad Hoesle-Kienzlen
CHK IT-Consulting
<http://ourworld.compuserve.com/homepages/conrad_hoesle>
Technical Details
Message-ID:
<354CCAA6.E6FFB3DF@csi.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<354BE8B5.3E72@concentric.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,b55d91ab529bfca5 X-Google-Attributes: gidf74b3,public From: Conrad Hoesle-Kienzlen <Conrad_Hoesle@csi.com> Subject: Re: SimpleDateFormat Date: 1998/05/03 Message-ID: <354CCAA6.E6FFB3DF@csi.com> X-Deja-AN: 349894447 Content-Transfer-Encoding: 7bit References: <354BE8B5.3E72@concentric.net> Content-Type: text/plain; charset=us-ascii Organization: CHK IT-Consulting Mime-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language