DateUtils 和 DateFormatUtils

 DateUtils

org.apache.commons.lang3.time.DateUtils
  • 判断两个日期是否是同一天
public static boolean isSameDay(final Date date1, final Date date2)
  • 判断两个日期是否在时间上表示同一时刻(毫秒数相同)
public static boolean isSameInstant(final Date date1, final Date date2)
  • String转换成Date
public static Date parseDate(final String str, final String... parsePatterns)
public static Date parseDate(final String str, final Locale locale, final String... parsePatterns) 

代码:

/**
 * String转换成Date
 * arg0 : 日期字符串 String
 * arg1 : 特定的地理,政治和文化地区.可以不传
 * arg3 : 日期格式.与arg0格式一致 也可以传入多个解析格式,解析将依次尝试每个解析模式
 */
Date date1 = DateUtils.parseDate("20171012 14:30:12", Locale.TRADITIONAL_CHINESE, "yyyyMMdd hh:mm:ss");
  • 获取指定日期前后amount年,正数向后天数,0当天,负数向前天数
public static Date addYears(final Date date, final int amount)
  • 获取指定日期前后amount月
public static Date addMonths(final Date date, final int amount)
  • 获取指定日期前后amount周
public static Date addWeeks(final Date date, final int amount)
  • 获取指定日期前后amount天
public static Date addDays(final Date date, final int amount)
  • 获取指定日期前后amount小时
public static Date addHours(final Date date, final int amount)
  • 获取指定日期前后amount分钟
public static Date addMinutes(final Date date, final int amount)
  • 获取指定日期前后amount秒
public static Date addSeconds(final Date date, final int amount)
  • 获取指定日期前后amount毫秒
ublic static Date addMilliseconds(final Date date, final int amount)
  • 指定日期年的值
public static Date setYears(final Date date, final int amount)
  • 指定日期月的值
public static Date setMonths(final Date date, final int amount)
  • 指定日期天的值
public static Date setDays(final Date date, final int amount)
  • 指定日期小时的值
public static Date setHours(final Date date, final int amount)
  • 指定日期分钟的值
public static Date setMinutes(final Date date, final int amount)
  • 指定日期秒的值
public static Date setSeconds(final Date date, final int amount)
  • 指定日期毫秒的值
 public static Date setMilliseconds(final Date date, final int amount)
  • 获取指定日期中从指定位置起的毫秒数
public static long getFragmentInMilliseconds(final Date date, final int fragment)
  • 获取指定日期中从指定位置起的秒数
 public static long getFragmentInSeconds(final Date date, final int fragment)
  • 获取指定日期中从指定位置起的分钟数
public static long getFragmentInMinutes(final Date date, final int fragment)
  • 获取指定日期中从指定位置起的小时数
public static long getFragmentInHours(final Date date, final int fragment)
  • 获取指定日期中从指定位置起的天数
public static long getFragmentInDays(final Date date, final int fragment)
  • 判断两个时间在指定的位置之上是否相等
/**
 * 判断两个时间在指定的位置之上是否相等
 * arg0 : 时间1 Date类型 或 Calendar类型
 * arg1 : 时间2 Date类型 或 Calendar类型
 * arg2 : 指定在位置上开始比较 int类型:建议使用 Calendar.YEAR Calendar.MONTH 等常量
 */
public static boolean truncatedEquals(final Date date1, final Date date2, final int field)
  • 判断两个时间在指定的位置上的时间差值
/**
 * 比较arg0与arg1两个时间在指定的位置上的时间差值
 * arg0 : 时间1 Date类型 或 Calendar类型
 * arg1 : 时间2 Date类型 或 Calendar类型
 * arg2 : 指定在位置上开始比较 int类型:建议使用 Calendar.YEAR Calendar.MONTH 等常量
 */
public static int truncatedCompareTo(final Date date1, final Date date2, final int field)

 

DateFormatUtils

org.apache.commons.lang3.time.DateFormatUtils
  •  Date转换成String
// 将日期格式转化为特定的字符串格式
public static String format(Date date, String pattern)
// 将毫秒值转化为特定的字符串格式
public static String format(long millis,String pattern)
// 使用UTC时区将日期/时间格式转化为特定的字符串格式
public static String formatUTC(Date date,String pattern)

代码: 

// 可以使用DateFormatUtils.format()方法
DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mephisto180502

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值