jeesite中org.apache.commons.lang3.time学习总结

在学习jeesite中我们看到的jeesite中封装了对date使用的工具类,代码如下:

package com.thinkgem.jeesite.common.utils;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * 日期工具类, 继承org.apache.commons.lang.time.DateUtils类
 * @author ThinkGem
 * @version 2014-4-15
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
	
	private static String[] parsePatterns = {
		"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", 
		"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
		"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

	/**
	 * 得到当前日期字符串 格式(yyyy-MM-dd)
	 */
	public static String getDate() {
		return getDate("yyyy-MM-dd");
	}
	
	/**
	 * 得到当前日期字符串 格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String getDate(String pattern) {
		return DateFormatUtils.format(new Date(), pattern);
	}
	
	/**
	 * 得到日期字符串 默认格式(yyyy-MM-dd) pattern可以为:"yyyy-MM-dd" "HH:mm:ss" "E"
	 */
	public static String formatDate(Date date, Object... pattern) {
		String formatDate = null;
		if (pattern != null && pattern.length > 0) {
			formatDate = DateFormatUtils.format(date, pattern[0].toString());
		} else {
			formatDate = DateFormatUtils.format(date, "yyyy-MM-dd");
		}
		return formatDate;
	}
	
	/**
	 * 得到日期时间字符串,转换格式(yyyy-MM-dd HH:mm:ss)
	 */
	public static String formatDateTime(Date date) {
		return formatDate(date, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前时间字符串 格式(HH:mm:ss)
	 */
	public static String getTime() {
		return formatDate(new Date(), "HH:mm:ss");
	}

	/**
	 * 得到当前日期和时间字符串 格式(yyyy-MM-dd HH:mm:ss)
	 */
	public static String getDateTime() {
		return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 得到当前年份字符串 格式(yyyy)
	 */
	public static String getYear() {
		return formatDate(new Date(), "yyyy");
	}

	/**
	 * 得到当前月份字符串 格式(MM)
	 */
	public static String getMonth() {
		return formatDate(new Date(), "MM");
	}

	/**
	 * 得到当天字符串 格式(dd)
	 */
	public static String getDay() {
		return formatDate(new Date(), "dd");
	}

	/**
	 * 得到当前星期字符串 格式(E)星期几
	 */
	public static String getWeek() {
		return formatDate(new Date(), "E");
	}
	
	/**
	 * 日期型字符串转化为日期 格式
	 * { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", 
	 *   "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm",
	 *   "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm" }
	 */
	public static Date parseDate(Object str) {
		if (str == null){
			return null;
		}
		try {
			return parseDate(str.toString(), parsePatterns);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 获取过去的天数
	 * @param date
	 * @return
	 */
	public static long pastDays(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(24*60*60*1000);
	}

	/**
	 * 获取过去的小时
	 * @param date
	 * @return
	 */
	public static long pastHour(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*60*1000);
	}
	
	/**
	 * 获取过去的分钟
	 * @param date
	 * @return
	 */
	public static long pastMinutes(Date date) {
		long t = new Date().getTime()-date.getTime();
		return t/(60*1000);
	}
	
	/**
	 * 转换为时间(天,时:分:秒.毫秒)
	 * @param timeMillis
	 * @return
	 */
    public static String formatDateTime(long timeMillis){
		long day = timeMillis/(24*60*60*1000);
		long hour = (timeMillis/(60*60*1000)-day*24);
		long min = ((timeMillis/(60*1000))-day*24*60-hour*60);
		long s = (timeMillis/1000-day*24*60*60-hour*60*60-min*60);
		long sss = (timeMillis-day*24*60*60*1000-hour*60*60*1000-min*60*1000-s*1000);
		return (day>0?day+",":"")+hour+":"+min+":"+s+"."+sss;
    }
	
	/**
	 * 获取两个日期之间的天数
	 * 
	 * @param before
	 * @param after
	 * @return
	 */
	public static double getDistanceOfTwoDate(Date before, Date after) {
		long beforeTime = before.getTime();
		long afterTime = after.getTime();
		return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
	}
	
	/**
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
//		System.out.println(formatDate(parseDate("2010/3/6")));
//		System.out.println(getDate("yyyy年MM月dd日 E"));
//		long time = new Date().getTime()-parseDate("2012-11-19").getTime();
//		System.out.println(time/(24*60*60*1000));
	}
}

以上是jeesite中封装后的DateUtlis,下面是org.apache.commons.lang3.time总的DateUtlis中的使用总结

package bruce;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import org.apache.commons.lang3.time.DateUtils;

public class DateUtilsTest {
    public static void main(String[] args) {
        new DateUtilsTest();
    }

    public DateUtilsTest() {
        // 当前时间是大约是2016.1.21 9:36 am,每个test输出可能会查几秒到几分钟,因为例子不是一下子写完的
        test1();
        test2();
        test3();
        test4();
        test5();
        test6();
        test7();
        test8();
        test9();
        test10();
        test11();
        test12();
    }

    public void printFormatDate(Date d) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(df.format(d));
    }

    public void test1() {// 下个月第一天
        Date d = new Date();
        d = DateUtils.ceiling(d, Calendar.MONTH);// 进位月份
        printFormatDate(d);// 2016-02-01 00:00:00
    }

    public void test2() {// 下个月第一天
        Date d = new Date();
        d = DateUtils.addMonths(d, 1);// 月+1
        d = DateUtils.setDays(d, 1);// 设置日为1号
        d = DateUtils.truncate(d, Calendar.DATE);// 过滤时分秒
        printFormatDate(d);// 2016-02-01 00:00:00
    }

    public void test3() {// 当月最后一天最后一秒
        Date d = new Date();
        d = DateUtils.ceiling(d, Calendar.MONTH);// 进位月份
        d = DateUtils.addMilliseconds(d, -1);// 减少1秒
        printFormatDate(d);// 2016-01-31 23:59:59
    }

    public void test4() {// 当月第一天第一秒
        Date d = new Date();
        d = DateUtils.truncate(d, Calendar.MONTH);// 截取时间到月份
        printFormatDate(d);// 2016-01-01 00:00:00
    }

    public void test5() {// 下个月的这个时候
        Date d = new Date();
        d = DateUtils.addMonths(d, 1);
        printFormatDate(d);// 2016-02-21 09:46:02
    }

    public void test6() {// 昨天的这个时候
        Date d = new Date();
        d = DateUtils.addDays(d, -1);// 增加1月,如果下个月没有这1天,那就不加
        printFormatDate(d);// 2016-01-20 09:46:48
    }

    public void test7() {// addMonth的注意点:2月没有29号
        Date d = new Date();
        d = DateUtils.setDays(d, 29);
        d = DateUtils.addMonths(d, 1);// 并没有增加
        printFormatDate(d);// 2016-01-29 09:47:45
    }

    public void test8() {// 这个月15号
        Date d = new Date();
        d = DateUtils.setDays(d, 15);
        d = DateUtils.truncate(d, Calendar.DATE);// 截取时间到日
        printFormatDate(d);// 2016-01-15 00:00:00
    }

    public void test9() {// 输出包含今天的这个星期的每一天,星期第一天是周日
        Date d = new Date();
        Iterator<Calendar> c = DateUtils.iterator(d, DateUtils.RANGE_WEEK_SUNDAY);
        System.out.println();
        while (c.hasNext()) {
            printFormatDate(new Date(c.next().getTimeInMillis()));
        }
    }

    public void test10() {// 今天是今年第几天
        Date d = new Date();
        System.out.println(DateUtils.getFragmentInDays(d, Calendar.YEAR));// 21
    }

    public void test11() {// 这个月第一个周日
        Date d = new Date();
        d = DateUtils.setDays(d, 1);
        while (true) {
            Calendar c = Calendar.getInstance();
            c.setTime(d);
            if (c.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
                printFormatDate(d);// 2016-01-03 10:31:43
                break;
            } else {
                d = DateUtils.addDays(d, 1);
            }
        }
    }

    public void test12() {// 距2月1号还有多少天
        Date d = new Date();
        Date d2 = new Date();
        d2 = DateUtils.ceiling(d, Calendar.MONTH);// 2.1号
        long day2 = DateUtils.getFragmentInDays(d2, Calendar.YEAR);//2.1是今年第几天?
        long day1 = DateUtils.getFragmentInDays(d, Calendar.YEAR);//今天是今年第几天?
        System.out.println(day2 - day1);//11
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值