温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java常用的时间相关转化有哪些

发布时间:2021-06-30 15:23:04 来源:亿速云 阅读:184 作者:chen 栏目:开发技术

这篇文章主要讲解了“Java常用的时间相关转化有哪些”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Java常用的时间相关转化有哪些”吧!

Java常用的时间相关转化

下面代码的一些变量基本解释说明
datePattern:时间对应的字符串格式
date: 时间
dateStr:字符串格式的时间
指定的几个常量:

public static final long DAYTIMESTAMP = 24 * 60 * 60 * 1000L; public static final  String SHORTDATEFORMATER = "yyyy-MM-dd"; public static final  String LONGDATEFORMATER = "yyyy-MM-dd HH:mm:ss";

1.时间转化为指定格式的字符串

public static final String convertDateToString(String datePattern, Date date) {	String returnValue = null;	if (date != null) {	SimpleDateFormat df = new SimpleDateFormat(datePattern);	returnValue = df.format(date);	}	return (returnValue);	}

2.指定格式的字符串转时间

public static final Date convertStringToDate(String datePattern,String dateStr) {	if( StringUtils.isBlank(dateStr) ){	return null;	}	SimpleDateFormat df = null;	Date date = null;	df = new SimpleDateFormat(datePattern);	try {	date = df.parse(dateStr);	} catch (ParseException pe) {	log.error("异常![{}]",pe);	return null;	}	return (date);	}

3.判断日期是否未过期

public static final boolean isNonExpired(Date date){	Calendar calendarNow = Calendar.getInstance();	calendarNow.setTime(calendarNow.getTime());	Calendar calendarGiven = Calendar.getInstance();	calendarGiven.setTime(date);	return calendarNow.before(calendarGiven);	}

4.判断日期是否过期

public static final boolean isExpired(Date date){	Calendar calendarNow = Calendar.getInstance();	calendarNow.setTime(calendarNow.getTime());	Calendar calendarGiven = Calendar.getInstance();	calendarGiven.setTime(date);	return calendarNow.after(calendarGiven);	}

5.判断两个日期大小

public static final int compare( Date firstDate,Date secondDate ){	return firstDate.compareTo(secondDate);	}

备注:如果第一个日期参数大于第二个日期返回 1;如果两个日期相等返回0;如果第一个日期小于第二个日期 返回-1

6.获取指定时间前n个月的时间

public static Date DateMinus(Date date,int month){	  Calendar calendar = Calendar.getInstance();	  calendar.setTime(date);	  calendar.add(Calendar.MONTH, -month);	return calendar.getTime();	}

7.获取指定日期之前指定天,包含传入的那一天

public static String getDaysBefore(Date date, int days) {	Date td = new Date(date.getTime() - DAYTIMESTAMP * days);	return DateUtils.convertDateToString(SHORTDATEFORMATER, td);	}

8.获取指定日期之前指定天的数组,包含传入的那一天

public static List<String> getDaysBeforeArray(Date date, int days){	List<String> resultList = new ArrayList<>();	for (int i = days-1; i >= 0; i--) {	resultList.add(getDaysBefore(date, i));	}	return resultList;	}

备注:配合第七条使用

9.获取指定时间的0点

public static Date getDayStartTimeByDate(Date date){	Calendar calendar = Calendar.getInstance();	calendar.setTime(date);	calendar.set(Calendar.HOUR_OF_DAY, 0);	calendar.set(Calendar.MINUTE, 0);	calendar.set(Calendar.SECOND, 0);	return calendar.getTime();	}

10.获取指定日期的最后一秒

public static Date getDayEndOfDay(Date date){	Calendar calendar = Calendar.getInstance();	calendar.setTime(date);	calendar.set(Calendar.HOUR_OF_DAY, 23);	calendar.set(Calendar.MINUTE, 59);	calendar.set(Calendar.SECOND, 59);	return calendar.getTime();	}

11.获取当时时间前一个小时时间

public static Date getDayBeforeHour(){	Calendar calendar = Calendar.getInstance();	calendar.set(Calendar.HOUR_OF_DAY,calendar.get(Calendar.HOUR_OF_DAY)-1);	return calendar.getTime();	}

12.获取两个时间之间相差的分钟数

public static String getdifferMinute(Date endDate, Date nowDate){	long nm = 1000 * 60;	// 获得两个时间的毫秒时间差异	long diff = endDate.getTime() - nowDate.getTime();	return String.valueOf(diff/nm);	}

备注:endDate 相对大的时间;nowDate 相对小的时间;可以在入参的时候就判断好,或者可以在方法内优化,即调用第五条操作根据返回值进行操作就可以。

13.获取两个时间之间间隔多少天

public static int differentDaysByMillisecond(Date date1,Date date2){	return (int) ((date2.getTime() - date1.getTime()) / (1000*3600*24));	}

14.获取两个时间之间的日期集合

public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate ) {         List<Date> dates = new ArrayList<>();         try{             dates.add(beginDate);// 把开始时间加入集合             Calendar cal = Calendar.getInstance();             // 使用给定的 Date 设置此 Calendar 的时间             cal.setTime(beginDate);             while (true) {                 // 根据日历的规则,为给定的日历字段添加或减去指定的时间量                 cal.add(Calendar.DAY_OF_MONTH, 1);                 // 测试此日期是否在指定日期之后                 if (endDate.after(cal.getTime())) {                     dates.add(cal.getTime());                 } else {                     break;                 }             }             dates.add(endDate);// 把结束时间加入集合         }catch(Exception e){             log.error("获取时间集合异常");         }         return dates;     }

15.获取当月月初第一天

public static String getMonthFirstDay() {	SimpleDateFormat format = new SimpleDateFormat(SHORTDATEFORMATER);	Calendar c = Calendar.getInstance();	c.add(Calendar.MONTH, 0);	c.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天	return format.format(c.getTime());	}

16.时间戳格式化

public static String parseDate(Long timeStamp){	String resDate = "";	if(null != timeStamp){	Date date = new Date(timeStamp);	SimpleDateFormat smf = new SimpleDateFormat(LONGDATEFORMATER);	resDate= smf.format(date);	}	return resDate;	}

17.获取今天是当前年第n周

public static int getWeekOfYear(String dateStr,int startCalendar){         SimpleDateFormat format = new SimpleDateFormat(SHORTDATEFORMATER);         Calendar calendar = Calendar.getInstance();         try {             Date date = format.parse(dateStr);             calendar.setFirstDayOfWeek(startCalendar);             calendar.setTime(date);         }         catch (Exception error) {             error.printStackTrace();         }         return calendar.get(Calendar.WEEK_OF_YEAR);     }

感谢各位的阅读,以上就是“Java常用的时间相关转化有哪些”的内容了,经过本文的学习后,相信大家对Java常用的时间相关转化有哪些这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI