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.

[757 byte] By [rajesh.chennaia] at [2007-11-26 17:39:33]
# 1
> 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
kajbja at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 2
> Please give your thougts on this issue to parse the> string of any format..> What date is 1/2/4 ?
sabre150a at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

tsitha at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 4
Oh look - a trifecta - and I'm last.But still faster'n Jos.
tsitha at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 5

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

rajesh.chennaia at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 6
> Hi,> > The problem here is, i dont know the format of the> String. Yes indeed, that's a problem.
tsitha at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 7
> 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.
kajbja at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 8
my doubt is , if something is present there in java 1.4 , why it cannot be achieved in Java 1.5..?..
rajesh.chennaia at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 9
> but DateFormat didnot accepts except String in the> format "Feb 01, 2007"> What did you not understand about the first three responses?
sabre150a at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 10
> 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 ?
sabre150a at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 11
Date( String s) was parsing all types of formats.. during this upgrade to Java 1.5 only i am getting Unparsable error..
rajesh.chennaia at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 12

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

tsitha at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 13
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..
rajesh.chennaia at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 14
> 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.
tsitha at 2007-7-9 0:07:42 > top of Java-index,Java Essentials,Java Programming...
# 15

> 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 > top of Java-index,Java Essentials,Java Programming...
# 16

> > > 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 > top of Java-index,Java Essentials,Java Programming...
# 17

> 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!

sabre150a at 2007-7-21 17:09:37 > top of Java-index,Java Essentials,Java Programming...
# 18
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
rajesh.chennaia at 2007-7-21 17:09:37 > top of Java-index,Java Essentials,Java Programming...
# 19
And what did you expect?API docs:Gets the date formatter with the default formatting style for the default localeNothing else.
CeciNEstPasUnProgrammeura at 2007-7-21 17:09:37 > top of Java-index,Java Essentials,Java Programming...
# 20

> 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 > top of Java-index,Java Essentials,Java Programming...
# 21
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).
rajesh.chennaia at 2007-7-21 17:09:37 > top of Java-index,Java Essentials,Java Programming...
# 22

> 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 > top of Java-index,Java Essentials,Java Programming...