Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parsing date as a string

 
Dalibor Toth
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

My sugestion:
String[] elements = s.split(".");
Date d = new Date(Integer.parseInt(elements[0]),
Integer.parseInt(elements[1]),
Integer.parseInt(elements[2]));
I hope it works.
Regards S�ren
 
Lon Allen
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dalibor,
Date has been depreciated in Java2. You should make use of the Calendar class. The following should work for you.

Enjoy,
Manfred.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use the StringTokenizer class

or

you could check out the GDate class in the JavaRanchCommon.zip 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).]
 
Dalibor Toth
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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());
k++;
}
Date current = new Date(j[2] -1900, j[1] -1, j[0]);

Have a nice day, everybody!
Dalibor
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is to study. He gave you the code.
 
Dalibor Toth
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic