2013-03-01 135 views
0

我必须发送系统当前时间(EPOCH)的消息,并根据以下细节发送消息。也EPOCH时间发送纳秒。如何在java中使用EPOCH时间?

field - current_time 
type - UINT64 
byte size - 8 
value - 0 to 1.84467E+19 

我的消息结构如下,

class MsgHeader { 
    int message_length; 
    String sender_sys; 
    String destination_sys; 
    **int current_time;** 
    char message_type; 

................ 

} 

任何人都可以请建议我如何做到这一点使用java吗?

+0

SO实际上是支持搜索。 – Drogba 2013-03-01 01:39:50

+0

我想发送8bytes的当前时间范围,并根据给定的规格。 – pradeekrathnayaka 2013-03-01 01:43:23

+0

你确定你想要_nanoseconds_,而不是_milliseconds_? – jtahlborn 2013-03-01 02:06:21

回答

2
long current_time = System.currentTimeMillis() * 1000000L; 
+0

谢谢,这是非常有用的 – pradeekrathnayaka 2013-03-01 03:45:11

0

我将current_time的长整型值转换为字节,如下所示。

public static byte[] longToBytes(long current_time) throws IOException { 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(Long.SIZE/8); 
     DataOutputStream dos = new DataOutputStream(baos); 
     dos.writeLong(current_time); 
     byte[] result = baos.toByteArray(); 
     dos.close(); 
     System.out.println(result.length);//length=8bytes 
     return result; 
    }