import org.springframework.stereotype.Component;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* 时间处理工具类
*
* @author wukun
*/
@Component
public class DateUtils {
/**
* 获得的是自1970-1-01 00:00:00.000 到当前时刻的时间距离
*
* @return long
*/
public long getCurrentDate() {
return Instant.now().toEpochMilli();
}
/**
* 时间戳转换为年月日
*
* @param timeStamp 时间戳
* @return
*/
public String timeStampToYMD(Long timeStamp) {
//这个是你要转成后的时间的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
// 时间戳转换成时间
return sdf.format(new Date(timeStamp));
}
/**
* 获取当前年月日
*
* @return
*/
public String getCurrentYMD() {
return LocalDate.now().toString();
}
/**
* 获取当前年月
*
* @return
*/
public String getCurrentYM() {
LocalDate localDate = LocalDate.now();
// 时间戳转换成时间
return localDate.getYear() + "-" + localDate.getMonthValue();
}
/**
* 获取当前月日
*
* @return
*/
public String getCurrentMD() {
LocalDate localDate = LocalDate.now();
// 时间戳转换成时间
return localDate.getMonthValue() + "-" + localDate.getDayOfMonth();
}
/**
* 年月日转换为时间戳
*
* @param date 时间 yyyy-MM-dd
* @return
*/
public Long ymdToTimeStamp(String date) {
return LocalDate.parse(date).atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
}
/**
* date转年月日时分秒
*
* @param date
* @return
*/
public String dateToYmdHms(Date date) {
return date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime().
format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
/**
* date转年月日
*
* @param date
* @return
*/
public String dateToYmd(Date date) {
return date.toInstant().atOffset(ZoneOffset.ofHours(8)).toLocalDateTime().
format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
/**
* 年月日 转时间戳
*
* @param localDate
* @return
*/
public Long localDateToInstant(LocalDate localDate) {
return localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
}
/**
* 年月日时分秒转时间戳
*
* @param localDateTime
* @return
*/
public Long localDateToInstant(LocalDateTime localDateTime) {
return localDateTime.toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
}
/**
* 几天后的时间戳
*
* @param days
* @return
*/
public Long timePlusDays(Date startTime, Integer days) {
return startTime
.toInstant()
.atOffset(ZoneOffset.ofHours(8))
.toLocalDate()
.plusDays(days)
.atStartOfDay(ZoneOffset.ofHours(8))
.toInstant().toEpochMilli();
}
/**
* 几天后的时间戳
*
* @param days
* @return
*/
public String timePlusDaysToYmd(Date startTime, Integer days) {
LocalDate localDate = startTime
.toInstant()
.atOffset(ZoneOffset.ofHours(8))
.toLocalDate()
.plusDays(days);
return localDate.toString();
}
/**
* 几天后的时间戳
*
* @param localDate
* @return
*/
public Date localDateToDate(LocalDate localDate) {
return Date.from(localDate.atStartOfDay(ZoneOffset.ofHours(8)).toInstant());
}
/**
* 获取昨天的日期
*
* @return
*/
public String getYesterdayDate() {
return LocalDate.now().plusDays(-1).toString();
}
/**
* 秒转换时分秒
*
* @param time 秒
* @return
*/
public String getSecondToHMS(int time) throws Exception {
int hour = time / 3600;
int minute = time / 60 % 60;
int second = time % 60;
return hour + "时" + minute + "分" + second + "秒";
}
/**
* 获取n 分钟后的时间戳
*
* @param minute 分钟
* @return
*/
public Long getLateMinute(int minute) {
return Instant.now().plusSeconds(minute * 60).toEpochMilli();
}
}