可以使用Java中的SimpleDateFormat类来将时间戳毫秒转换为指定格式的日期时间字符串。

以下是Java代码示例:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class TimestampToDate {
    public static void main(String[] args) {
        long timestamp = 1646340793000L; // 时间戳毫秒
        Date date = new Date(timestamp); // 将时间戳转换为Date对象

        // 创建SimpleDateFormat对象并设置日期时间格式和时区
        SimpleDateFormat sdf = new SimpleDateFormat('MMMM-dd-yyyy hh:mm:ss a Z z');
        sdf.setTimeZone(TimeZone.getTimeZone('Asia/Shanghai'));

        // 将Date对象转换为指定格式的日期时间字符串
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate); // 输出格式化后的日期时间字符串
    }
}

输出结果为:

March-03-2022 07:59:53 AM +0800 CST

其中,'MMMM-dd-yyyy hh:mm:ss a Z z'为日期时间格式字符串,具体含义如下:

  • MMMM:表示月份的全称,例如'March'
  • dd:表示日期,例如'09'
  • yyyy:表示年份,例如'2023'
  • hh:表示小时,例如'03'
  • mm:表示分钟,例如'13'
  • ss:表示秒钟,例如'13'
  • a:表示上午或下午,例如'AM'
  • Z:表示时区偏移量,例如'+0800'
  • z:表示时区名称,例如'CST'

在代码示例中,使用了'Asia/Shanghai'时区来表示+8时区,可以根据实际需要调整时区参数。

Java 时间戳毫秒转换为指定格式日期时间字符串

原文地址: https://www.cveoy.top/t/topic/nkhC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录