-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
1.1.7
-
generic, sparc
-
generic, solaris_2.6
According to spec, java.sql.Date is supposed to zero the time componenet
of the date passed into it via constructor or setTime(int) method.
This does not occur if the Default timezone is set to something other
than 'GMT'.
This happens as of jdk1.1.7. The spec was changed for 1.2, so this bug
will not appear there.
I have spoken with Seth White regarding this problem, while we both
agreed that this is a non-critical issue, we both agreed that it is
a bug.
It may be the result of the constructor and setTime method relying on
deprecated methods in java.util.Date.
=======test case=========
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.TimeZone;
/**
* Tests java.sql.Date(long) in EST time zone
*/
public class SqlDateTest3 {
public static void main(String[] args) {
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S zzz");
// parse an arbitrary date
java.util.Date testdate = null;
try {
testdate = fullFormat.parse("1998-10-26 16:30:00.0 EST");
}
catch( ParseException e ) {
System.out.println("Log: Parse exception trying to parse date");
System.out.println("TEST FAILED");
System.exit(1);
}
// Set the timezone
TimeZone.setDefault(TimeZone.getTimeZone("EST"));
// make sure we're getting a reasonable date
long millis = testdate.getTime();
if( millis == 0 ) {
System.out.println("Log: java.util.Date() not returning correct time!");
System.out.println("Test Failed");
}
// Create a java.sql.Date representing this date
Date sqlDate = new Date(millis);
// Get text representations of the two dates
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.S");
String testdateDateStr = dateFormat.format(testdate);
String sqlDateStr = dateFormat.format(sqlDate);
String timeStr = timeFormat.format(testdate);
String sqlTimeStr = timeFormat.format(sqlDate);
// Dates should be the same
if( ! sqlDateStr.equals(testdateDateStr) ) {
System.out.println("Log: dates not equal");
System.out.println("Test Failed");
System.exit(1);
}
// time should be zeroed
if( ! sqlTimeStr.equals("00:00:00.0") ) {
System.out.println("Log: sql time not 0 : " + sqlTimeStr);
System.out.println("Test Failed");
System.exit(1);
}
System.out.println("Test Passed");
System.exit(0);
}
}
======test case #2 (proves that it works if timezone set to GMT=========
mport java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.TimeZone;
/**
* Tests java.sql.Date(long) in GMT time zone
*/
public class SqlDateTest1 {
public static void main(String[] args) {
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S zzz");
// parse an arbitrary date
java.util.Date testdate = null;
try {
testdate = fullFormat.parse("1998-10-26 16:30:00.0 EST");
}
catch( ParseException e ) {
System.out.println("Log: Parse exception trying to parse date");
System.out.println("TEST FAILED");
System.exit(1);
}
// Set the timezone
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
// make sure we're getting a reasonable date
long millis = testdate.getTime();
if( millis == 0 ) {
System.out.println("Log: java.util.Date() not returning correct time!");
System.out.println("Test Failed");
}
// Create a java.sql.Date representing this date
Date sqlDate = new Date(millis);
// Get text representations of the two dates
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.S");
String testdateDateStr = dateFormat.format(testdate);
String sqlDateStr = dateFormat.format(sqlDate);
String timeStr = timeFormat.format(testdate);
String sqlTimeStr = timeFormat.format(sqlDate);
// Dates should be the same
if( ! sqlDateStr.equals(testdateDateStr) ) {
System.out.println("Log: dates not equal");
System.out.println("Test Failed");
System.exit(1);
}
// time should be zeroed
if( ! sqlTimeStr.equals("00:00:00.0") ) {
System.out.println("Log: sql time not 0 : " + sqlTimeStr);
System.out.println("Test Failed");
System.exit(1);
}
System.out.println("Test Passed");
System.exit(0);
}
}
of the date passed into it via constructor or setTime(int) method.
This does not occur if the Default timezone is set to something other
than 'GMT'.
This happens as of jdk1.1.7. The spec was changed for 1.2, so this bug
will not appear there.
I have spoken with Seth White regarding this problem, while we both
agreed that this is a non-critical issue, we both agreed that it is
a bug.
It may be the result of the constructor and setTime method relying on
deprecated methods in java.util.Date.
=======test case=========
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.TimeZone;
/**
* Tests java.sql.Date(long) in EST time zone
*/
public class SqlDateTest3 {
public static void main(String[] args) {
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S zzz");
// parse an arbitrary date
java.util.Date testdate = null;
try {
testdate = fullFormat.parse("1998-10-26 16:30:00.0 EST");
}
catch( ParseException e ) {
System.out.println("Log: Parse exception trying to parse date");
System.out.println("TEST FAILED");
System.exit(1);
}
// Set the timezone
TimeZone.setDefault(TimeZone.getTimeZone("EST"));
// make sure we're getting a reasonable date
long millis = testdate.getTime();
if( millis == 0 ) {
System.out.println("Log: java.util.Date() not returning correct time!");
System.out.println("Test Failed");
}
// Create a java.sql.Date representing this date
Date sqlDate = new Date(millis);
// Get text representations of the two dates
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.S");
String testdateDateStr = dateFormat.format(testdate);
String sqlDateStr = dateFormat.format(sqlDate);
String timeStr = timeFormat.format(testdate);
String sqlTimeStr = timeFormat.format(sqlDate);
// Dates should be the same
if( ! sqlDateStr.equals(testdateDateStr) ) {
System.out.println("Log: dates not equal");
System.out.println("Test Failed");
System.exit(1);
}
// time should be zeroed
if( ! sqlTimeStr.equals("00:00:00.0") ) {
System.out.println("Log: sql time not 0 : " + sqlTimeStr);
System.out.println("Test Failed");
System.exit(1);
}
System.out.println("Test Passed");
System.exit(0);
}
}
======test case #2 (proves that it works if timezone set to GMT=========
mport java.sql.Date;
import java.text.SimpleDateFormat;
import java.text.ParseException;
import java.util.TimeZone;
/**
* Tests java.sql.Date(long) in GMT time zone
*/
public class SqlDateTest1 {
public static void main(String[] args) {
SimpleDateFormat fullFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S zzz");
// parse an arbitrary date
java.util.Date testdate = null;
try {
testdate = fullFormat.parse("1998-10-26 16:30:00.0 EST");
}
catch( ParseException e ) {
System.out.println("Log: Parse exception trying to parse date");
System.out.println("TEST FAILED");
System.exit(1);
}
// Set the timezone
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
// make sure we're getting a reasonable date
long millis = testdate.getTime();
if( millis == 0 ) {
System.out.println("Log: java.util.Date() not returning correct time!");
System.out.println("Test Failed");
}
// Create a java.sql.Date representing this date
Date sqlDate = new Date(millis);
// Get text representations of the two dates
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss.S");
String testdateDateStr = dateFormat.format(testdate);
String sqlDateStr = dateFormat.format(sqlDate);
String timeStr = timeFormat.format(testdate);
String sqlTimeStr = timeFormat.format(sqlDate);
// Dates should be the same
if( ! sqlDateStr.equals(testdateDateStr) ) {
System.out.println("Log: dates not equal");
System.out.println("Test Failed");
System.exit(1);
}
// time should be zeroed
if( ! sqlTimeStr.equals("00:00:00.0") ) {
System.out.println("Log: sql time not 0 : " + sqlTimeStr);
System.out.println("Test Failed");
System.exit(1);
}
System.out.println("Test Passed");
System.exit(0);
}
}