JAVA 日期工具(待续)

1. 根据日期转星期
2. 根据日期获取前后n天的日期
3. 根据时间获取当前日份
4. 获取往前或往后退n个小时的整点时间
5. 获取往前或往之后n分钟的时间
6. 日期转字符串
7. 字符串转日期
8. 获取从现在起过去的天数
9. 获取从现在起过去的小时数
10. 获取从现在起过去的分钟数
11. 转换为时间(天,时:分:秒.毫秒)
12. 获取两个日期之间的天数

Hutool很给力的,不止日期工具哦,值得拥有。
参考文档:https://hutool.cn/docs/

1.根据日期转星期

/**
 * 
 * @title getWeekOfDate
 * @Description 根据日期转星期
 * @author chenlf
 * @param date
 * @param type 日期格式
 * @return
 */
public static String getWeekOfDate(Date date, String type) { 
	//type = 1
	String[] weekDaysName = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; 
	//type = 2
	String[] weekDaysCode = { "七","一", "二", "三", "四", "五", "六" }; 
	Calendar calendar = Calendar.getInstance(); 
	calendar.setTime(date); 
	int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
	type = StringUtils.isBlank(type)?"2":"1";
	switch (type) {
	case "1":
		return weekDaysName[intWeek]; 
	case "2":
		return weekDaysCode[intWeek]; 
	default:
		return weekDaysCode[intWeek]; 
	}
}

2.根据日期获取前后n天的日期)

/**
* 
 * @title getDate
 * @Description 根据日期获取前后n天的日期
 * @author chenlf
 * @param date
 * @param day
 * @return
 */
public static Date getDate(Date date, int day){
	Calendar c = Calendar.getInstance();
	c.setTime(date);
	c.add(Calendar.DATE, day);
	return c.getTime();
}

3.根据时间获取当前日份

/**
 * 
 * @title getDay
 * @Description 根据时间获取当前日份
 * @author chenlf
 * @param date
 * @return
 */
public static int getDay(Date date){
	Calendar c = Calendar.getInstance();
	c.setTime(date);
    return c.get(Calendar.DAY_OF_MONTH);
}

4.获取往前或往后退n个小时的整点时间

/**
 * 
 * @title getHourTime
 * @Description 获取往前或往后退n个小时的整点时间
 * 		eg:
 * 			13:12 --> 获取12:00  则  n=-1
 * 				  --> 获取14:00  则  n=1
 * @author chenlf
 * @param date 时间
 * @param n 小时
 * @return
 */
public static Date getHourTime (Date date, int n) {
	Calendar ca = Calendar.getInstance();
       ca.set(Calendar.MINUTE, 0);
       ca.set(Calendar.SECOND, 0);
       ca.set(Calendar.HOUR_OF_DAY, ca.get(Calendar.HOUR_OF_DAY)+n);
       return ca.getTime();
}

5.获取往前或往之后n分钟的时间

	
/**
 * 
 * @title getMinuteTime
 * @Description 获取往前或往之后n分钟的时间
 * @author chenlf
 * @param date 时间
 * @param min 分钟
 * @return
 */
public static Date getMinuteTime (Date date, int min) {
	Calendar ca = Calendar.getInstance();
	ca.setTime(date);
	ca.add(Calendar.MINUTE, min);
	return ca.getTime();
}

6.日期转字符串 (可以直接使用hutool工具包)

/**
 * 
 * @title getDateStr
 * @Description 日期转字符串
 * @param date 日期
 * @param pattern 格式
 * 				pattern为空时,默认yyyy-MM-dd
 * @return
 */
public static String getDate(Date date, String pattern){
	pattern = StringUtils.isBlank(pattern) ? "yyyy-MM-dd" : pattern;
	//cn.hutool.core.date.DateUtil 工具包
	return DateUtil.format(date, pattern);
	
}

7.字符串转日期)

/**
 * 
 * @title getDate
 * @Description 字符串转日期
 * @param date 字符串日期
 * @param pattern 格式
 * 				pattern为空时,默认yyyy-MM-dd
 * @return
 */
public static Date getDate(String date, String pattern){
	pattern = StringUtils.isBlank(pattern) ? "yyyy-MM-dd" : pattern;
	return DateUtil.parse(date,pattern);
}

8.获取从现在起过去的天数

/**
 * 
 * @title getPastDays
 * @Description 获取从现在起过去的天数
 * @param date 日期
 * @return
 */
public static long getPastDays(Date date) {
	long n = System.currentTimeMillis()-date.getTime();
	return n/(24*60*60*1000);
}

9.获取从现在起过去的小时数

/**
 * 
 * @title getPastHour
 * @Description 获取从现在起过去的小时数
 * @param date 日期
 * @return
 */
public static long getPastHour(Date date) {
	long h = System.currentTimeMillis()-date.getTime();
	return h/(60*60*1000);
}

10.获取从现在起过去的分钟数

/**
 * 
 * @title getPastMinutes
 * @Description 获取从现在起过去的分钟数
 * @param date 日期
 * @return
 */
public static long getPastMinutes(Date date) {
	long m = System.currentTimeMillis()-date.getTime();
	return m/(60*1000);
}

11.转换为时间(天,时:分:秒.毫秒)

/**
 * 
 * @title formatDateTime
 * @Description 转换为时间(天,时:分:秒.毫秒)
 * @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;
}

12.获取两个日期之间的天数

/**
  * 
  * @title getDistanceOfTwoDate
  * @Description 获取两个日期之间的天数
  * @param before
  * @param after
  * @return
  */
public static double getDistanceOfTwoDate(Date before, Date after) {
	long beforeTime = before.getTime();
	long afterTime = after.getTime();
	return (double)(afterTime - beforeTime)/(1000 * 60 * 60 * 24);
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值