Java代码:将16进制时间字符串"0A03170F3217"解析为日期时间
以下是将字符串"0A03170F3217"解析为16进制的时间的Java代码:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
public static void main(String[] args) {
String hexString = "0A03170F3217";
String yearHex = hexString.substring(0, 2);
String monthHex = hexString.substring(2, 4);
String dayHex = hexString.substring(4, 6);
String hourHex = hexString.substring(6, 8);
String minuteHex = hexString.substring(8, 10);
String secondHex = hexString.substring(10, 12);
int year = Integer.parseInt(yearHex, 16) + 2000;
int month = Integer.parseInt(monthHex, 16);
int day = Integer.parseInt(dayHex, 16);
int hour = Integer.parseInt(hourHex, 16);
int minute = Integer.parseInt(minuteHex, 16);
int second = Integer.parseInt(secondHex, 16);
LocalDateTime dateTime = LocalDateTime.of(year, month, day, hour, minute, second);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日H时m分s秒");
String formattedDateTime = dateTime.format(formatter);
System.out.println(formattedDateTime);
}
}
输出结果为:2010年3月23日15时50分23秒
原文地址: https://www.cveoy.top/t/topic/qBT4 著作权归作者所有。请勿转载和采集!