例如67 42 9b 5d怎么转换为标准时间(是小端模式,低位在前)求给出Java代码。
chargpt回答:
可以使用 Java 中的 Date 类和 ByteBuffer 类来实现将 4 个字节的时间戳转换为标准时间的功能。
代码如下:
import java.nio.ByteBuffer;
import java.util.Date;
public class TimestampConverter {
public static void main(String[] args) {
byte[] bytes = {0x5d, 0x9b, 0x42, 0x67}; // 小端模式,低位在前
ByteBuffer buffer = ByteBuffer.wrap(bytes);
int timestamp = buffer.getInt(); // 将 4 个字节转换为 int 类型的时间戳
Date date = new Date(timestamp * 1000L); // 将时间戳转换为标准时间
System.out.println(date); // 输出标准时间
}
}
解释一下代码: