Posts Tagged: Java

Java stack trace with line numbers

Often in Java program stack trace, you will see some stack frames include a source line number, while some only say “compiled code”. This is because the stack frames without line numbers have been compiled by the JIT compiler, and the JIT compiler obliterates line number information when it compiles code. Continue reading “Java stack trace with line numbers” →

Spy on a Java program

While a Java program is running in a console window, you can can get a stack dump of the program at any time, while it is running. This is very useful for debugging, especially for heavily multithreaded programs. Continue reading “Spy on a Java program” →

Java versus Daylight savings time

Depending on the version of the Java runtime and the location of the host, the Java Date and Calendar routines may take daylight savings time into account. For example:

  • If you set a Calendar or Date to a specific value and then read it back, you may end up with a time later than the one you set. (You could even conceivably end up with an earlier time if you set it a millisecond before daylight savings time ends, and then read it back after.)
  • Continue reading “Java versus Daylight savings time” →

java.sql.Date is not a real date

java.sql.Date stores only date information, not times. Simply converting a java.util.Date into a java.sql.Date will silently set the time to midnight. So, to store date/times to be manipulated as java.util.Date objects, don’t do this:

// BUG: loses time of day
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));

do this instead:

preparedStatement.setTimestamp(1, new java.sql.Timestamp(date.getTime()));

java.sql.Timestamp extends java.util.Date, but it should not be used as a Date. In JDK 1.3.1, Timestamp.getTime() (inherited from Date) returns the time to the nearest second only, but JDK 1.4.2 and JDK 1.5 it returns the time to the nearest millisecond as expected. Continue reading “java.sql.Date is not a real date” →