差不多还有半个月就过年了,忆往昔,岁月匆匆。公司今年的项目已经收工,接下来的十来天留给复盘一年来的问题和年终总结。
"人生的重要法则:
1.原谅自己
2.不要在意他人的目光
3.好好睡觉"
下面总结下在java开发中常用的时间工具类:(以下为博主日常项目使用的方法,若有不妥欢迎指正)
0.字符串和日期之间的相互转换
/**
* 字符串转日期
* @param strDate
* @param pattern
* @return
*/
public static Date stringToDate(String strDate,String pattern) {
Date date = null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (strDate != null && !"".equals(strDate)) {
try {
date = sdf.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
}
return date;
}
/**
* 日期转字符串
* @param date
* @param pattern
* @return
*/
public static String dateToString(Date date,String pattern) {
String dateString = null;
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (date != null) {
dateString = sdf.format(date);
}
return dateString;
}
/*
* java.util.date转java.sql.date
*/
public static java.sql.Date dateToSqlDate(Date date) {
java.sql.Date sqlDate = null;
if (date != null) {
sqlDate = new java.sql.Date(date.getTime());
}
return sqlDate;
}
1.获取系统当前时间 yyyy-MM-dd/HH-mm-ss
/*
* 方式一:获取当天的日期 yyyy-MM-dd
*/
public static String getDay() {
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
date = calendar.getTime();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
//小写的hh表示12小时制,大些的HH表示24小时制
//如果想获取yyyy-MM-dd HH:mm:ss 格式的日期只需要修改("yyyy-MM-dd HH:mm:ss");
//方式二:当然也可以直接用SimpleDateFormat获取
public static String getCurrentTime() {
// 小写的hh取得12小时,大写的HH取的是24小时
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date();
return df.format(date);
}
}
2.获取系统当前时间yyyy-MM-dd的前一天或者后一天
/*
* 获取当天的日期 yyyy-MM-dd
*/
public static String getBeforeDay() {
Date date=new Date();
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
//使用.add(Calendar.DAY_OF_MONTH, -1) 前一天是-1,后一天是+1,以此类推前N天和后N天
calendar.add(Calendar.DAY_OF_MONTH, -1);
date = calendar.getTime();
SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd");
return format.format(date);
//如果想获取yyyy-MM-dd HH:mm:ss 格式的日期只需要修改("yyyy-MM-dd HH:mm:ss");
}
3.判断某一天是否在某个时间段内
/**
* 判断时间是否在时间段内
*
* @param nowTime
* @param beginTime
* @param endTime
* @return
*/
public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
Calendar date = Calendar.getInstance();
date.setTime(nowTime);
Calendar begin = Calendar.getInstance();
begin.setTime(beginTime);
Calendar end = Calendar.getInstance();
end.setTime(endTime);
if (date.after(begin) && date.before(end)) {
return true;
} else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
return true;
} else {
return false;
}
}
// 字符串 转 日期
public static Date strToDate(String str) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(str);
} catch (ParseException e) {
}
return date;
}
public static void main(String[] args) {
// 生效时间
Date effectivetime = strToDate("2018-7-8 17:00:00");
// 失效时间
Date invalidtime = strToDate("2018-9-8 17:30:00");
boolean flag = belongCalendar(new Date(), effectivetime, invalidtime);
System.out.println(flag);
}
4.获取系统当前时间戳
public static String getSystemTime() {
String current = String.valueOf(System.currentTimeMillis());
return current;
}
5.获取两个时间点的时间差
/**
* 得到两个时间差 格式yyyy-MM-dd HH:mm:ss
*
*/
public static long dateSubtraction(String start, String end) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date date1 = df.parse(start);
Date date2 = df.parse(end);
return date2.getTime() - date1.getTime();
} catch (ParseException e) {
e.printStackTrace();
return 0;
}
}
6.判断一个日期是当前星期的第几天
/**
* 判断一个日期是当前星期的第几天
* @return
*/
public static int getDayOfWeek() {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
return cal.get(Calendar.DAY_OF_WEEK) - 1;
}
7.计算date2 - date1之间相差的天数
/**
* 计算date2 - date1之间相差的天数
*
* @param date1
* @param date2
* @return
*/
public static int daysBetween(Date date1, Date date2) {
DateFormat sdf = new SimpleDateFormat("yyyyMMdd");
Calendar cal = Calendar.getInstance();
try {
Date d1 = sdf.parse(DateUtils.dateStr7(date1));
Date d2 = sdf.parse(DateUtils.dateStr7(date2));
cal.setTime(d1);
long time1 = cal.getTimeInMillis();
cal.setTime(d2);
long time2 = cal.getTimeInMillis();
return Integer.parseInt(String.valueOf((time2 - time1) / 86400000L));
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
8.计算date2 - date1之间相差的分钟
/**
* 计算date2 - date1之间相差的分钟
*
* @param date1
* @param date2
* @return
*/
public static int minutesBetween(Date date1, Date date2) {
Calendar cal = Calendar.getInstance();
cal.setTime(date1);
long time1 = cal.getTimeInMillis();
cal.setTime(date2);
long time2 = cal.getTimeInMillis();
if (time2 - time1 <= 0) {
return 0;
} else {
return Integer.parseInt(String.valueOf((time2 - time1) / 60000L)) + 1;
}
}
9.根据出生日期计算年龄
/**
* 根据出生日期计算年龄
*
* @param birthday 出生日期
* @return
*/
public static int getAge(Date birthday) {
int age = 0;
Calendar born = Calendar.getInstance();
Calendar now = Calendar.getInstance();
if (birthday != null) {
now.setTime(new Date());
born.setTime(birthday);
if (born.after(now)) {
throw new IllegalArgumentException("年龄不能超过当前日期");
}
age = now.get(Calendar.YEAR) - born.get(Calendar.YEAR);
int nowDayOfYear = now.get(Calendar.DAY_OF_YEAR);
int bornDayOfYear = born.get(Calendar.DAY_OF_YEAR);
if (nowDayOfYear < bornDayOfYear) {
age -= 1;
}
}
return age;
}
10.获取当前时间的年、月、日、分
/**
* java取得年,月,日,时,分
*/
Calendar calendar = Calendar.getInstance();
int now_m = calendar.get(Calendar.MONTH) + 1;//得到月份
int now_d = calendar.get(Calendar.DATE);//得到月份中今天的号数
int now_h = calendar.get(Calendar.HOUR_OF_DAY);//得到一天中现在的时间,24小时制
int now_mm = calendar.get(Calendar.MINUTE);//得到分钟数