当前位置:首页 > 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。时区敏感的应用务必明确指定时区处理。

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

相关文章

jquery时间

jquery时间

jQuery 时间处理 jQuery 本身不提供专门的时间处理函数,但可以通过 JavaScript 的 Date 对象结合 jQuery 的事件和方法来实现时间操作。以下是常见的时间处理需求及实现方…

java中如何获取当前时间

java中如何获取当前时间

获取当前时间的几种方法 使用 java.time 包(Java 8及以上推荐) import java.time.LocalDateTime; LocalDateTime currentTime =…

vue 实现时间

vue 实现时间

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

vue 时间控件实现

vue 时间控件实现

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

vue实现当前时间

vue实现当前时间

实现当前时间的Vue方法 在Vue中获取并显示当前时间可以通过以下方式实现: 方法一:使用data属性 new Vue({ el: '#app', data: { curren…

vue页面实现文章格式

vue页面实现文章格式

实现文章格式化的方法 使用富文本编辑器组件 集成第三方富文本编辑器如Quill、TinyMCE或WangEditor,通过npm安装后注册为Vue组件。编辑器提供工具栏按钮实现加粗、斜体、标题等基础格…