File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes parsing date as a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parsing date as a string" Watch "parsing date as a string" New topic

parsing date as a string

Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
Here's the (bloody) situation:
wonder how to convert string to date. Say you have a
String s = "10.10.2001";
I need to somehow split the string s by delimiter . and send them to array and then use Date (int,int,int) to create a date out of array.
Q: how do I split string s?
Please, anybody?!
Soren Therkildsen

Joined: Aug 10, 2001
Posts: 9

My sugestion:
String[] elements = s.split(".");
Date d = new Date(Integer.parseInt(elements[0]),
I hope it works.
Regards S�ren
Lon Allen
Ranch Hand

Joined: May 24, 2001
Posts: 67
I think that the split method is part of a package that isn't included in basic Java libraries. I can't find it...
Here is how I handle breaking elements of a string into pieces where the delimiters are known ("." in your case). The remaining delimiters in my example are "tab", "newline", and "carriage-return". The final token in your string is delimited by something other than the "." and you will need to cover that delimeter in one fashion or another to get all three tokens. It doesn't hurt to put the remaining delimiters in the delimiter list.

I hope that this helps...
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Dalibor,
Date has been depreciated in Java2. You should make use of the Calendar class. The following should work for you.

Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
You could use the StringTokenizer class


you could check out the GDate class in the file.

By the way, the Date class is not deprecated. Some of the methods in the Date class are deprecated.

[This message has been edited by Marilyn deQueiroz (edited September 26, 2001).]

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
1. Thanks everybody!
2. Soren Therkildsen: your code doesn't work. It would be nice it does though, because it is simple. Pitty.
3. Lon Allen: you have some mistakes in your code.
4. Manfred Leonhardt: sorry, I am not familiar with Calendar class and my (nervous) boss won't give me time to study it.
5. In the mean time I come up with this, and I am posting it to help others and answer myself:
StringTokenizer st1 = new StringTokenizer(dateOutOfVector, ".", false);
int[] j = new int[3];
int k = 0;
int proba = 0;
while (st1.hasMoreTokens()) {
j[k] = Integer.parseInt(st1.nextToken());
Date current = new Date(j[2] -1900, j[1] -1, j[0]);

Have a nice day, everybody!
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
What is to study. He gave you the code.
Dalibor Toth
Ranch Hand

Joined: Jul 16, 2001
Posts: 38
Originally posted by Paul Stevens:
What is to study. He gave you the code.

Yes he did. But it is not the problem in the code, the problem is its implementation into the rest of mine - it was quicker for me to do it my way. I am sorry if I offended somebody.
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
I think using SimpleDateFormat is much easier than your solution. Even if you don't want to use Calendar, you can still get the date.

That's it.
Isn't that much easier???
I agree. Here's the link:
subject: parsing date as a string
It's not a secret anymore!