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
Gordon Beaton - 28 Nov 2007 10:03 GMT
> The input value is, e.g.: 2007-11-27T14:55:40-08:00
>
[quoted text clipped - 3 lines]
> 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
--
Thomas Kellerer - 28 Nov 2007 10:38 GMT
Gordon Beaton, 28.11.2007 11:03:
>> The input value is, e.g.: 2007-11-27T14:55:40-08:00
>>
[quoted text clipped - 7 lines]
>
> 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
Wayne - 28 Nov 2007 10:25 GMT
> Hi,
>
[quoted text clipped - 20 lines]
> 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
Thomas Kellerer - 28 Nov 2007 10:42 GMT
Wayne, 28.11.2007 11:25:
>> The input value is, e.g.: 2007-11-27T14:55:40-08:00
>>
[quoted text clipped - 18 lines]
> 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
Dirk Michaelsen - 28 Nov 2007 10:37 GMT
Thomas Kellerer <YQDHXVLMUBXG@spammotel.com> schrieb:
>The input value is, e.g.: 2007-11-27T14:55:40-08:00
try this pattern:
"yyyy-MM-dd'T'HH:mm:ssZ"
cu
Dirk
Thomas Kellerer - 28 Nov 2007 10:40 GMT
Dirk Michaelsen, 28.11.2007 11:37:
> Thomas Kellerer <YQDHXVLMUBXG@spammotel.com> schrieb:
>
[quoted text clipped - 6 lines]
> 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
Dirk Michaelsen - 28 Nov 2007 11:39 GMT
Thomas Kellerer <YQDHXVLMUBXG@spammotel.com> schrieb:
>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
Gordon Beaton - 28 Nov 2007 11:43 GMT
> Thomas Kellerer <YQDHXVLMUBXG@spammotel.com> schrieb:
>>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:
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
--
Dirk Michaelsen - 28 Nov 2007 13:35 GMT
Gordon Beaton <n.o.t@for.email> schrieb:
>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