时间工具(jodaTime)

来源:互联网 发布:啥叫大数据 编辑:程序博客网 时间:2024/06/11 03:22
package com.tynet.common.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Date;import java.util.List;import java.util.Locale;import org.apache.commons.lang3.time.DateUtils;import org.joda.time.DateTime;import org.joda.time.Days;import org.joda.time.Hours;import org.joda.time.Minutes;import org.joda.time.Months;import org.joda.time.Seconds;import org.joda.time.Weeks;import org.joda.time.Years;import org.joda.time.format.DateTimeFormat;/** * 时间工具(jodaTime) *  */public class DateUtil extends DateUtils {    /**     * 转时间类型     */    public static Date toDate(int year, int month, int day) {        return new DateTime(year, month, day, 0, 0).toDate();    }    /**     * 转时间类型     */    public static Date toDate(int year, int month, int day, int hour, int minute) {        return new DateTime(year, month, day, hour, minute).toDate();    }    /**     * 转时间类型     */    public static Date toDate(int year, int month, int day, int hour, int minute, int second) {        return new DateTime(year, month, day, hour, minute, second).toDate();    }    /**     * 转时间类型     *      * @param format     *            时间格式     * @return     */    public static Date toDate(String format, String timeStr) {        return DateTimeFormat.forPattern(format).parseDateTime(timeStr).toDate();    }    /**     * 获取日期 yyyy-MM-dd     *      * @return     */    public static String getDate() {        return new DateTime().toString("yyyy-MM-dd");    }    /**     * 获取时间 HH:mm:ss     *      * @return     */    public static String getTime() {        return new DateTime().toString("HH:mm:ss");    }    /**     * 获取日期和时间 yyyy-MM-dd HH:mm:ss     *      * @return     */    public static String getDateTime() {        return getDateTime("yyyy-MM-dd HH:mm:ss");    }    /**     * 格式化返回当前时间     *      * @param format     * @return     */    public static String getDateTime(String format) {        return formatDateTime(new Date(), format);    }    /**     * 返回指定时间的指定格式化字符串     *      * @param date     * @param format     * @return     */    public static String formatDateTime(Date date, String format) {        return new DateTime(date.getTime()).toString(format);    }    /**     * 获取指定时间是星期几 周一至周日:1至7     *      * @param date     * @return     */    public static int getWeekNo(Date date) {        return new DateTime(date.getTime()).getDayOfWeek();    }    public static String dateFormate(Date date, String formate) {        Locale.setDefault(Locale.CHINA);        if (date != null) {            java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(formate);            return sdf.format(date);        } else {            return null;        }    }    /**     * 获取某个月的所有天     *      * @param date     * @return     */    public static List<Date> getDateListByMonth(Date date) {        List<Date> list = new ArrayList<Date>();        Calendar c = Calendar.getInstance();        c.setTime(date);        int totalDays = c.getActualMaximum(Calendar.DAY_OF_MONTH);        for (int i = 1; i <= totalDays; i++) {            c.set(Calendar.DAY_OF_MONTH, i);            Date d = c.getTime();            list.add(d);        }        return list;    }    /**     * 在当前时间上加上指定的天数     *      * @param date     * @param plus     * @return     */    public static Date getAddDay(Date date, int plus) {        return new DateTime(date.getTime()).plusDays(plus).toDate();    }    /**     * 在当前时间上加上指定的月数     *      * @param date     * @param plus     * @return     */    public static Date getAddMonth(Date date, int plus) {        return new DateTime(date.getTime()).plusMonths(plus).toDate();    }    /**     * 在当前时间上加上指定的分钟数     *      * @param date     * @param plus     * @return     */    public static Date getAddMinutes(Date date, int plus) {        return new DateTime(date.getTime()).plusMinutes(plus).toDate();    }    /**     * 在当前时间上加上指定的小时数     *      * @param date     * @param plus     * @return     */    public static Date getAddHours(Date date, int plus) {        return new DateTime(date.getTime()).plusHours(plus).toDate();    }    /**     * 在当前时间上加上指定的周数     *      * @param date     * @param plus     * @return     */    public static Date getAddWeeks(Date date, int plus) {        return new DateTime(date.getTime()).plusWeeks(plus).toDate();    }    /**     * 在当前时间上加上指定的年数     *      * @param date     * @param plus     * @return     */    public static Date getAddYears(Date date, int plus) {        return new DateTime(date.getTime()).plusYears(plus).toDate();    }    /***     * 日期 转指定格式 时间戳     *      * @param date     * @param dateFormat     * @return     */    public static long getUnixDate(Date date, String dateFormat) {        SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);        long ctime = 0;        try {            ctime = formatter.parse(formatter.format(date)).getTime();        } catch (ParseException e) {            e.printStackTrace();        }        return ctime;    }    /**     * 判断指定日期是否在当前日期之前     *      * @param dateFormat yyyy-mm-dd     * @return     */    public static boolean judgeDateBefore(String dateFormat) {        DateTime d1 = new DateTime(dateFormat);        return d1.isBeforeNow();    }    /**     * 判断指定日期是否在当前日期之后     *      * @param dateFormat yyyy-mm-dd     * @return     */    public static boolean judgeDateAfter(String dateFormat) {        DateTime d1 = new DateTime(dateFormat);        return d1.isAfterNow();    }    /**     * 返回两个日期的时间差,     * 返回的时间差格式可以是: Calendar.YEAR, Calendar.MONTH, Calendar.DATE, Calendar.HOUR, Calendar.MINUTE, Calendar.SECOND     * 为空时,返回week的差     * @param earlyDate     * @param lateDate     * @param returnTimeFormat     * @return time     */    public static int getBetweenTime(Date earlyDate, Date lateDate, int returnTimeFormat) {        DateTime earlyDateTime = new DateTime(earlyDate);        DateTime lateDateTime = new DateTime(lateDate);        if (Calendar.YEAR == returnTimeFormat) {            return Years.yearsBetween(earlyDateTime, lateDateTime).getYears();        } else if (Calendar.MONTH == returnTimeFormat) {            return Months.monthsBetween(earlyDateTime, lateDateTime).getMonths();        } else if (Calendar.DATE == returnTimeFormat) {            return Days.daysBetween(earlyDateTime, lateDateTime).getDays();        } else if (Calendar.HOUR == returnTimeFormat) {            return Hours.hoursBetween(earlyDateTime, lateDateTime).getHours();        } else if (Calendar.MINUTE == returnTimeFormat) {            return Minutes.minutesBetween(earlyDateTime, lateDateTime).getMinutes();        } else if (Calendar.SECOND == returnTimeFormat) {            return Seconds.secondsBetween(earlyDateTime, lateDateTime).getSeconds();        } else {            return Weeks.weeksBetween(earlyDateTime, lateDateTime).getWeeks();        }    }    public static void main(String[] args) {        DateTime d1 = new DateTime("2016-11-30");        // 和系统时间比        System.out.println(d1.isAfterNow());        System.out.println(d1.isBeforeNow());        System.out.println(d1.isEqualNow());    }}
0 0
原创粉丝点击