Note that there are some explanatory texts on larger screens.

plurals
  1. POSending Dates over RMI between machines in different regions
    text
    copied!<p>I'm having a problem sending java.util.Date objects over RMI, to and from machines in different timezones. </p> <p>For example, a client in Germany will send a date object to a server in the UK.</p> <ol> <li>User enters a date string e.g. 20090220.</li> <li>Client app in Germany converts it to date using SimpleDateFormat("yyyyMMdd") to give: Fri Feb 20 00:00:00 CET 2009</li> <li>Server in UK receives Date over RMI as: Thu Feb 19 23:00:00 GMT 2009</li> <li>Server stores Date into a UK Oracle Database DATE column</li> </ol> <p>What is the best way to get around this issue of incorrect dates? I could send the date string across and have the Server convert it to a Date, but I want to reduce the amount of work that the Server has to do.</p> <p>Here is a stand-alone test program showing date serialisation:</p> <pre><code>import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Locale; public class SerializationTest { public static void main(String[] args) throws Exception { final String yyyyMMdd = "20090220"; final Date date = new SimpleDateFormat("yyyyMMdd").parse(yyyyMMdd); if (args.length != 1) { System.out.println("Usage SerializationTest S|D"); } boolean serialise = false; if (args[0].equals("S")) { serialise = true; } else if (args[0].equals("D")) { serialise = false; } String filename = "date.ser"; if (serialise) { // write the object to file FileOutputStream fos = new FileOutputStream(filename); BufferedOutputStream bos = new BufferedOutputStream(fos); ObjectOutputStream outputStream = new ObjectOutputStream(bos); outputStream.writeObject(date); outputStream.flush(); outputStream.close(); System.out.println("Serialised: " + date); } else { FileInputStream fis = new FileInputStream(filename); BufferedInputStream bis = new BufferedInputStream(fis); ObjectInputStream inputStream = new ObjectInputStream(bis); Date outDate = (Date) inputStream.readObject(); inputStream.close(); // print the object System.out.println(outDate); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload