SimpleDateFormat challenge

T

Thomas Kellerer

Hi,

I need to convert a timestamp value in a string (from a text file that I
need to import into the database) into a java.util.Date object.

The problem is, that I the format of the input string does not seem to
match any of the possibilities that I have with SimpleDateFormat:

The input value is, e.g.: 2007-11-27T14:55:40-08:00

The problem I have, is with the non-standard timezone offset at the end.

I cannot find any pattern for SimpleDateFormat to parse the time portion
correctly. For the beginning it would also be sufficient if I could
simply make SimpleDateFormat ignore that part.

Note that I cannot change the input value (e.g. with substring) so I am
looking for a format model that covers the above input string.

Currently I don't think it's possible, but maybe there is someone out
there who has a brilliant idea ;)

Cheers
Thomas
 
G

Gordon Beaton

The input value is, e.g.: 2007-11-27T14:55:40-08:00

The problem I have, is with the non-standard timezone offset at the end.

I cannot find any pattern for SimpleDateFormat to parse the time
portion correctly. For the beginning it would also be sufficient if
I could simply make SimpleDateFormat ignore that part.

Doesn't this ignore the timezone?

sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")

/gordon

--
 
W

Wayne

Thomas said:
Hi,

I need to convert a timestamp value in a string (from a text file that I
need to import into the database) into a java.util.Date object.

The problem is, that I the format of the input string does not seem to
match any of the possibilities that I have with SimpleDateFormat:

The input value is, e.g.: 2007-11-27T14:55:40-08:00

The problem I have, is with the non-standard timezone offset at the end.

I cannot find any pattern for SimpleDateFormat to parse the time portion
correctly. For the beginning it would also be sufficient if I could
simply make SimpleDateFormat ignore that part.

Note that I cannot change the input value (e.g. with substring) so I am
looking for a format model that covers the above input string.

Currently I don't think it's possible, but maybe there is someone out
there who has a brilliant idea ;)

Cheers
Thomas

How about using a regular expression (java.util.Matcher) to parse and
replace the non-standard format into a standard one?

Or, strip out the timezone, format the rest, then add the
timezoneValue * 60 * 60 * 1000 to the resulting long?

-Wayne
 
T

Thomas Kellerer

Gordon Beaton, 28.11.2007 11:03:
Doesn't this ignore the timezone?

sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")

I was dead sure I have tried that, but probably I didn't :)

Thanks, it's working indeed.

Cheers
Thomas
 
T

Thomas Kellerer

Dirk Michaelsen, 28.11.2007 11:37:
try this pattern:

"yyyy-MM-dd'T'HH:mm:ssZ"

cu
Dirk
That will reject the input string as -08:00 is not a valid RFC timezone
(the Z will only accept -800)

Cheers
Thomas
 
T

Thomas Kellerer

Wayne, 28.11.2007 11:25:
How about using a regular expression (java.util.Matcher) to parse and
replace the non-standard format into a standard one?

Or, strip out the timezone, format the rest, then add the
timezoneValue * 60 * 60 * 1000 to the resulting long?

Basically yes. I'm using a Java based tool to do the import and that
currently only allows to specify a format model for the input values
(based on the features of SimpleDateFormat)

Thanks
Thomas
 
D

Dirk Michaelsen

Thomas Kellerer said:
That will reject the input string as -08:00 is not a valid RFC timezone
(the Z will only accept -800)

not on my system! I ran it as junit test under jdk 1.5:

public class SimpleDateFormatTest extends TestCase {

public void testSimpleDateFormat() {
String data = "2007-11-27T14:55:40-08:00";
SimpleDateFormat sdf = new
SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
try {
Date date = sdf.parse(data);
System.out.println(date);
} catch (ParseException e) {
e.printStackTrace();
fail("Fehler beim Parsen");
}
}
}

Dirk
 
G

Gordon Beaton

not on my system! I ran it as junit test under jdk 1.5:

On my system, the sdf fails to parse the string on 1.4.2, 1.5 and 1.6.

Maybe if you didn't catch the exception...

/gordon

--
 
D

Dirk Michaelsen

Gordon Beaton said:
Maybe if you didn't catch the exception...

the catch block is not the problem. If there was a problem, a stack
trace would have been printed out.

The problem is my Java runtime environment. I tested the code in my
IBM RAD 7.0 wich uses the IBM J9 Java runtime as default. There the
code runs without any fault. (Thanks to IBM :))

When I switch to SUNs JDK 1.5.0 then the exception is thrown as you
reported.

My fault! So please forget what I said ;-)

The pattern you have posted to Thomas was the right one.

Dirk
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top