我知道我可以用4个字节得到它,就像这样:
int unixTime = (int)(System.currentTimeMillis() / 1000);byte[] productionDate = new byte[]{ (byte) (unixTime >> 24), (byte) (unixTime >> 16), (byte) (unixTime >> 8), (byte) unixTime};
int unixTime = (int)(System.currentTimeMillis() / 1000);
byte[] productionDate = new byte[]{
(byte) (unixTime >> 24),
(byte) (unixTime >> 16),
(byte) (unixTime >> 8),
(byte) unixTime
};
但是有没有一种方法可以让它在8字节内使用移位?
gcxthw6b1#
当然,只要读一读签名 long .
long
long unixTime = System.currentTimeMillis() / 1000L;byte[] bytes = new byte[] { (byte) (unixTime >> 56), (byte) (unixTime >> 48), (byte) (unixTime >> 40), (byte) (unixTime >> 32), (byte) (unixTime >> 24), (byte) (unixTime >> 16), (byte) (unixTime >> 8), (byte) unixTime};
long unixTime = System.currentTimeMillis() / 1000L;
byte[] bytes = new byte[] {
(byte) (unixTime >> 56),
(byte) (unixTime >> 48),
(byte) (unixTime >> 40),
(byte) (unixTime >> 32),
或者,使用nio bytebuffer
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES) .putLong(unixTime);byte[] bytes = buffer.array();
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES)
.putLong(unixTime);
byte[] bytes = buffer.array();
1条答案
按热度按时间gcxthw6b1#
当然,只要读一读签名
long
.或者,使用nio bytebuffer