全网最全的日期格式化工具类
日期工具类
import lombok.extern.slf4j.Slf4j;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
/**
* 时间类型
*/
@Slf4j
public class DateUtils {
/**
* @param startDate 开始日期
* @param endDate 结束日期
* @return 返回相差的天数
*/
public static int differentDaysByString(String startDate, String endDate) {
int days = 0;
try {
days = (int) ((Objects.requireNonNull(parseDate(endDate)).getTime() - Objects.requireNonNull(parseDate(startDate)).getTime()) / (1000 * 3600 * 24));
} catch (ParseException e) {
log.info("数据异常" + e.getMessage());
}
return days;
}
/**
* 日期格式转换
*
* @param date 日期
* @return 日期格式
*/
public static Date parseDate(String date) throws ParseException {
if (date.isEmpty()) {
return null;
}
return new SimpleDateFormat("yyyy-MM-dd").parse(date);
}
/**
* 获取10为的时间戳 0点0分0秒的
* @param time 时间
* @return 时间戳
*/
public static long timestampPlaceZeroZeroPointsZeroSecondsGetString(String time) {
LocalDate startDate = LocalDate.parse(time);
LocalDateTime startDateTime = startDate.atTime(LocalTime.MIN);
return startDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
}
/**
* 获取10为时间戳 24点59分59秒
* @param time 时间
* @return 时间戳
*/
public static long timestampTwentyThreeFiftyNineMinutesAndFiftyNineSecondsGetString(String time) {
LocalDate date = LocalDate.parse(time);
LocalDateTime dateTime = date.atTime(LocalTime.MAX);
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli() / 1000;
}
/**
* 切换为中文日期
* @param time 时间
* @return 中文日期
*/
public static String getChinaDateByMm(long time) {
String retDate = "";
SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
retDate = formatter.format(time);
return retDate;
}
/**
* 字符串转时间格式 第二个参数定义格式化格式
*
* @param strDate
* @param formater
* @return
*/
public static Date str2Date(String strDate, String formater) {
if (strDate == null) {
return null;
}
if (formater == null) {
formater = "yyyy-MM-dd";
}
SimpleDateFormat df = new SimpleDateFormat(formater);
Date date = new Date();
try {
date = df.parse(strDate);
} catch (ParseException pe) {
pe.getStackTrace();
}
return date;
}
/**
* yyyy-MM-dd格式的字符串转日期类型
*
* @param strDate
* @return
*/
public static Date ymdStr2Date(String strDate) {
String formater = "yyyy-MM-dd";
return str2Date(strDate, formater);
}
/**
* yyyy-MM-dd HH:mm:ss 格式的字符串转日期类型
*
* @param strDate
* @return
*/
public static Date ymdHmsStr2Date(String strDate) {
String formater = "yyyy-MM-dd HH:mm:ss";
return str2Date(strDate, formater);
}
/**
* 默认格式的字符串转日期类型
*
* @param date
* @return
*/
public static String ymd(Date date) {
return formaterDate(date, null);
}
public static String formaterDate(Date date, String formater) {
if (date == null) {
return "";
}
if (formater == null) {
formater = "yyyy-MM-dd";
}
SimpleDateFormat sdf = new SimpleDateFormat(formater);
return sdf.format(date);
}
/**
* 将日期格式的数据转成yyyy-MM-dd HH:mm格式字符串
*
* @param date
* @return
*/
public static String ymdHm(Date date) {
return formaterDate(date, "yyyy-MM-dd HH:mm");
}
/**
* 将日期格式的数据转成yyyyMMddHHmmss格式字符串
*
* @param date
* @return
*/
public static String yyyyMMddHHmmss(Date date) {
return formaterDate(date, "yyyyMMddHHmmss");
}
/**
* 将日期格式的数据转成HH:mm:ss格式字符串
*
* @param date
* @return
*/
public static String hms(Date date) {
String s = formaterDate(date, "yyyy-MM-dd HH:mm:ss");
s = s.substring(11);
return s.replaceAll(":", "");
}
/**
* 获取字符串格式的当前时间,不包含日期
*
* @return
*/
public static String hms() {
return hms(new Date());
}
/**
* 将日期格式的数据转成yyyy-MM-dd格式字符串
*
* @param date
* @return
*/
public static String ym(Date date) {
return formaterDate(date, "yyyy-MM-dd");
}
/**
* @param @param date
* @param @return 设定文件
* @return String 返回类型
* @throws
* @Title: md
* @Description: TODO(将时间格式转换成 MM - dd 格式的字符串)
*/
public static String md(Date date) {
return formaterDate(date, "MM-dd");
}
/**
* 将日期格式的数据转成yyyy-MM-dd HH:mm:ss格式字符串
*
* @param date
* @return
*/
public static String ymdHms(Date date) {
return formaterDate(date, "yyyy-MM-dd HH:mm:ss");
}
public static Date ymdhmsStr2Date(String strDate) {
String formater = "yyyyMMddHHmmss";
return str2Date(strDate, formater);
}
/**
* yyyyMMddHHmmss to yyyy-MM-dd HH:mm:ss
*
* @param str
* @return
*/
public static String ymdhms2yMdHms(String str) {
return ymdHms(ymdhmsStr2Date(str));
}
/**
* 把时分秒置0
*
* @param strDate
* @return
*/
public static Date toh00m00s00(Date strDate) {
String yyymmdd = DateUtils.ymd(strDate);
String date = yyymmdd + " 00:00:00";
return DateUtils.ymdHmsStr2Date(date);
}
/**
* 把时分秒置23:59:59
*
* @param strDate
* @return
*/
public static Date toh23m59s59(Date strDate) {
String yyymmdd = DateUtils.ymd(strDate);
String date = yyymmdd + " 23:59:59";
return DateUtils.ymdHmsStr2Date(date);
}
// 计算当月最后一天,返回字符串
public static String getMonthLastDay() {
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 1);// 设为当前月的1号
c.add(Calendar.MONTH, 1);// 加一个月,变为下月的1号
c.add(Calendar.DATE, -1);// 减去一天,变为当月最后一天
return ymd(c.getTime());
}
// 计算当月第一天,返回字符串
public static String getMonthFirstDay() {
Calendar c = Calendar.getInstance();
c.set(Calendar.DATE, 1);// 设为当前月的1号
return ymd(c.getTime());
}
/**
* 获得距当前时间前后N天的日期
*
* @param day
* @return
*/
public static String getDayFromToday(int day) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + day);
return ym(c.getTime());
}
/**
* 获得距当前时间前后N小时的日期
*
* @param day
* @return
*/
public static Date getHourFromToday(int day) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + day);
return c.getTime();
}
public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
List<Date> lDate = new ArrayList();
lDate.add(beginDate);
Calendar cal = Calendar.getInstance();
cal.setTime(beginDate);
boolean bContinue = true;
while (bContinue) {
cal.add(5, 1);
if (!endDate.after(cal.getTime())) {
break;
}
lDate.add(cal.getTime());
}
lDate.add(endDate);
return lDate;
}
/**
* 通过时间秒毫秒数判断两个时间的间隔
*
* @param date1
* @param date2
* @return
*/
public static int differentDaysByMillisecond(Date date1, Date date2) {
int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
return days;
}
/*
* 获取当前时间的上一个月的开始时间
*/
public static Date getMonthBeginlast(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
//设置为1号,当前日期既为本月第一天
c.set(Calendar.DAY_OF_MONTH, 1);
//将小时至0
c.set(Calendar.HOUR_OF_DAY, 0);
//将分钟至0
c.set(Calendar.MINUTE, 0);
//将秒至0
c.set(Calendar.SECOND, 0);
//将毫秒至0
c.set(Calendar.MILLISECOND, 0);
// 获取本月第一天的时间戳
return c.getTime();
}
/*
* 获取当前时间的上一个月的开始时间
*/
public static Date getMonthEndlast(Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);
//设置为当月最后一天
c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
//将小时至23
c.set(Calendar.HOUR_OF_DAY, 23);
//将分钟至59
c.set(Calendar.MINUTE, 59);
//将秒至59
c.set(Calendar.SECOND, 59);
//将毫秒至999
c.set(Calendar.MILLISECOND, 999);
// 获取本月最后一天的时间戳
return c.getTime();
}
public static String getWeek(Date date) {
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
String week = sdf.format(date);
return week;
}
/**
* 获得距当前时间前后N天的日期
*
* @param day
* @return
*/
public static Date getDayFromTodayDate(int day) {
Calendar c = Calendar.getInstance();
c.setTime(new Date());
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) + day);
return c.getTime();
}
/**
* 获取指定日期是星期几
* <p>
* 参数为null时表示获取当前日期是星期几
*
* @param date
* @return
*/
public static String getWeekOfDate(Date date) {
String[] weekOfDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
Calendar calendar = Calendar.getInstance();
if (date != null) {
calendar.setTime(date);
}
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (w < 0) {
w = 0;
}
return weekOfDays[w];
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.
- 317.
- 318.
- 319.
- 320.
- 321.
- 322.
- 323.
- 324.
- 325.
- 326.
- 327.
- 328.
- 329.
- 330.
- 331.
- 332.
- 333.
- 334.
- 335.
- 336.
- 337.
- 338.
- 339.
- 340.
- 341.
- 342.
- 343.
- 344.
- 345.
- 346.
- 347.
- 348.
- 349.
- 350.
- 351.
- 352.
- 353.
- 354.
- 355.
- 356.
- 357.
- 358.
- 359.
- 360.
- 361.
- 362.
- 363.
- 364.
- 365.
- 366.
- 367.
- 368.
- 369.
- 370.
- 371.
- 372.
- 373.
- 374.
- 375.
- 376.
- 377.
- 378.
- 379.
- 380.
- 381.
- 382.
- 383.
- 384.
- 385.
- 386.
- 387.
- 388.
- 389.
- 390.
- 391.
- 392.
- 393.
- 394.
- 395.
- 396.
- 397.
- 398.
- 399.
- 400.
- 401.
- 402.
- 403.
- 404.
- 405.
- 406.
- 407.
- 408.
- 409.
- 410.