2012-08-29 54 views
2

我正嘗試將使用C#生成的消息轉換爲JAVA。作爲第一步,我生成原型文件,這是我得到Protobuf C#消息轉換爲JAVA

package Om.Business.Scanner; 

message ScannerActivityDetail { 
    optional string ActivityId = 1; 
    optional string ContextId = 2; 
    optional int32 ActivityStart = 3; 
    optional bcl.DateTime ActivityEnd = 4; 
} 

我如何在Java世界中演繹bcl.DateTime?

我正在使用protobuf-net並試圖反序列化由C#應用程序生成的消息。

在此先感謝您的幫助。

+0

如果你使用的是跨平臺,我會建議更簡單地處理日期/時間,因爲一個時間間隔長到一個時代(傳統上1月1日1970年) –

回答

3

看着bcl.proto,它應該是非常簡單的。在明顯的方式創建一個Map<DateTime.TimeSpanScale, TimeUnit>,則:

public static Date toDate(bcl.DateTime proto) { 
    TimeUnit unit = SCALE_TO_UNIT_MAP.get(proto.getScale()); 
    if (unit == null) { 
     throw new IllegalArgumentException("Invalid scale: " + proto.getScale()); 
    } 
    long millis = unit.toMillis(proto.getValue()); 
    return new Date(millis); 
} 

你可以以完全相同的方式使用Joda TimeDateTime類型,因爲它有一個構造函數接受long了。 (你可能想考慮哪個時區需要指定,但是...)