一、时间工具类实例代码
public class DateUtilsTest {
public static void main(String[] args) {
//1.获取当前Date型日期
Date nowDate = DateUtils.getNowDate();
System.out.println("获取当前Date型日期:"+nowDate);
System.out.println();
//2.获取当前日期, 默认格式为yyyy-MM-dd
String date = DateUtils.getDate();
System.out.println("获取当前日期, 默认格式为yyyy-MM-dd:"+date);
System.out.println();
//3.获取当前日期, 默认格式为yyyy-MM-dd HH:mm:ss
String time = DateUtils.getTime();
System.out.println("获取当前日期, 默认格式为yyyy-MM-dd HH:mm:ss:"+time);
System.out.println();
//4.获取当前日期, 默认格式为yyyyMMddHHmmss
String dateTimeNow = DateUtils.dateTimeNow();
System.out.println("获取当前日期, 默认格式为yyyyMMddHHmmss:"+dateTimeNow);
System.out.println();
//5.日期路径 即年/月/日 如2018/08/08
String datePath = DateUtils.datePath();
System.out.println("日期路径 即年/月/日 如2018/08/08:"+datePath);
System.out.println();
//6.日期路径 即年/月/日 如20180808
String dateTime = DateUtils.dateTime();
System.out.println("日期路径 即年/月/日 如20180808:"+dateTime);
System.out.println();
//7.获取服务器启动时间
Date serverStartDate = DateUtils.getServerStartDate();
String parseDateToStr = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, serverStartDate);
System.out.println("获取服务器启动时间:"+parseDateToStr);
System.out.println();
//8.计算两个时间差
Date startDate = DateUtils.parseDate("2021-09-16 15:53:16");
Date endDate = DateUtils.getNowDate();
String datePoor = DateUtils.getDatePoor(endDate, startDate);
System.out.println(startDate + "\t和\t" + endDate + "\t相差" + datePoor);
System.out.println();
//9.获取近七天日期
List lastSixDayData = DateUtils.getSevenDate();
System.out.println(lastSixDayData);
System.out.println();
//10.获取近六个月数据
List<String> lastSixMonthDate = DateUtils.getLastSixMonthDate();
System.out.println(lastSixMonthDate);
System.out.println();
//11.获取近六个月数据
List sixMonthData = DateUtils.getLastSixMonthData(2021, 4);
System.out.println(sixMonthData);
System.out.println();
//12.获取近六年日期
List<String> lastSixYearDate = DateUtils.getLastSixYearDate();
System.out.println(lastSixYearDate);
System.out.println();
}
}
二、时间工具类
/**
* 时间工具类
*
* @author
*/
public class DateUtils extends org.apache.commons.lang3.time.DateUtils
{
public static String YYYY = "yyyy";
public static String YYYY_MM = "yyyy-MM";
public static String YYYY_MM_DD = "yyyy-MM-dd";
public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
private static String[] parsePatterns = {
"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM",
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM",
"yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};
/**
* 获取当前Date型日期
*
* @return Date() 当前日期
*/
public static Date getNowDate()
{
return new Date();
}
/**
* 获取当前日期, 默认格式为yyyy-MM-dd
*
* @return String
*/
public static String getDate()
{
return dateTimeNow(YYYY_MM_DD);
}
public static final String getTime()
{
return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
}
public static final String dateTimeNow()
{
return dateTimeNow(YYYYMMDDHHMMSS);
}
public static final String dateTimeNow(final String format)
{
return parseDateToStr(format, new Date());
}
public static final String dateTime(final Date date)
{
return parseDateToStr(YYYY_MM_DD, date);
}
public static final String parseDateToStr(final String format, final Date date)
{
return new SimpleDateFormat(format).format(date);
}
public static final Date dateTime(final String format, final String ts)
{
try
{
return new SimpleDateFormat(format).parse(ts);
}
catch (ParseException e)
{
throw new RuntimeException(e);
}
}
/**
* 日期路径 即年/月/日 如2018/08/08
*/
public static final String datePath()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyy/MM/dd");
}
/**
* 日期路径 即年/月/日 如20180808
*/
public static final String dateTime()
{
Date now = new Date();
return DateFormatUtils.format(now, "yyyyMMdd");
}
/**
* 日期型字符串转化为日期 格式
*/
public static Date parseDate(Object str)
{
if (str == null)
{
return null;
}
try
{
return parseDate(str.toString(), parsePatterns);
}
catch (ParseException e)
{
return null;
}
}
/**
* 获取服务器启动时间
*/
public static Date getServerStartDate()
{
long time = ManagementFactory.getRuntimeMXBean().getStartTime();
return new Date(time);
}
/**
* 计算两个时间差
*/
public static String getDatePoor(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;
// 计算差多少秒//输出结果
// long sec = diff % nd % nh % nm / ns;
return day + "天" + hour + "小时" + min + "分钟";
}
//获取近七天日期
public static List<String> getSevenDate() {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < 7; i++) {
Date date = DateUtils.addDays(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
//获取近六月日期
public static List<String> getLastSixMonthDate() {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
for (int i = 0; i < 6; i++) {
Date date = DateUtils.addMonths(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
/**
* @Author chy
* @Description 获取近六个月的月份数据
* @Date
* @Param
* @return
**/
public static List getLastSixMonthData(int year,int month){
List resultList = new ArrayList();
Calendar cal = Calendar.getInstance();
//要先+1,才能把本月的算进去
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, month);
for(int i=0; i<6; i++) {
//逐次往前推1个月
cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
resultList.add(String.valueOf(cal.get(Calendar.YEAR)) + (cal.get(Calendar.MONTH) + 1 < 10 ? "0" + (cal.get(Calendar.MONTH) + 1) : (cal.get(Calendar.MONTH) + 1)));
}
return resultList;
}
//获取近六年日期
public static List<String> getLastSixYearDate() {
List<String> dateList = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
for (int i = 0; i < 6; i++) {
Date date = DateUtils.addYears(new Date(), -i);
String formatDate = sdf.format(date);
dateList.add(formatDate);
}
return dateList;
}
}
三、示例代码
JavaUtilsDemo: java工具类实例代码