LocalDateTime工具类
方式1
这种官方的工具类挺不错,就是要看文档找,推荐这种工具包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.3</version>
</dependency>
参考文档:Hutool参考文档
方式2
时间相关的基本操作,没有处理异常,自己注意
package untils;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;
/**
* @author CBeann
* @create 2020-06-04 14:52
*/
public class LocalDateTimeUtils {
//默认使用系统当前时区
private static final ZoneId ZONE = ZoneId.systemDefault();
/**
* 转换格式1:通用转换格式:yyyy-MM-dd HH:mm:ss
*/
public static final String YYYY_MM_DD_HH_MM_SS_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 转换格式2:年-月-日
*/
public static final String YYYY_MMDD_PATTERN = "yyyy-MM-dd";
//---------------->Local* fromat to String
/**
* @param format 获取的当前LocalDateTime并且格式化
* @return
*/
public static String NowLocalDateTimeByFormat(String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
LocalDateTime now = LocalDateTime.now();
return now.format(dateTimeFormatter);
}
/**
* @param format 把LocalDateTime并且格式化
* @return
*/
public static String LocalDateTimeByFormat(LocalDateTime localDateTime, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return localDateTime.format(dateTimeFormatter);
}
/**
* @param format 把LocalDate并且格式化
* @return
*/
public static String LocalDateByFormat(LocalDate localDate, String format) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
return localDate.format(dateTimeFormatter);
}
//---------------->Date to Local*
/**
* 将Date转换成LocalDateTime
*
* @param d date
* @return
*/
public static LocalDateTime dateToLocalDateTime(Date d) {
Instant instant = d.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZONE);
return localDateTime;
}
/**
* 将Date转换成LocalDate
*
* @param d date
* @return
*/
public static LocalDate dateToLocalDate(Date d) {
Instant instant = d.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZONE);
return localDateTime.toLocalDate();
}
/**
* 将Date转换成LocalTime
*
* @param d date
* @return
*/
public static LocalTime dateToLocalTime(Date d) {
Instant instant = d.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZONE);
return localDateTime.toLocalTime();
}
//---------------->Local* to date
/**
* 将LocalDate转换成Date
*
* @param localDate
* @return date
*/
public static Date localDateToDate(LocalDate localDate) {
Instant instant = localDate.atStartOfDay().atZone(ZONE).toInstant();
return Date.from(instant);
}
/**
* 将LocalDateTime转换成Date
*
* @param localDateTime
* @return date
*/
public static Date localDateTimeToDate(LocalDateTime localDateTime) {
Instant instant = localDateTime.atZone(ZONE).toInstant();
return Date.from(instant);
}
//---------------->string to Local*
/**
* 将相应格式yyyy-MM-dd的字符串转化为LocalDate
*
* @param time string
* @param format string
* @return date
*/
public static LocalDate stringToLocalDate(String time, String format) {
DateTimeFormatter f = DateTimeFormatter.ofPattern(format);
if (YYYY_MMDD_PATTERN.equals(format)) {
return LocalDate.parse(time, f);
}
return null;
}
/**
* 将相应格式yyyy-MM-dd HH:mm:ss 的字符串转化为 LocalDateTime
*
* @param time string
* @param format string
* @return date
*/
public static LocalDateTime stringToLocalDateTime(String time, String format) {
DateTimeFormatter f = DateTimeFormatter.ofPattern(format);
if (YYYY_MM_DD_HH_MM_SS_PATTERN.equals(format)) {
return LocalDateTime.parse(time, f);
}
return null;
}
//---------------->getBefore LocalDateTime
/**
* before 参数小于0
* after 大于0
* =0 表示不大不小
*
* @param localDateTime
* @param year
* @param month
* @param day
* @param hour
* @param minute
* @param sencod
* @return
*/
public static LocalDateTime getBeforeOrAfterLocalDateTime(LocalDateTime localDateTime, int year, int month, int day, int hour, int minute, int sencod) {
if (year != 0) {
localDateTime = localDateTime.plusYears(year);
}
if (month != 0) {
localDateTime = localDateTime.plusMonths(month);
}
if (day != 0) {
localDateTime = localDateTime.plusDays(day);
}
if (hour != 0) {
localDateTime = localDateTime.plusHours(hour);
}
if (minute != 0) {
localDateTime = localDateTime.plusMinutes(minute);
}
if (sencod != 0) {
localDateTime = localDateTime.plusSeconds(sencod);
}
return localDateTime;
}
/**
* 获得LocalDateTime这个月的的第一天
* @return
*/
LocalDateTime getMonthFirstday(LocalDateTime localDateTime) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime date = localDateTime;
LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
return firstday;
}
/**
* 获得LocalDateTime这个月的最后一天
* @return
*/
LocalDateTime getMonthLastday(LocalDateTime localDateTime) {
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime date = localDateTime;
LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());
return lastDay;
}
/**
* 获得Localdatetime的星期
*/
public int getWeekNum(LocalDateTime localDateTime){
System.out.println("week is " + localDateTime.getDayOfWeek());
System.out.println("week is " + localDateTime.getDayOfWeek().getValue());
return localDateTime.getDayOfWeek().getValue();
}
}
参考:Date、LocalTime、LocalDate、LocalDate-时间操作工具类_Hatsune_Miku_的博客
Date工具类
方式1
这种官方的工具类挺不错,就是要看文档找,推荐这种工具包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.3</version>
</dependency>
参考文档:Hutool参考文档
方式2
package untils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class TimeUtils {
/**
* 转换格式1:通用转换格式:yyyy-MM-dd HH:mm:ss
*/
public static final String CURRENT_PATTERN = "yyyy-MM-dd HH:mm:ss";
/**
* 转换格式2:年-月-日
*/
public static final String YYYYMMDD_PATTERN = "yyyy-MM-dd";
/**
* 星期几
*/
private static final String weekNames[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
/**
* 获取 获取格式转换器(带格式)
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午7:03:04
*/
private static SimpleDateFormat getSimpleDateFormat(String pattern) {
return new SimpleDateFormat(pattern);
}
/**
* 获取 获取格式转换器(不带格式)
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午11:16:45
*/
private static SimpleDateFormat getSimpleDateFormat() {
return new SimpleDateFormat();
}
/**
* 根据时间类型转为字符串类型
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午7:03:33
*/
public static String dateFormatWithPattern(Date date, String pattern) {
return getSimpleDateFormat(pattern).format(date);
}
/**
* 根据字符串转化为日期类型
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午11:17:32
*/
public static Date dateFormatWithPattern(String date) throws ParseException {
return getSimpleDateFormat(CURRENT_PATTERN).parse(date);
}
/**
* 根据格式将字符串转换为日期类型
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月22日下午10:41:13
*/
public static Date dateFormatWithPatternWithPattern(String date, String pattern) throws ParseException {
return getSimpleDateFormat(pattern).parse(date);
}
/**
* 获取前n天日期的字符串
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午9:24:59
*/
public static String getBeforeDayString(Date date, int day, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, -day);
date = calendar.getTime();
String dateString = getSimpleDateFormat(pattern).format(date);
return dateString;
}
/**
* 获取后n天日期的字符串
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月27日下午1:01:47
*/
public static String getNextDayString(Date date, int day, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.DATE, day);
date = calendar.getTime();
String dateString = getSimpleDateFormat(pattern).format(date);
return dateString;
}
/**
* 获取当前时间的前N小时
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午11:13:20
*/
public static String getBeforeHourDay(Date date, int hour, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, -hour);
date = calendar.getTime();
String dateString = getSimpleDateFormat(pattern).format(date);
return dateString;
}
/**
* 获取当前时间的后N小时
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月20日下午11:14:48
*/
public static String getNextHourDay(Date date, int hour, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.HOUR_OF_DAY, hour);
date = calendar.getTime();
String dateString = getSimpleDateFormat(pattern).format(date);
return dateString;
}
/**
* 获取当前时间的前n个月
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月21日下午12:37:55
*/
public static String getBeforeMonthDay(Date date, int n, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MONTH, -n);
date = calendar.getTime();
String dateString = getSimpleDateFormat(pattern).format(date);
return dateString;
}
/**
* 获取一个月的最后一天
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月21日下午1:23:06
*/
public static String getLastDayOfMonth(Date date, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
String lastDay = getSimpleDateFormat(pattern).format(calendar.getTime());
return lastDay;
}
/**
* 获取一个月的第一天
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月21日下午1:23:06
*/
public static String getFirstDayOfMonth(Date date, String pattern) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
String lastDay = getSimpleDateFormat(pattern).format(calendar.getTime());
return lastDay;
}
/**
* 获取这一天是周几
* <p>
* Description:
* </p>
*
* @author Tianyu Xiao
* @date 2019年3月22日下午10:18:07
*/
public static String getDayOfWeek(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if (dayOfWeek < 0)
dayOfWeek = 0;
return weekNames[dayOfWeek];
}
/**
* 判断两个日期相差得天数
* day1:前面得天数
* day2:后面得天数
* <p>Description: </p>
*
* @author Tianyu Xiao
* @date 2019年3月29日下午1:26:04
*/
public static long getTwoDayDifference(String day1, String day2) throws ParseException {
Date oDate = dateFormatWithPatternWithPattern(day1, YYYYMMDD_PATTERN);
Date fDate = dateFormatWithPatternWithPattern(day2, YYYYMMDD_PATTERN);
long days = (fDate.getTime() - oDate.getTime()) / (1000 * 3600 * 24);
return days;
}
public static void main(String[] args) throws ParseException {
//String res = stringDateFormat("2019-10-14-14:43:04", "yyyy-MM-dd-HH:mm:ss", "yyyy-MM-dd HH:mm:ss");
Date date = dateFormatWithPattern("2019-12-31 14:43:04");
System.out.println(isLastDayOfMonth(date));
}
/**
* 将旧时间格式转换新时间格式
* <p>Description: </p>
*
* @author Tianyu Xiao
* @date 2019年10月21日下午2:51:09
*/
public static String stringDateFormat(String date, String oldPattern, String newPattern) throws ParseException {
SimpleDateFormat simpleDateFormat = getSimpleDateFormat(oldPattern);
Date dateTime = simpleDateFormat.parse(date);
simpleDateFormat = getSimpleDateFormat(newPattern);
return simpleDateFormat.format(dateTime);
}
/**
* 判断该日期是否是该月的第一天
* <p>Description: </p>
*
* @author Tianyu Xiao
* @date 2019年12月2日下午8:37:19
*/
public static boolean isFirstDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
System.out.println(calendar.get(Calendar.MONTH));
return calendar.get(Calendar.DAY_OF_MONTH) == 1;
}
/**
* 判断该日期是否是该月的最后一天
* <p>Description: </p>
*
* @author Tianyu Xiao
* @date 2019年12月2日下午8:36:58
*/
public static boolean isLastDayOfMonth(Date date) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_MONTH) == calendar
.getActualMaximum(Calendar.DAY_OF_MONTH);
}
}
注意
在使用MyBatis时对象用Date合适,用LocalDateTime就出错,百度查的答案是 mybaits版本问题(我没有试),所以如果打算用LocalDateTime 作为entity的属性,先测试好在用