I'm having a problem sending java.util.Date objects over RMI, to and from machines in different timezones.
For example, a client in Germany will send a date object to a server in the UK.
- User enters a date string e.g. 20090220.
- Client app in Germany converts it to date using SimpleDateFormat("yyyyMMdd") to give: Fri Feb 20 00:00:00 CET 2009
- Server in UK receives Date over RMI as: Thu Feb 19 23:00:00 GMT 2009
- Server stores Date into a UK Oracle Database DATE column
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.
Here is a stand-alone test program showing date serialisation:
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);
}
}
}