【日期格式化】DateFormatUtils

[ 常量 ]

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));

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值