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

[160 byte] By [mark07a] at [2007-11-27 11:41:20]
# 1

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.

CeciNEstPasUnProgrammeura at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

mark07a at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 3

if you can rely on the date format to be always that way,

search for the third space and cut before it.

tom_jansena at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 4

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?

mark07a at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 5

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

malcolmmca at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 6

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

Vectorizeda at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 7

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?

mark07a at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 8

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.

CeciNEstPasUnProgrammeura at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 9

> 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

BigDaddyLoveHandlesa at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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

mark07a at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 11

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

BigDaddyLoveHandlesa at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 12

> Problem, as they say, solved.

Groovy.

;o)

~

yawmarka at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 13

> > Problem, as they say, solved.

>

> Groovy.

>

> ;o)

>

> ~

I was wondering what that tingling feeling at the back of my skull was...

BigDaddyLoveHandlesa at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 14

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

}

mark07a at 2007-7-29 17:37:31 > top of Java-index,Java Essentials,Java Programming...
# 15

> <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"?

BigDaddyLoveHandlesa at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 16

ya its text that im taking off a website in a table

mark07a at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 17

java.text.SimpleDateFormat.parse()

~

yawmarka at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 18

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

?

BigDaddyLoveHandlesa at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 19

ya....well I'm tired and read that wrong lol i thought you meant my problem is im turning the date into a string ^.^

mark07a at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 20

Stay tuned for the complete Groovy solution in 4, 3, 2, 1...

BigDaddyLoveHandlesa at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 21

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

~

yawmarka at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 22

> Stay tuned for the complete Groovy solution in 4, 3, 2, 1...

What, you gonna post it?

~

yawmarka at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 23

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

BigDaddyLoveHandlesa at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...
# 24

> Sorry, that was wrong of me. No Groovy should be

> posted in this thread.

Icky rule.

~

yawmarka at 2007-7-29 17:37:35 > top of Java-index,Java Essentials,Java Programming...