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