date parse errors again...
Niklas Fondberg
niklas.fondberg@i3micro.com
Tue Oct 2 01:00:00 GMT 2001
I get totally different results of the parsed date with jdk 1.3 (sun) and gcj 3.0.2.
How come? I tried everything but the only way I could get it to work in
gcj was to set the DateFormat timezone to my own! Which is strange because obviously it allways is GMT in HTTP responses.
This snippet works fine for me in gcj but does return a GMT date in jdk.
Look for the lines setting the pdfmt (parse date format) try switching to "jdk-mode".
Another thing; setting my timezone manually only brings me 3 hours back whatever I give to timezone as offset, even if I give a positive value like
TimeZone tz2 = new SimpleTimeZone(2 * 3600 * 1000, "Europe/Stockholm");
Please help me understand the differences.
code below
--------------------8<-----------8<------------------------------
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* modified for testpurposes by Niklas Fondberg
*/
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;
/**
* A class that displays information about a URL.
**/
public class GetURLInfo {
/** Use the URLConnection class to get info about the URL */
public static void printinfo(URL url) throws IOException {
Locale locale = new Locale("En", "Us", "Unix");
TimeZone tz = TimeZone.getTimeZone("Europe/Stockholm");
DateFormat pdfmt = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'", locale);
pdfmt.setTimeZone(tz);
// result fine in gcj
//pdfmt.setTimeZone(TimeZone.getTimeZone("GMT"));// result fine in jdk
DateFormat dfmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss zzz");
dfmt.setTimeZone(tz);
//TimeZone timeZone = TimeZone.getTimeZone("GMT+10:00");
//timeZone.setRawOffset(3600*10*1000);
//this.dfmt.setTimeZone(timeZone);
//TimeZone tz2 = new SimpleTimeZone(1000 * 3600, "Europe/Stockholm");
//this.dfmt.setTimeZone(tz2);
URLConnection c = url.openConnection(); // Get URLConnection from URL
c.setUseCaches(false);
c.connect(); // Open a connection to URL
System.out.println(" Content Length: " + c.getContentLength());
Date resDate = new Date(c.getDate());
System.out.println(" Date: " + resDate);
System.out.println(" headdates timezone: : " + resDate.getTimezoneOffset() );
System.out.println(" TimeZoneHere: " + tz.getID());
String hdatestr = c.getHeaderField("Date");
Date hdate = pdfmt.parse(hdatestr, new ParsePosition(0));
String fdate = dfmt.format( hdate );
System.out.println(" HeaderFieldDate: " + hdatestr);
//System.out.println(" TimeZone pdfmt: " + pdfmt.getTimeZone());
//System.out.println(" TimeZone dfmt: " + dfmt.getTimeZone());
System.out.println(" ParsedDate: " + fdate);
// If it is an HTTP connection, display some additional information.
if (c instanceof HttpURLConnection) {
HttpURLConnection h = (HttpURLConnection) c;
System.out.println(" Request Method: " + h.getRequestMethod());
System.out.println(" Response Message: " +h.getResponseMessage());
System.out.println(" Response Code: " + h.getResponseCode());
}
}
/** Create a URL, call printinfo() to display information about it. */
public static void main(String[] args) {
try { printinfo(new URL(args[0])); }
catch (Exception e) {
System.err.println(e);
System.err.println("Usage: java GetURLInfo <url>");
}
}
}
--------------------8<-----------8<------------------------------
-------------------------------------------------
Niklas Fondberg I3 Micro Technology
niklas.fondberg@i3micro.org
More information about the Java
mailing list