在日常编程工作中,我们常常会使用到日期处理的,为此,我将对日期时间的处理封装成了一个工具类
1、对日期时间的处理过程,我放在了 DateUtils.java 中
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
/**
* @ClassName DateUtils
* 日期时间处理工具类
* @Author Lizhou
* @Date 2019-09-05 12:43:43
* @Version 1.0
**/
public class DateUtils {
/**
* 获取当前时间,yyyy-MM-dd HH:mm:ss
*/
public static String getYmdHms() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(new Date());
}
/**
* 时间格式化,yyyy-MM-dd HH:mm:ss
*/
public static String getYmdHms(Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return df.format(date);
}
/**
* 时间格式化,yyyy-MM-dd HH:mm:ss
*/
public static Date getYmdHms(String date) {
try {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取当前日期,yyyy-MM-dd
*/
public static String getYmd() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(new Date());
}
/**
* 日期格式化,yyyy-MM-dd
*/
public static String getYmd(Date date) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
return df.format(date);
}
/**
* 日期格式化,yyyy-MM-dd
*/
public static Date getYmd(String date) {
try {
return new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取本月的天数
*/
public static int getCurrentMonthDays() {
Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
return aCalendar.getActualMaximum(Calendar.DATE);
}
/**
* 获取本月第一天,yyyy-MM-dd
*/
public static String getCurrentMonthFirstDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date());
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return sdf.format(calendar.getTime());
}
/**
* 获取本月最后一天,yyyy-MM-dd
*/
public static String getCurrentMonthLastDay() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(new Date());
calendar2.set(Calendar.DAY_OF_MONTH, calendar2.getActualMaximum(Calendar.DAY_OF_MONTH));
return sdf.format(calendar2.getTime());
}
/**
* 获取某个月的第一天,yyyy-MM-dd
*/
public static String getThisMonthFirstDay(String month) {
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf1.parse(month));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
return sdf2.format(calendar.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取某个月的最后一天,yyyy-MM-dd
*/
public static String getThisMOnthLastDay(String month) {
try {
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM");
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = Calendar.getInstance();
calendar.setTime(sdf1.parse(month));
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
return sdf2.format(calendar.getTime());
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 获取该日期往前推多少天的日期,yyyy-MM-dd
*/
public static String getThisDayBeforeDate(Date date, int days) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, -days);
return sdf.format(calendar.getTime());
}
/**
* 获取该日期往后推多少天的日期,yyyy-MM-dd
*/
public static String getThisDayAfterDate(Date date, int days) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.add(Calendar.DATE, days);
return sdf.format(calendar.getTime());
}
/**
* 获取本周周一,yyyy-MM-dd
*/
public static String getCurrentWeekMonday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
String imptimeBegin = sdf.format(cal.getTime());
cal.add(Calendar.DATE, 6);
return imptimeBegin;
}
/**
* 获取本周周日,yyyy-MM-dd
*/
public static String getCurrentWeekSunday() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
// 判断要计算的日期是否是周日,如果是则减一天计算周六的,否则会出问题,计算到下一周去了
int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
if (1 == dayWeek) {
cal.add(Calendar.DAY_OF_MONTH, -1);
}
// 设置一个星期的第一天,按中国的习惯一个星期的第一天是星期一
cal.setFirstDayOfWeek(Calendar.MONDAY);
// 获得当前日期是一个星期的第几天
int day = cal.get(Calendar.DAY_OF_WEEK);
// 根据日历的规则,给当前日期减去星期几与一个星期第一天的差值
cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
cal.add(Calendar.DATE, 6);
String imptimeEnd = sdf.format(cal.getTime());
return imptimeEnd;
}
/**
* 补全时间,开始时间
*/
public static String completionTimeStart(String date) {
return date + " 00:00:00";
}
/**
* 补全时间,结束时间
*/
public static String completionTimeEnd(String date) {
return date + " 23:59:59";
}
/**
* 获取两个时间的时间差
*/
public static String getDateTimeDiffer(Date endDate, Date nowDate) {
long nd = 1000 * 24 * 60 * 60;
long nh = 1000 * 60 * 60;
long nm = 1000 * 60;
// long ns = 1000;
// 获得两个时间的毫秒时间差异
long diff = endDate.getTime() - nowDate.getTime();
// 计算差多少天
long day = diff / nd;
// 计算差多少小时
long hour = diff % nd / nh;
// 计算差多少分钟
long min = diff % nd % nh / nm;
// 计算差多少秒//输出结果
return day + "天" + hour + "时" + min + "分";
}
/**
* 根据生日算年龄
*/
private static int getAgeByBirthDay(Date birthday) {
int age;
try {
Calendar now = Calendar.getInstance();
now.setTime(new Date());
Calendar birth = Calendar.getInstance();
birth.setTime(birthday);
//如果传入的时间,在当前时间的后面,返回0岁
if (birth.after(now)) {
age = 0;
} else {
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
age += 1;
}
}
return age;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}
如您在阅读中发现不足,欢迎留言!!!