当前位置:首页 > Java

java中如何格式化时间格式

2026-03-03 22:23:59Java

使用 SimpleDateFormat 类

在 Java 中,SimpleDateFormat 类是最传统的时间格式化工具,属于 java.text 包。通过指定模式字符串,可以将 Date 对象格式化为需要的字符串形式。

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

public class Main {
    public static void main(String[] args) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(date);
        System.out.println(formattedDate); // 输出类似 2023-10-05 14:30:45
    }
}

常用模式符号:

  • yyyy:四位年份
  • MM:两位月份
  • dd:两位日期
  • HH:24小时制小时
  • mm:分钟
  • ss:秒

使用 DateTimeFormatter 类(Java 8+)

从 Java 8 开始,引入了新的日期时间 API(java.time 包),其中 DateTimeFormatter 是更现代的替代方案,线程安全且支持更多功能。

java中如何格式化时间格式

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedDate = now.format(formatter);
        System.out.println(formattedDate); // 输出类似 2023-10-05 14:30:45
    }
}

预定义格式:

DateTimeFormatter.ISO_LOCAL_DATE_TIME // 格式如 2023-10-05T14:30:45
DateTimeFormatter.ISO_LOCAL_DATE      // 格式如 2023-10-05

时区处理

在全球化应用中,可能需要考虑时区转换。ZonedDateTime 结合 DateTimeFormatter 可以处理时区问题。

java中如何格式化时间格式

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;

public class Main {
    public static void main(String[] args) {
        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("America/New_York"));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
        String formattedDate = zonedDateTime.format(formatter);
        System.out.println(formattedDate); // 输出类似 2023-10-05 10:30:45 EDT
    }
}

字符串解析为日期

反向操作(字符串转日期)同样重要:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class Main {
    public static void main(String[] args) {
        String dateStr = "2023-10-05";
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate date = LocalDate.parse(dateStr, formatter);
        System.out.println(date); // 输出 2023-10-05
    }
}

国际化支持

通过 Locale 可以生成本地化的日期格式:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate date = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d MMM yyyy", Locale.FRENCH);
        String formattedDate = date.format(formatter);
        System.out.println(formattedDate); // 输出类似 5 oct. 2023
    }
}

每种方法适用于不同场景,传统代码可能仍在使用 SimpleDateFormat,而新项目推荐使用 java.time API。时区敏感的应用务必明确指定时区处理。

标签: 格式时间
分享给朋友:

相关文章

vue 时间控件实现

vue 时间控件实现

vue 时间控件实现 使用 Element UI 的 DatePicker Element UI 提供了功能丰富的日期选择组件,支持单日、日期范围、时间选择等场景。安装 Element UI 后,可以…

vue实现文章发表时间

vue实现文章发表时间

实现文章发表时间显示 在Vue中显示文章发表时间通常涉及日期格式化处理。以下是几种常见实现方式: 使用JavaScript原生Date对象 直接通过JavaScript的Date对象处理时间戳或日期…

vue时间跨度实现

vue时间跨度实现

Vue 时间跨度实现方法 使用计算属性计算时间差 通过计算属性可以方便地计算两个日期之间的时间差。需要先定义开始和结束日期,然后在计算属性中进行计算。 computed: { timeDiffe…

react 如何处理时间戳

react 如何处理时间戳

时间戳转换为可读格式 使用 new Date() 将时间戳转换为日期对象,再通过内置方法格式化输出。例如显示为 YYYY-MM-DD HH:MM:SS: const timestamp = 1625…

css制作时间轴

css制作时间轴

时间轴的基本结构 使用HTML构建时间轴的基础结构,通常采用无序列表<ul>嵌套列表项<li>,每个列表项包含时间节点内容和时间标记: <ul class="timel…

java中如何获取当前时间

java中如何获取当前时间

获取当前时间的方法 在Java中获取当前时间有多种方式,以下是几种常见的方法: 使用 java.util.Date Date 类是Java早期版本中用于表示日期和时间的类,可以直接获取当前时间:…