Java将bigint 转换为格式化时间
Java中的BigInt类型表示大整数,通常用于处理超出long类型范围的数字。要将BigInt转换为格式化时间,需要使用Java中的Date和SimpleDateFormat类。
下面是一个示例代码,将BigInt类型的时间戳转换为格式化时间:
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
public class BigIntToTime {
public static void main(String[] args) {
BigInteger timestamp = BigInteger.valueOf(1609459200000L); // 2021-01-01 00:00:00的时间戳,单位毫秒
Date date = new Date(timestamp.longValue());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String formattedTime = sdf.format(date);
System.out.println(formattedTime);
}
}
输出结果为:2021-01-01 00:00:00
在示例代码中,首先创建了一个BigInt类型的时间戳,然后使用longValue()方法将其转换为long类型的毫秒数。接着,使用Date类的构造函数将毫秒数转换为日期,最后使用SimpleDateFormat类将日期格式化为指定的格式。
原文地址: https://www.cveoy.top/t/topic/b1KQ 著作权归作者所有。请勿转载和采集!