package com.pflm.common.utils; import org.joda.time.DateTime; import org.joda.time.LocalDate; import org.joda.time.Period; import org.joda.time.format.DateTimeFormat; import org.joda.time.format.DateTimeFormatter; import java.text.SimpleDateFormat; import java.util.Date; /** * 日期处理 * * @author qinxuewu * @date 2016年12月21日 下午12:53:33 */ public class DateUtils { /** 时间格式(yyyy-MM-dd) */ public final static String DATE_PATTERN = "yyyy-MM-dd"; /** 时间格式(yyyy-MM-dd HH:mm:ss) */ public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; /** * 日期格式化 日期格式为:yyyy-MM-dd * @param date 日期 * @return 返回yyyy-MM-dd格式日期 */ public static String format(Date date) { return format(date, DATE_PATTERN); } /** * 日期格式化 日期格式为:yyyy-MM-dd * @param date 日期 * @param pattern 格式,如:DateUtils.DATE_TIME_PATTERN * @return 返回yyyy-MM-dd格式日期 */ public static String format(Date date, String pattern) { if(date != null){ SimpleDateFormat df = new SimpleDateFormat(pattern); return df.format(date); } return null; } public static String getNowTiemStr() { SimpleDateFormat df = new SimpleDateFormat(DATE_TIME_PATTERN); return df.format(new Date()); } /** * 字符串转换成日期 * @param strDate 日期字符串 * @param pattern 日期的格式,如:DateUtils.DATE_TIME_PATTERN */ public static Date stringToDate(String strDate, String pattern) { DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern); return fmt.parseLocalDateTime(strDate).toDate(); } /** * 根据周数,获取开始日期、结束日期 * @param week 周期 0本周,-1上周,-2上上周,1下周,2下下周 * @return 返回date[0]开始日期、date[1]结束日期 */ public static Date[] getWeekStartAndEnd(int week) { DateTime dateTime = new DateTime(); LocalDate date = new LocalDate(dateTime.plusWeeks(week)); date = date.dayOfWeek().withMinimumValue(); Date beginDate = date.toDate(); Date endDate = date.plusDays(6).toDate(); return new Date[]{beginDate, endDate}; } /** * 对日期的【秒】进行加/减 * * @param date 日期 * @param seconds 秒数,负数为减 * @return 加/减几秒后的日期 */ public static Date addDateSeconds(Date date, int seconds) { DateTime dateTime = new DateTime(date); return dateTime.plusSeconds(seconds).toDate(); } /** * 对日期的【分钟】进行加/减 * * @param date 日期 * @param minutes 分钟数,负数为减 * @return 加/减几分钟后的日期 */ public static Date addDateMinutes(Date date, int minutes) { DateTime dateTime = new DateTime(date); return dateTime.plusMinutes(minutes).toDate(); } /** * 对日期的【小时】进行加/减 * * @param date 日期 * @param hours 小时数,负数为减 * @return 加/减几小时后的日期 */ public static Date addDateHours(Date date, int hours) { DateTime dateTime = new DateTime(date); return dateTime.plusHours(hours).toDate(); } /** * 对日期的【天】进行加/减 * * @param date 日期 * @param days 天数,负数为减 * @return 加/减几天后的日期 */ public static Date addDateDays(Date date, int days) { DateTime dateTime = new DateTime(date); return dateTime.plusDays(days).toDate(); } /** * 对日期的【周】进行加/减 * * @param date 日期 * @param weeks 周数,负数为减 * @return 加/减几周后的日期 */ public static Date addDateWeeks(Date date, int weeks) { DateTime dateTime = new DateTime(date); return dateTime.plusWeeks(weeks).toDate(); } /** * 对日期的【月】进行加/减 * * @param date 日期 * @param months 月数,负数为减 * @return 加/减几月后的日期 */ public static Date addDateMonths(Date date, int months) { DateTime dateTime = new DateTime(date); return dateTime.plusMonths(months).toDate(); } /** * 对日期的【年】进行加/减 * * @param date 日期 * @param years 年数,负数为减 * @return 加/减几年后的日期 */ public static Date addDateYears(Date date, int years) { DateTime dateTime = new DateTime(date); return dateTime.plusYears(years).toDate(); } /** * 取两个日期间隔 * @param startDate * @param endDate * @return */ public static String getDateTimeBetween(Date startDate, Date endDate) { DateTime start = new DateTime(startDate.getTime()); DateTime end = new DateTime(endDate.getTime()); Period period = new Period(start, end); String result = period.getDays() + "天" + period.getHours() + "小时" + period.getMinutes() + "分" + period.getSeconds() + "秒"; return result; } /** * 取两个日期相隔多少分钟 * @param startDate * @param endDate * @return */ public static int getDateTimeMinutesBetween(Date startDate, Date endDate) { DateTime start = new DateTime(startDate.getTime()); DateTime end = new DateTime(endDate.getTime()); Period period = new Period(start, end); return period.getMinutes(); } /** * 传入秒 返回日时分秒 * @param second 秒 * @return */ public static String secondToTime(long second) { //转换天数 long days = second / 86400; //剩余秒数 second = second % 86400; //转换小时数 long hours = second / 3600; //剩余秒数 second = second % 3600; //转换分钟 long minutes = second / 60; //剩余秒数 second = second % 60; if (0 < days){ return days+"天"+hours+"小时"+minutes+"分"+second+"秒"; }else { return hours+"小时"+minutes+"分"+second+"秒"; } } public static String getNowHourMillisSecond(){ DateTime dt = new DateTime(); //获取当前时间的小时 int hour=dt.getHourOfDay(); //获取当前分钟 int millis=dt.getMinuteOfHour(); //获取当前时间的秒 int second = dt.getSecondOfMinute(); return hour+":"+millis+":"+second; } public static void main(String[] args) { System.err.println(getNowHourMillisSecond()); } }