概述
实用日期工具包
/**
* 获取指定格式的时间 (默认:yyyy-MM-dd HH:mm:ss)
*/
public static String getStrToDate(Date date,String pattern) {
if (pattern == null || "".equals(pattern)) {
pattern = "yyyy-MM-dd HH:mm:ss";
}
return DateFormatUtils.format(date,pattern);
}
/**
* 字符串日期转Date (默认:yyyy-MM-dd HH:mm:ss)
*/
public static Date getDateToStr(String dateString,String pattern) throws ParseException {
if (pattern == null || "".equals(pattern)) {
pattern = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date date = sdf.parse(dateString);
return date;
}
/**
* 指定时间 相加或相减 几月
*/
public static Date getDateByMonths(Date date,String pattern,Integer month){
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Calendar c = Calendar.getInstance();
c.setTime(date);
c.add(Calendar.MONTH, month);
try {
return getStrToDate(sdf.format(c.getTime()),pattern);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 日期比较 (如果s>=e 返回true 否则返回false)
*/
public static boolean compareDate(String s, String e) {
Date start = getDateToStr(s, "yyyy-MM-dd");
Date end= getDateToStr(s, "yyyy-MM-dd");
if (start == null || end == null) {
return false;
}
return start .getTime() >= end.getTime();
}
/**
* 把日期转换为Timestamp
*/
public static Timestamp format(Date date) {
return new Timestamp(date.getTime());
}
/**
* 校验日期是否合法
*/
public static boolean isValidDate(String s, String pattern) {
return parse(s, pattern) != null;
}
/**
* 获取两个时间内相差的年限
* @return
*/
public static int getDiffYear(String startTime, String endTime) {
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
try {
int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
return years;
} catch (Exception e) {
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
return 0;
}
}
/**
* 功能描述:时间相减得到天数
*/
public static long getDiffDay(String beginDateStr, String endDateStr) {
long day = 0;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date beginDate = null;
Date endDate = null;
try {
beginDate = format.parse(beginDateStr);
endDate = format.parse(endDateStr);
} catch (ParseException e) {
e.printStackTrace();
}
day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
return day;
}
/**
* 时间相减得到小时数
*/
public static Integer getDiffHour(Date beginDate, Date endDate){
Long day = (endDate.getTime() - beginDate.getTime()) / (60 * 60 * 1000);
return day.intValue();
}
/**
* 获取当前日期N天之后或之前的日期
*/
public static Date getAfterDateByDay(Integer days) {
Calendar canlendar = Calendar.getInstance();
canlendar.add(Calendar.DATE, days);
Date date = canlendar.getTime();
return date;
}
/**
* 得到n天之后是周几
*/
public static String getAfterWeekDay(String days) {
int daysInt = Integer.parseInt(days);
Calendar canlendar = Calendar.getInstance();
canlendar.add(Calendar.DATE, daysInt);
Date date = canlendar.getTime();
SimpleDateFormat sdf = new SimpleDateFormat("E");
String dateStr = sdf.format(date);
return dateStr;
}
/**
* 获取指定时间N月后/前的日期字符串
*/
public static String getAfterMonth(String inputDate,int number) {
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try{
date = sdf.parse(inputDate);
}catch(Exception e){
e.printStackTrace();
}
c.setTime(date);//设置日历时间
c.add(Calendar.MONTH,number);
String strDate = sdf.format(c.getTime());
return strDate;
}
/**
* 获取指定日期的月份的第一天
*/
public static String getFirstDayOfMonth(Date date) {
Integer month = Integer.parseInt(DateFormatUtils.format(date, "MM"));
Calendar cal = Calendar.getInstance();
// 设置月份
cal.set(Calendar.MONTH, month - 1);
// 获取某月最小天数
int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
// 设置日历中月份的最小天数
cal.set(Calendar.DAY_OF_MONTH, firstDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String firstDayOfMonth = sdf.format(cal.getTime())+" 00:00:00";
return firstDayOfMonth;
}
/**
* 获取指定日期的月份的最后一天
*/
public static String getLastDayOfMonth(Date date) {
Integer month = Integer.parseInt(DateFormatUtils.format(date, "MM"));
Calendar cal = Calendar.getInstance();
// 设置月份
cal.set(Calendar.MONTH, month - 1);
// 获取某月最大天数
int lastDay=0;
//2月的平年瑞年天数
if(month==2) {
lastDay = cal.getLeastMaximum(Calendar.DAY_OF_MONTH);
}else {
lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
}
// 设置日历中月份的最大天数
cal.set(Calendar.DAY_OF_MONTH, lastDay);
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String lastDayOfMonth = sdf.format(cal.getTime())+" 23:59:59";
return lastDayOfMonth;
}
/**
* 获取上个月第一天
* @return
*/
public static String getLastMonthFirstDay(){
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//获取前一个月第一天
Calendar calendar1 = Calendar.getInstance();
calendar1.add(Calendar.MONTH, -1);
calendar1.set(Calendar.DAY_OF_MONTH,1);
String firstDay = sdf.format(calendar1.getTime());
return firstDay+" 00:00:00";
}
/**
* 获取上个月最后一天
* @return
*/
public static String getLastMonthLastDay(){
// 格式化日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//获取前一个月最后一天
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, 0);
String lastDay = sdf.format(calendar2.getTime());
System.out.println(lastDay);
return lastDay+" 23:59:59";
}
/**
* 获取分钟差 ps:date1>date2
*
* @param date1 日期时间1
* @param date2 日期时间2
* @return 返回分钟数,
*/
public static Double getDistanceMinuteD(Date date1, Date date2) {
try {
Long l = (date1.getTime() - date2.getTime()) / (1000);
Double d = l.intValue()/60d;
return new BigDecimal(d).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
} catch (Exception e) {
e.printStackTrace();
}
return 0d;
}
/**
* 通过生日计算年龄
*/
public static int getAgeByBirth(Date birthDay) throws ParseException {
int age = 0;
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
throw new IllegalArgumentException( "生日大于当前时间!");
}
int yearNow = cal.get(Calendar.YEAR); //当前年份
int monthNow = cal.get(Calendar.MONTH); //当前月份
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
age = yearNow - yearBirth; //计算整岁数
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
if (dayOfMonthNow < dayOfMonthBirth) age--;//当前日期在生日之前,年龄减一
} else {
age--;//当前月份在生日之前,年龄减一
}
}
return age;
}
/**
* 判断当前时间是否在[startTime, endTime]区间,注意时间格式要一致
*
* @param nowTime 当前时间
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
if (nowTime.getTime() == startTime.getTime()
|| nowTime.getTime() == endTime.getTime()) {
return true;
}
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(startTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else {
return false;
}
}
/**
* 判断时间是否在小时:分钟区间
* @param now 当前时间
* @param startDate 开始时间
* @param endDate 结束时间
* @return
*/
public static boolean insideTimeLimit(Date now ,String startDate,String endDate) {
Integer hour = Integer.valueOf(timeToString(now,"HH"));
Integer minute = Integer.valueOf(timeToString(now,"mm"));
boolean flag = true;
boolean temp = false;
//startArr 下标0为小时,下标1为分钟。endArr同
String[] startArr = startDate.split(":");
String[] endArr = endDate.split(":");
if (hour == Integer.valueOf(endArr[0]) && minute == Integer.valueOf(endArr[1]))
return true;
if (hour == Integer.valueOf(startArr[0]) && minute == Integer.valueOf(startArr[1]))
return true;
if (Integer.valueOf(endArr[0]) < Integer.valueOf(startArr[0]) || (Integer.valueOf(endArr[0]) == Integer.valueOf(startArr[0]) && Integer.valueOf(endArr[1]) < Integer.valueOf(startArr[1]))) {
temp = true;
String[] tempArr = startArr;
startArr = endArr;
endArr = tempArr;
}
//1当前小时如果小于开始小时 直接返回 false
if (hour < Integer.valueOf(startArr[0]))
flag = false;
//2当前小时等于开始小时,但分钟数小于开始分钟数 直接返回 false
if (hour == Integer.valueOf(startArr[0]) && minute < Integer.valueOf(startArr[1]))
flag = false;
//同1
if (hour > Integer.valueOf(endArr[0]))
flag = false;
//同2
if (hour == Integer.valueOf(endArr[0]) && minute > Integer.valueOf(endArr[1]))
flag = false;
if (temp) {
flag = !flag;
}
return flag;
}
最后
以上就是无私白猫为你收集整理的Java日期的各式转换的全部内容,希望文章能够帮你解决Java日期的各式转换所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复