1、joda time 介绍

Joda Time提供了一组Java类包用于处理包括ISO8601标准在内的date和time。 可以利用它把JDK的DateCalendar类完全替换掉,而且仍然能够提供很好的集成。

2、pom依赖

    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.7</version>
    </dependency>

3、示例

package com.laurel.demo.jodatime;

import org.joda.time.DateTime;

import java.util.Date;

public class DateUtils {

public static Date makeDate(Date date, int hour, int minute, int second) {
DateTime dateTime = new DateTime(date);
return new DateTime(dateTime.getYear(),
dateTime.getMonthOfYear(),
dateTime.getDayOfMonth(),
hour,
minute,
second).toDate();
}

/**
* 制作开始时间
* @param date 日期
* @return 形如:2016-05-29 00:00:00
*/

public static Date makeBeginDate(Date date) {
return makeDate(date, 0, 0, 0);
}

/**
* 制作结束时间
* @param date 日期
* @return 形如:2016-05-29 23:59:59
*/

public static Date makeEndDate(Date date) {
return makeDate(date, 23, 59, 59);
}

/**
* 获取n天之后的时间
* @param date 日期
* @param days n天,可正可负
* @return
*/

public static Date getNextNDay(Date date, int days) {
DateTime dateTime = new DateTime(date);
return dateTime.plusDays(days).toDate();
}

/**
* 格式化日期
* @param date 日期
* @param pattern 日期格式
* @return
*/

public static String format(Date date, String pattern) {
return new DateTime(date).toString(pattern);
}

/**
* 格式化日期(年月日)
* @param date 日期
* @return
*/

public static String dateFormat(Date date) {
return new DateTime(date).toString("yyyy-MM-dd");
}

/**
* 格式化日期(年月日 时分秒)
* @param date 日期
* @return
*/

public static String sencondFormat(Date date) {
return new DateTime(date).toString("yyyy-MM-dd HH:mm:ss");
}
}

参考资料