package com.gkhy.safePlatform.emergency.utils;
|
|
import com.gkhy.safePlatform.commons.enums.E;
|
|
import java.text.DateFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
|
public class TimeUtils {
|
|
public static String formatDate(Date date) {
|
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
|
String sDate = f.format(date);
|
return sDate;
|
}
|
|
|
public static void main(String[] args) {
|
String XXX= "2022-09-01";
|
String EEE = formatDate(new Date());
|
int a = differentDays(XXX, EEE);
|
System.out.println(a);
|
}
|
|
|
/**
|
* 计算两个时间的间隔
|
* date2比date1多的天数
|
*/
|
public static int differentDays(String date1, String date2) {
|
int days = 0;
|
try {
|
DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
|
Date star = dft.parse(date1);
|
Date end = dft.parse(date2);//结束时间
|
Long starTime = star.getTime();
|
Long endTime = end.getTime();
|
Long num = endTime - starTime;//时间戳相差的毫秒数
|
days = (int) (num / 24 / 60 / 60 / 1000);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return days;
|
}
|
|
|
/**
|
* 获取某年第一天日期
|
*
|
* @param year 年份
|
* @return Date
|
*/
|
public static String getYearFirst(int year) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.clear();
|
calendar.set(Calendar.YEAR, year);
|
Date currYearFirst = calendar.getTime();
|
return formatDate(currYearFirst);
|
}
|
|
/**
|
* 获取某年最后一天日期
|
*
|
* @param year 年份
|
* @return Date
|
*/
|
public static String getYearLast(int year) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.clear();
|
calendar.set(Calendar.YEAR, year);
|
calendar.roll(Calendar.DAY_OF_YEAR, -1);
|
Date currYearLast = calendar.getTime();
|
|
return formatDate(currYearLast);
|
}
|
|
/**
|
* 获取某月第一天日期
|
*
|
* @param month 年份
|
* @return Date
|
*/
|
public static String getMonthFirst(int year, int month) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.clear();
|
calendar.set(Calendar.YEAR, year);
|
calendar.set(Calendar.MONTH, month - 1);
|
Date currYearFirst = calendar.getTime();
|
return formatDate(currYearFirst);
|
}
|
|
/**
|
* 获取某月最后一天日期
|
*
|
* @param month 年份
|
* @return Date
|
*/
|
public static String getMonthLast(int year, int month) {
|
Calendar calendar = Calendar.getInstance();
|
calendar.clear();
|
calendar.set(Calendar.YEAR, year);
|
calendar.set(Calendar.MONTH, month - 1);
|
calendar.roll(Calendar.DAY_OF_MONTH, -1);
|
Date currYearLast = calendar.getTime();
|
|
return formatDate(currYearLast);
|
}
|
|
|
}
|