Date contructor deprecation : Parsing a String to Date
Hi All,
In Java 1.5 the Date() constructor Date(String s) is deprecated. As per the API Documentation DateFormat.Parse() method is used.
The following code from Java 1.4 version has to be upgraded to Java 1.5.
Existing Code:
Date dDate = new Date(sDate);
Modified Code:
DateFormat df = DateFormat.getDateInstance();
Date dDate = df.parse(sDate);
Here the DateFormat accepts a default formatting style as "Feb 01, 2007" and parses the String.
If the String sDate belongs to any other formatting style such as "01 Feb, 2007" or "01 Feb, 07" the code piece throws unparsable date error.
Please give your thougts on this issue to parse the string of any format..
Thanks,
Rajesh.
> Please give your thougts on this issue to parse the> string of any format..Can't be done. You must know the format of the date.Is 06/01/11 2006 jan 11 or 2006 nov 01, or 6 jan 2011 etc...Kaj
> Please give your thougts on this issue to parse the> string of any format..> What date is 1/2/4 ?
> Hi All,
>
> In Java 1.5 the Date() constructor Date(String s) is
> deprecated. As per the API Documentation
> DateFormat.Parse() method is used.
>
> The following code from Java 1.4 version has to be
> upgraded to Java 1.5.
>
> Existing Code:
> Date dDate = new Date(sDate);
>
> Modified Code:
> DateFormat df = DateFormat.getDateInstance();
> Date dDate = df.parse(sDate);
>
> Here the DateFormat accepts a default formatting
> style as "Feb 01, 2007" and parses the String.
>
> If the String sDate belongs to any other formatting
> style such as "01 Feb, 2007" or "01 Feb, 07" the code
> piece throws unparsable date error.
>
> Please give your thougts on this issue to parse the
> string of any format..
You can't. What date is this: "08/04/24"? 8 April, 1924? 4 August, 2024?
>
> Thanks,
> Rajesh.
Oh look - a trifecta - and I'm last.But still faster'n Jos.
Hi,
The problem here is, i dont know the format of the String.
It can be of anything as i have said earliar.
like,
Feb 01, 2007 or 01 Feb, 07 or 01 Feb, 2007..
Date constructor Date(String s) will parse the date regardless of the formatting style of the argument given.
but DateFormat didnot accepts except String in the format "Feb 01, 2007"
Thanks,
Rajesh
> Hi,> > The problem here is, i dont know the format of the> String. Yes indeed, that's a problem.
> Date constructor Date(String s) will parse the date> regardless of the formatting style of the argument> given.No it won't.> > but DateFormat didnot accepts except String in the> format "Feb 01, 2007"See above.
my doubt is , if something is present there in java 1.4 , why it cannot be achieved in Java 1.5..?..
> but DateFormat didnot accepts except String in the> format "Feb 01, 2007"> What did you not understand about the first three responses?
> my doubt is , if something is present there in java> 1.4 , why it cannot be achieved in Java 1.5..?..So what did 1.4 do with 1/2/3 ?
Date( String s) was parsing all types of formats.. during this upgrade to Java 1.5 only i am getting Unparsable error..
> > Date constructor Date(String s) will parse the
> date
> > regardless of the formatting style of the argument
> > given.
>
> No it won't.
>
A quick test shows that it does - must do some guessing based on locale and I bet there's some pretty ugly code involved (thus the reason it's deprecated)
In first three responses, you are asking about the format of the string i am supposed to parse. it can be any format other than MMM dd, yyyy.hope you ppl getting it closely..
> Date( String s) was parsing all types of formats..> during this upgrade to Java 1.5 only i am getting> Unparsable error..Just leave the deprecated ctor call in. I compiled under 1.5 and it worked as you described.
> In first three responses, you are asking about the
> format of the string i am supposed to parse. it can
> be any format other than MMM dd, yyyy.
>
> hope you ppl getting it closely..
Yes, you are doing something inherently brittle and poorly thought out. In Java a Date is just a number, converting a String to that number with any degree of certainty requires knowing the format of the String you are converting, otherwise you are doing some amount of guessing. If you are all right with that, then use the deprecated ctor call you were using and move on.
tsitha at 2007-7-21 17:09:37 >

> > > Date constructor Date(String s) will parse the
> > date
> > > regardless of the formatting style of the
> argument
> > > given.
> >
> > No it won't.
> >
>
> A quick test shows that it does - must do some
> guessing based on locale and I bet there's some
> pretty ugly code involved (thus the reason it's
> deprecated)
That's correct, it will try, but that's all. I will still not solve the problem with 07/01/02 and it will not be able to parse dates in a format of another locale.
Kaj
kajbja at 2007-7-21 17:09:37 >

> A quick test shows that it does - must do some
> guessing based on locale and I bet there's some
> pretty ugly code involved (thus the reason it's
> deprecated)
Using
Date d = new Date("1/44/3");
System.out.println(d);
with 1.4.2 results in
Thu Feb 13 00:00:00 GMT 2003
Now that really is an ugly result!
Just have a look at the code i executedString sTargetDate = "08 Feb, 07";DateFormat df = DateFormat.getDateInstance();Date dTargetDate = df.parse(sTargetDate);If i run this piece in 1.5, i get teh ParseException : Unparsable date
And what did you expect?API docs:Gets the date formatter with the default formatting style for the default localeNothing else.
> Just have a look at the code i executed
>
> String sTargetDate = "08 Feb, 07";
> DateFormat df = DateFormat.getDateInstance();
> Date dTargetDate = df.parse(sTargetDate);
>
> If i run this piece in 1.5, i get teh ParseException
> : Unparsable date
Yes, we know. Why don't you just go back to using Date dTargetDate = new Date(sTargetDate)?
tsitha at 2007-7-21 17:09:37 >

I can no longer use the deprecated Date ctor. The Application is upgraded to 1.5. Here Date ctor is deprecated and needs to be replaced by DateFormat.parse(String s).
> I can no longer use the deprecated Date ctor. The
> Application is upgraded to 1.5. Here Date ctor is
> deprecated and needs to be replaced by
> DateFormat.parse(String s).
Yes, and it was deprecated 'cause it's a BAD IDEA to use it. It was a bad design decision to ever have included it and it's been deprecated since 1.1. It was deprecated in 1.4, 1.3, 1.2 and 1.1. If you agree that it's a bad idea to try and guess the format of your date, then you'll need to specify a format. If you don't think this is a bad idea then keep using the deprecated constructor call - Sun doesn't seem to be in any hurry to remove deprecated items.
tsitha at 2007-7-21 17:09:37 >
