String
Is there a real simple way to break a string of July 09 2007 9:00 PM into just July 09 2007? becuase i am having trouble thinking of something simple lol
Is there a real simple way to break a string of July 09 2007 9:00 PM into just July 09 2007? becuase i am having trouble thinking of something simple lol
Substring, or better, String.split(). Although I wonder why you don't keep a date as Date, and use SimpleDateFormat to make human-readable Strings of it as you need them.
I'm reading html code off a table in a website and i go by column breaks (</td>). The date and time is part of a cell but i only want the date.
if you can rely on the date format to be always that way,
search for the third space and cut before it.
The String.split() can't be applied to a 2-D array can it? becuase i keep getting the error found java.lang.string[] required java.lang.string?
String.split generates an array of Strings, it breaks a string down into substrings with the supplier separator.
I'd use substring e.g.
int lastspace = timestring.lastIndexOf(' '); // last space
lastspace = timestring.lastIndexOf(' ', lastspace); // previous space
if(lastspace > 0)
lastspace = lastspace.substring(0, lastspace);
Or, more likely, I'd parse it with a DataFormat, then format it back to a string (thus checking it really was a valid time).
Message was edited by:
malcolmmc
no, you will need to construct some sort of loop like
String[] dates = parseDatesFromHTML(); // your date string retrieval function goes here
for (String date : dates) {
String[] temp = date.split(" "); // split the date at the spaces
date = temp[0] + temp[1] + temp[2]; // reconcatenate the first three parts (month day, year))
}
// now your dates array is filled with propper dates
ok I'm still stuck on this problem becuase the website is a little different than i though... i have a 2-D array which contains the row and column (table[row][column] ) in column 1 is the date and the only thing i want to change but i have 2 different formats...7/26/2007 9:00:00 PM and July 09 2007 9:00 PM any suggestions on how i can replace table[row][1] with just the date?
Treat a date as a Date and get rid of 2D arrays altogether by introducing a Row object. I mean, Java's an OO language after all.
> Treat a date as a Date and get rid of 2D arrays
> altogether by introducing a Row object. I mean,
> Java's an OO language after all.
I notice many forum members render that:
O_o
> Treat a date as a Date and get rid of 2D arrays
> altogether by introducing a Row object. I mean,
> Java's an OO language after all.
Ya but my problem is that it is just text in a table on a website so the date gets streamed in with everything else and i need all the other data around it lol
> > Treat a date as a Date and get rid of 2D arrays
> > altogether by introducing a Row object. I mean,
> > Java's an OO language after all.
>
> Ya but my problem is that it is just text in a table
> on a website so the date gets streamed in with
> everything else and i need all the other data around
> it lol
Your basic problem is to turn a String into a Date. (Unless you have
a compelling reason *not* to turn it into a date!) So this breaks into
two subproblems:
1. When to parse the date string.
2. How to parse it.
1. It's hard to answer 1 without seeing some code, but the sooner the better
is my gut feeling, and I have a huge gut. Your 2-D array sounds like a
big mistake. You should have a collection of appropriate objects instead:
http://java.sun.com/docs/books/tutorial/collections/index.html
2. If the date string comes in multiple formats, the simplest way to tackle this
is to have multiple SimpleDateFormat objects, and try each in turn:
http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html
Problem, as they say, solved.
> > Problem, as they say, solved.
>
> Groovy.
>
> ;o)
>
> ~
I was wondering what that tingling feeling at the back of my skull was...
<Your basic problem is to turn a String into a Date.
It's not a date its just text
thats where i take the row of data and break it up the columns
public String[][] setRowColum(String[] row)
{
int tempindex = 0;
while(tempindex >< index)
{
int front = 0;
int back = 0;
while (back < row[tempindex].lastIndexOf("</td>"))
{
back = row[tempindex].indexOf("</td>",back+1);
newrow[tempindex][colum] = row[tempindex].substring(front, back);
colum++;
front = back;
}
colum = 0;
tempindex++;
}
return newrow;
}
> <Your basic problem is to turn a String into a Date.
>
> It's not a date its just text
Text like "July 09 2007 9:00 PM"?
> ya its text that im taking off a website in a table
I wrote: "Your basic problem is to turn a String into a Date"
And you replied, "It's not a date its just text ".
?
ya....well I'm tired and read that wrong lol i thought you meant my problem is im turning the date into a string ^.^
> ya....well I'm tired and read that wrong lol i
> thought you meant my problem is im turning the date
> into a string ^.^
No, the suggestion is to parse the java.lang.String to a java.util.Date.
~
> > Stay tuned for the complete Groovy solution in
> 4, 3, 2, 1...
>
> What, you gonna post it?
Sorry, that was wrong of me. No Groovy should be posted in this thread.