时间工具(jodaTime)

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Joda-Time 是一个 Java 编程语言的日期和时间处理库,它提供了简化日期和时间操作的功能。使用 Joda-Time,你可以轻松地进行日期和时间的计算、格式化、解析等操作。 下面是 Joda-Time 的一些常见用法: 1. 创建日期对象: ```java DateTime now = new DateTime(); // 创建当前日期和时间对象 DateTime specificDate = new DateTime(2022, 1, 1, 0, 0, 0); // 创建指定日期和时间对象 ``` 2. 获取日期和时间的各个部分: ```java int year = now.getYear(); // 获取年份 int month = now.getMonthOfYear(); // 获取月份 int day = now.getDayOfMonth(); // 获取日期 int hour = now.getHourOfDay(); // 获取小时 int minute = now.getMinuteOfHour(); // 获取分钟 int second = now.getSecondOfMinute(); // 获取秒数 ``` 3. 格式化日期和时间: ```java String formattedDate = now.toString("yyyy-MM-dd"); // 将日期格式化为指定格式的字符串 String formattedTime = now.toString("HH:mm:ss"); // 将时间格式化为指定格式的字符串 String formattedDateTime = now.toString("yyyy-MM-dd HH:mm:ss"); // 将日期和时间格式化为指定格式的字符串 ``` 4. 解析字符串为日期对象: ```java DateTime parsedDate = DateTime.parse("2022-01-01"); // 解析字符串为日期对象 ``` 5. 对日期进行计算和操作: ```java DateTime modifiedDate = now.plusDays(7); // 将日期加上指定天数 DateTime result = now.minusYears(1).plusMonths(3); // 对日期进行复合操作 ``` 以上是 Joda-Time 的一些基本用法,你可以根据自己的需求进一步探索该工具类的其他功能。请注意,Joda-Time 在 Java 8 及更高版本中已经被官方的 java.time 包所取代,因此在使用新的 Java 版本时,你可以直接使用 java.time 包中的类来处理日期和时间

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值