【日期格式化】DateFormatUtils

本文详细介绍了使用FastDateFormat进行日期时间格式化的各种方法,包括如何将Date、Calendar和时间戳转换为不同格式的字符串,以及如何指定时区和区域设置。

[ 常量 ]

private static final TimeZone UTC_TIME_ZONE = FastTimeZone.getGmtTimeZone();

public static final FastDateFormat ISO_8601_EXTENDED_DATETIME_FORMAT
        = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ss");

public static final FastDateFormat ISO_DATETIME_FORMAT = ISO_8601_EXTENDED_DATETIME_FORMAT;

public static final FastDateFormat ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT
        = FastDateFormat.getInstance("yyyy-MM-dd'T'HH:mm:ssZZ");

public static final FastDateFormat ISO_DATETIME_TIME_ZONE_FORMAT = ISO_8601_EXTENDED_DATETIME_TIME_ZONE_FORMAT;

public static final FastDateFormat ISO_8601_EXTENDED_DATE_FORMAT
        = FastDateFormat.getInstance("yyyy-MM-dd");

public static final FastDateFormat ISO_DATE_FORMAT = ISO_8601_EXTENDED_DATE_FORMAT;

public static final FastDateFormat ISO_DATE_TIME_ZONE_FORMAT
        = FastDateFormat.getInstance("yyyy-MM-ddZZ");

public static final FastDateFormat ISO_TIME_FORMAT
        = FastDateFormat.getInstance("'T'HH:mm:ss");

public static final FastDateFormat ISO_TIME_TIME_ZONE_FORMAT
        = FastDateFormat.getInstance("'T'HH:mm:ssZZ");

public static final FastDateFormat ISO_8601_EXTENDED_TIME_FORMAT
        = FastDateFormat.getInstance("HH:mm:ss");

public static final FastDateFormat ISO_TIME_NO_T_FORMAT = ISO_8601_EXTENDED_TIME_FORMAT;

public static final FastDateFormat ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMAT
        = FastDateFormat.getInstance("HH:mm:ssZZ");

public static final FastDateFormat ISO_TIME_NO_T_TIME_ZONE_FORMAT = ISO_8601_EXTENDED_TIME_TIME_ZONE_FORMAT;

public static final FastDateFormat SMTP_DATETIME_FORMAT
            = FastDateFormat.getInstance("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);

 

[ 常用方法 ]

参数

Date date = new Date();
Calendar calendar = Calendar.getInstance();
long currentTimeMillis = System.currentTimeMillis();
String pattern = "yyyy-MM-dd HH:mm:ss";

DateFormatUtils.format => 将 日期时间 | 日历 | 日期时间毫秒数设置为特定模式字符串

// 将日期/时间格式化为区域设置中的特定模式。[日期/时间][格式化日期的模式]
System.out.println(DateFormatUtils.format(date, pattern));
// 将日期/时间格式化为区域设置中的特定模式。[日期/时间][格式化日期的模式][区域设置]
System.out.println(DateFormatUtils.format(date, pattern, Locale.ENGLISH));
// 将日期/时间格式化为时区中的特定模式。[日期/时间][格式化日期的模式][时区设置]
System.out.println(DateFormatUtils.format(date, pattern, TimeZone.getTimeZone("Asia/Shanghai")));
// 将日期/时间格式化为时区和地区中的特定模式。[日期/时间][格式化日期的模式][时区设置][区域设置]
System.out.println(DateFormatUtils.format(date, pattern, TimeZone.getDefault(), Locale.CHINESE));
// 将日历格式化为区域设置中的特定模式。[日历][格式化日期的模式]
System.out.println(DateFormatUtils.format(calendar, pattern));
// 将日历格式化为区域设置中的特定模式。[日历][格式化日期的模式][区域设置]
System.out.println(DateFormatUtils.format(calendar, pattern, Locale.CHINESE));
// 将日历格式化为时区中的特定模式。[日历][格式化日期的模式][时区设置]
System.out.println(DateFormatUtils.format(calendar, pattern, TimeZone.getTimeZone("Asia/Shanghai")));
// 将日历格式化为时区和地区中的特定模式.[日历][格式化日期的模式][时区设置][区域设置]
System.out.println(DateFormatUtils.format(calendar, pattern, TimeZone.getDefault(), Locale.CHINESE));
// 将日期/时间毫秒数格式化为特定的模式。[日期时间毫秒数][格式化日期的模式]
System.out.println(DateFormatUtils.format(currentTimeMillis, pattern));
// 将日期/时间毫秒数格式化为区域设置中的特定模式。[日期时间毫秒数][格式化日期的模式][区域设置]
System.out.println(DateFormatUtils.format(currentTimeMillis, pattern, Locale.CHINESE));
// 将日期/时间毫秒数格式化为时区中的特定模式。[日期时间毫秒数][格式化日期的模式][时区设置]
System.out.println(DateFormatUtils.format(currentTimeMillis, pattern, TimeZone.getDefault()));
// 将日期/时间毫秒数格式化为时区和地区中的特定模式。[日期时间毫秒数][格式化日期的模式][时区设置][区域设置]
System.out.println(DateFormatUtils.format(currentTimeMillis, pattern, TimeZone.getDefault(), Locale.CHINESE));

 

DateFormatUtils.formatUTC => 使用UTC时区将 日期时间 | 日期时间毫秒数设置为特定模式字符串

// 使用UTC时区将日期/时间格式化为特定的模式。[日期时间][格式化日期的模式]
System.out.println(DateFormatUtils.formatUTC(date, pattern));
// 使用UTC时区将日期/时间格式化为特定的模式。[日期时间][格式化日期的模式][区域设置]
System.out.println(DateFormatUtils.formatUTC(date, pattern, Locale.CHINESE));
// 使用UTC时区将日期/时间毫秒数格式化为特定的模式。[日期时间毫秒数][格式化日期的模式]
System.out.println(DateFormatUtils.formatUTC(currentTimeMillis, pattern));
// 使用UTC时区将日期/时间毫秒数格式化为特定的模式。[日期时间毫秒数][格式化日期的模式][区域设置]
System.out.println(DateFormatUtils.formatUTC(currentTimeMillis, pattern, Locale.CHINESE));

 

 

### Java 中使用 `SimpleDateFormat` 将日期转换为 'yyyyMMdd' 格式的示例 在 Java 中,可以利用 `SimpleDateFormat` 类来实现日期格式化操作。以下是具体的代码示例: ```java import java.text.SimpleDateFormat; import java.util.Date; public class DateFormattingExample { public static void main(String[] args) { // 获取当前日期 Date currentDate = new Date(); // 定义日期格式为 "yyyyMMdd" SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd"); // 对日期进行格式化处理 String formattedDate = dateFormat.format(currentDate); // 输出格式化后的日期字符串 System.out.println("Formatted Date: " + formattedDate); } } ``` 上述代码展示了如何创建一个 `SimpleDateFormat` 实例并指定模式 `"yyyyMMdd"` 来完成日期到字符串的转换[^4]。 需要注意的是,在定义日期格式时,字母大小写非常重要。例如,“y” 表示年份,“M” 表示月份,“d” 表示天数。“yyyyMMDD” 和 “yyyyMMdd” 虽然看起来相似,但由于大写字母 D 的含义不同(D 是一年中的第几天),因此它们会产生不同的结果[^5]。 如果希望进一步简化或者优化日期格式化的逻辑,也可以考虑 Apache Commons Lang 库中的 `DateFormatUtils` 工具类。它提供了静态方法可以直接用于快速格式化日期而无需手动实例化对象[^2]。 #### 注意事项 - 使用 `SimpleDateFormat` 进行线程安全的操作时需格外小心,因为它不是线程安全的。对于多线程环境下的应用建议采用其他替代方案如 `DateTimeFormatter`(Java 8+) 或者每次调用都重新初始化一个新的 `SimpleDateFormat` 实例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值