当前位置:首页 > 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 是更现代的替代方案,线程安全且支持更多功能。

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 可以处理时区问题。

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 可以生成本地化的日期格式:

java中如何格式化时间格式

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。时区敏感的应用务必明确指定时区处理。

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

相关文章

jquery格式

jquery格式

jQuery 基本格式 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。以下是 jQuery 的基本使用格式: $(docu…

vue 实现时间

vue 实现时间

Vue 实现时间的几种方法 在Vue中实现时间显示或处理时间数据,可以通过多种方式实现,包括原生JavaScript、第三方库或Vue插件。以下是几种常见的方法: 使用原生JavaScript显示当…

vue 时间控件实现

vue 时间控件实现

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

vue实现时间刻度

vue实现时间刻度

Vue实现时间刻度组件 时间刻度组件常用于时间轴、日程管理或数据可视化场景。以下是基于Vue的实现方案: 基础时间刻度实现 使用v-for循环生成刻度元素,结合CSS实现布局: <templ…

vue实现时间滑块

vue实现时间滑块

Vue 实现时间滑块 使用原生 HTML5 input range 通过 HTML5 的 input[type="range"] 结合 Vue 的数据绑定实现基础时间滑块: <template…

react如何实现时间戳转换

react如何实现时间戳转换

时间戳转换为日期格式 在React中实现时间戳转换通常需要借助JavaScript的Date对象或第三方库(如moment.js或date-fns)。以下是几种常见方法: 使用原生JavaScrip…