사용자가 데이터베이스 데이터를 업로드하는 시나리오가 있습니다. 사용자는 데이터베이스에서 java 객체로 데이터를 읽은 다음 java 객체를 파일로 serialize합니다. 이 파일은 원격 서버에 업로드됩니다. 그런 다음 원격 서버에서이 파일을 deserialize하여 원격 서버의 데이터베이스에 저장합니다.타임 스탬프가 포함 된 객체의 비 직렬화
타임 스탬프와 날짜를 deserialise 할 때 문제가 나타납니다. 타임 스탬프는 클라이언트 시스템의 타임 스탬프와 비교할 때 다릅니다. 우리는 문제를 클라이언트 시스템의 잘못 설정된 시간대로 다시 추적했습니다. 클라이언트는 미국과 캐나다의 태평양 표준시 (UTC - 8:00)이고 서버는 인도 표준시 (UTC + 5:30)입니다. 따라서 데이터베이스가 데이터를 삽입하면 시간차가 보상됩니다. 클라이언트 시스템에서 올바르지 않게 설정된 시간대를 변경 했으므로 이제는 모든 것이 정상입니다.
하지만 우리는 모든 클라이언트 시스템을 제어 할 필요가 없습니다. 그들이 서로 다른 시간대 (시스템에 잘못 설정)에 있다면 서버가 데이터를 보완하고 저장하지 않도록 어떻게 지시해야합니까? deserialising 의미는 사용자가 보낸 그대로 데이터를 DB에 저장해야합니다.
또한 서버를 다른 시간대로 이동하면 문제가 모든 사용자에게 나타납니다.
우리는 자바를 사용하고 데이터베이스는 MySQL의
입니다편집 :
public class Test
{
public static void main(String[] args) {
try {
DBObject db = new DBObject();
db.setTs(new Timestamp(System.currentTimeMillis()));
//first save the data on one timezone
ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream("/Users/Sethu/temp/test.dat"));
os.writeObject(db);
os.close();
//comment the top portion of saving.. change the timezone and run the code,
//you will see a different date appearing in your screen
ObjectInputStream is=new ObjectInputStream(new FileInputStream("/Users/Sethu/temp/test.dat"));
DBObject dbin=(DBObject)is.readObject();
System.out.println(dbin.getTs());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class DBObject implements Serializable{
private Timestamp ts;
public Timestamp getTs() {
return ts;
}
public void setTs(Timestamp ts) {
this.ts = ts;
}
}
편집 : 2 : 다음 코드 샘플의 IT 원래 생성 된 시간대에 다시 날짜를 변환하기 위해, 나는 이제 직렬화 된 시간대를 보내도록 코드를 변경했습니다. 지금 시간대는 먼저 직렬화 된 객체는하고 DBOBJECT 지금 나는 resonctructed 개체를 변경하려고 직렬화 된 시간대를 사용하여 ... 초 하나입니다
System.out.println("Current Timezone="+DateTimeZone.getDefault());
DateTimeZone timezone=(DateTimeZone)is.readObject();
System.out.println("Timezone of saved object="+timezone);
DBObject dbin=(DBObject)is.readObject();
System.out.println("Date in current timezone="+dbin.getTs());
System.out.println("Date time Object in timezone of saved object="+new DateTime(dbin.getTs()).withZone(timezone));
//This is where the issue is.. As soon as I do this, I get the date in the current timezone again..
System.out.println("Date time exported to date="+new DateTime(dbin.getTs()).withZone(timezone).toDate());
나는이 작업을 수행 할 때, 이것이 내가 무엇입니까 출력이입니다!
Current Timezone=America/Los_Angeles
Timezone of saved object=+05:30
Date in current timezone=Tue Dec 06 23:30:56 PST 2011
Date time Object in timezone of saved object=2011-12-07T13:00:56.687+05:30
Date time exported to date=Tue Dec 06 23:30:56 PST 2011
문제가 직렬화 또는 데이터베이스에 있는지 여부는 확실하지 않으며 사용자가 코드를 제공하지 않았거나 관련 유형의 표시. 그것은 대답을하기가 매우 어렵습니다. –
추측 : 밀리 초 단위로 날짜 저장 (Long 타입)? –