java long类型时间字符串转为指定格式字符串
要将long类型的时间字符串转换为指定格式的字符串,可以使用java.util.Date类和java.text.SimpleDateFormat类来实现。以下是一个示例代码:
import java.util.Date;
import java.text.SimpleDateFormat;
public class Main {
public static void main(String[] args) {
long time = 1595884800000L; // long类型的时间字符串,单位为毫秒
Date date = new Date(time); // 将long类型的时间字符串转换为Date对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 指定要转换的格式
String formattedTime = sdf.format(date); // 将Date对象格式化为指定格式的字符串
System.out.println(formattedTime);
}
}
在上述代码中,我们首先定义了一个long类型的时间字符串time,然后将其转换为Date对象date。接下来,我们使用SimpleDateFormat类来指定要转换的格式,这里使用了yyyy-MM-dd HH:mm:ss表示年月日时分秒的格式。最后,我们使用format方法将date对象格式化为指定格式的字符串,并将其输出。
运行上述代码,输出结果为2020-07-28 00:00:00,即将long类型的时间字符串转换为了指定格式的字符串
原文地址: https://www.cveoy.top/t/topic/iw97 著作权归作者所有。请勿转载和采集!