通用工具类,满足大量需求(可自行补充)
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.Locale;
/**
* 备注知识点
* 1.yyyy 与 YYYY的区别 :yyyy可避免年尾出现年份bug
* 2.dd 与 DD的区别:大写的DD代表的是处于这一年中那一天,不是处于这个月的那一天
* 3.hh 与 HH的区别:大写的HH代表的是24时制
* 所以我们均采用 yyyy-MM-dd HH:mm:ss的格式
*
* 工具类中 Date 和 Calendar的简单区别
* a、Date类表示的是特定的,瞬间的,它能精确毫秒。
*
* b、Calendar它是一种抽象类,相比Date它在操作日历的时候提供了一些方法来操作日历字段
*/
/**
* @version: v1.0
* @description: 时间日期工具类
* @author: 小黑
* @date :2019/6/6
*/
public class DateUtils {
static SimpleDateFormat format;
/**
* 日期格式:yyyy-MM-dd HH:mm:ss
**/
public static final String DF_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
/**
* 日期格式:yyyy-MM-dd HH:mm
**/
public static final String DF_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
/**
* 日期格式:yyyy-MM-dd
**/
public static final String DF_YYYY_MM_DD = "yyyy-MM-dd";
/**
* 日期格式:yyyy-MM-dd
**/
public static final String DF_YYYY_MM= "yyyy-MM";
/**
* 日期格式:HH:mm:ss
**/
public static final String DF_HH_MM_SS = "HH:mm:ss";
/**
* 日期格式:HH:mm
**/
public static final String DF_HH_MM = "HH:mm";
/**
* 获取系统当前日期
*
* @return
*/
public static Date getCurrentDate() {
return new Date();
}
/**
* 将日期以yyyy-MM-dd HH:mm:ss等指定格式进行格式化
*
* @param date 日期
* @param formater 格式化样式
* @return 格式化字符串
*/
public static String formatDateTime(Date date, String formater) {
try {
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}catch (Exception e){
return "";
}
}
/**
* 将日期字符串转成日期
*
* @param strDate 字符串日期
* @return java.util.date日期类型
*/
public static Date parseDate(String strDate) {
DateFormat dateFormat = new SimpleDateFormat(DF_YYYY_MM_DD_HH_MM_SS);
Date returnDate = null;
try {
returnDate = dateFormat.parse(strDate);
} catch (ParseException e) {
e.printStackTrace();
}
return returnDate;
}
/**
* 将字符型转换成日期型 (格式化字符串)
*/
public static String getStringFromString(String strDate) {
String returnStr = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
try {
Date date = sdf.parse(strDate);
returnStr = sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return returnStr;
}
/**
* 将字符型转换成日期型(格式化字符串)
*/
public static String getStringFromString1(String strDate) {
String returnStr = "";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
Date date = sdf.parse(strDate);
returnStr = sdf.format(date);
} catch (Exception e) {
e.printStackTrace();
}
return returnStr;
}
/**
* 将日期转成毫秒数
* @param date 日期
* @return
*/
public static long getLongTimeDate(Date date){
Long longTimeDate = 0L;
longTimeDate = date.getTime();
return longTimeDate;
}
/**
* 验证日期是否比当前日期早
*
* @param target1 比较时间1
* @param target2 比较时间2
* @return true 则代表target1比target2晚或等于target2,否则比target2早
*/
public static boolean compareDate(Date target1, Date target2) {
boolean flag = false;
try {
String target1DateTime = formatDateTime(target1, DF_YYYY_MM_DD_HH_MM_SS);
String target2DateTime = formatDateTime(target2, DF_YYYY_MM_DD_HH_MM_SS);
if (target1DateTime.compareTo(target2DateTime) <= 0) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
/**
* 比较日期
*
* @param date1
* @param date2
* @return
*/
public static boolean compareDate(String date1, String date2) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
try {
Date dt1 = df.parse(date1);
Date dt2 = df.parse(date2);
if (dt1.getTime() > dt2.getTime()) {
return true;
} else {
return false;
}
} catch (Exception exception) {
exception.printStackTrace();
}
return false;
}
/**
* 倒计时相关 此处仅一小时内
*/
public static String formatLongToTimeStr(Long l) {
int minute = 0;
int second = 0;
String str_minute = "00";
String str_second = "00";
second = l.intValue();
if (second > 60) {
minute = second / 60; //取整
second = second % 60; //取余
}
if (minute >= 0 && minute < 10) {
str_minute = "0" + minute;
} else {
str_minute = minute + "";
}
if (second >= 0 && second < 10) {
str_second = "0" + second;
} else {
str_second = second + "";
}
String strtime = str_minute + " : " + str_second;
return strtime;
}
/**
* 倒计时相关 1小时以上
*/
public static String formatLongToTimeStr2(Long l) {
int hour = 0;
int minute = 0;
int second = 0;
String str_hour = "00";
String str_minute = "00";
String str_second = "00";
second = l.intValue();
if (second > 60) {
minute = second / 60; //取整
second = second % 60; //取余
}
if (minute > 60) {
hour = minute / 60;
minute = minute % 60;
}
if (hour >= 0 && hour < 10) {
str_hour = "0" + hour;
} else {
str_hour = minute + "";
}
if (minute >= 0 && minute < 10) {
str_minute = "0" + minute;
} else {
str_minute = minute + "";
}
if (second >= 0 && second < 10) {
str_second = "0" + second;
} else {
str_second = second + "";
}
return str_hour + " : " + str_minute + " : " + str_second;
}
/**
* 两个时间之间相差距离多少天
*
* @param str1 时间参数 1:
* @param str2 时间参数 2:
* @return 相差天数
*/
public static long getDistanceDays(String str1, String str2) {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date one;
Date two;
long days = 0;
try {
one = df.parse(str1);
two = df.parse(str2);
long time1 = one.getTime();
long time2 = two.getTime();
long diff;
if (time1 < time2) {
diff = time2 - time1;
} else {
diff = time1 - time2;
}
days = diff / (1000 * 60 * 60 * 24);
} catch (Exception e) {
e.printStackTrace();
}
return days;
}
/**
* 日期转星期
*
* @param datetime
* @return
*/
public static String dateToWeek(String datetime) {
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar cal = Calendar.getInstance(); // 获得一个日历
Date datet = null;
try {
datet = f.parse(datetime);
cal.setTime(datet);
} catch (ParseException e) {
e.printStackTrace();
}
int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
if (w < 0)
w = 0;
return weekDays[w];
}
}