注意: LocalDate、LocalTime、LocalDateTime、Instant,这几个类的所有属性,都有 final 修饰符,所以,所有修改这些类属性的方法,都会返回一个新的对象副本。如果还是使用原来的对象,修改无效。
// LocalDate
// 获取当前日期
LocalDate localDate = LocalDate.now(); // 2020-07-07
// 获取年
int year1 = localDate.getYear(); // 2020
int year2 = localDate.get(ChronoField.YEAR); // 2020
// 获取月
Month month1 = localDate.getMonth(); // JULY
int month2 = localDate.getMonthValue(); // 7
int month3 = localDate.get(ChronoField.MONTH_OF_YEAR); // 7
// 获取日
int dayOfMonth1 = localDate.getDayOfMonth(); // 7
int dayOfMonth2 = localDate.get(ChronoField.DAY_OF_MONTH); // 7
// LocalTime
// 获取当前时间
LocalTime localTime = LocalTime.now(); // 17:11:55.956
// 获取时
int hour1 = localTime.getHour(); // 17
int hour2 = localTime.get(ChronoField.HOUR_OF_DAY); // 17
// 获取分
int minute1 = localTime.getMinute(); // 11
int minute2 = localTime.get(ChronoField.MINUTE_OF_HOUR); // 11
// 获取秒
int second1 = localTime.getSecond(); // 55
int second2 = localTime.get(ChronoField.SECOND_OF_MINUTE); // 55
// LocalDateTime
// 获取当前日期和时间
LocalDateTime localDateTime = LocalDateTime.now(); // 2020-07-07T17:22:13.067
// 获取LocalDate对象
LocalDate localDate4 = localDateTime.toLocalDate(); // 2020-07-07
// 获取LocalTime对象
LocalTime localTime4 = localDateTime.toLocalTime(); // 17:22:13.067
// 获取年
int year4 = localDateTime.getYear(); // 2020
// 获取月
int month4 = localDateTime.getMonthValue(); // 7
// 获取日
int dayOfMonth4 = localDate.getDayOfMonth(); // 7
// 获取时
int hour4 = localDateTime.getHour(); // 17
// 获取分
int minute4 = localDateTime.getMinute(); // 22
// 获取秒
int second4 = localTime.getSecond(); // 13
// Instant
// 获取Instant对象
Instant instant = Instant.now();
// 获取从1970-01-01T00:00:00Z以来的秒数
long curSecond = instant.getEpochSecond(); // 1594120503
// 获取从1970-01-01T00:00:00Z以来的毫秒数
long curMilli = instant.toEpochMilli(); // 1594120503969
// LocalDateTime
// 创建日期时间 2020-07-06 20:23:57
LocalDateTime localDateTime = LocalDateTime.of(2020, 7, 6, 20, 23, 57);
// 修改年
localDateTime = localDateTime.withYear(2019); // 2019-07-06 20:23:57
localDateTime = localDateTime.with(ChronoField.YEAR, 2021); // 2021-07-06 20:23:57
// 修改月
localDateTime = localDateTime.withMonth(9); // 2021-09-06 20:23:57
// 修改日
localDateTime = localDateTime.withDayOfMonth(8); // 2021-09-08 20:23:57
// 修改时
localDateTime = localDateTime.withHour(5); // 2021-09-08 05:23:57
// 修改分
localDateTime = localDateTime.withMinute(6); // 2021-09-08 05:06:57
// 修改秒
localDateTime = localDateTime.withSecond(8); // 2021-09-08 05:06:08
// 预置格式
// 2020-05-30
LocalDate localDate1 = LocalDate.parse("20200530", DateTimeFormatter.BASIC_ISO_DATE);
// 2020-05-30
LocalDate localDate2 = LocalDate.parse("2020-05-30", DateTimeFormatter.ISO_LOCAL_DATE);
// 2020-05-30T08:46:32
LocalDateTime localDateTime1 = LocalDateTime.parse("2020-05-30T08:46:32", DateTimeFormatter.ISO_LOCAL_DATE_TIME);
// 自定义格式
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
// 2020-05-30
LocalDate localDate3 = LocalDate.parse("2020/05/30", dtf1);
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
// 2020-05-30T08:55:13
LocalDateTime localDateTime2 = LocalDateTime.parse("2020-05-30 08:55:13", dtf2);
LocalDateTime localDateTime = LocalDateTime.now(); // 2020-07-08T08:59:50.152
// 预置格式
String s1 = localDateTime.format(DateTimeFormatter.BASIC_ISO_DATE); // 20200708
String s2 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE); // 2020-07-08
String s3 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_TIME); // 08:59:50.152
String s4 = localDateTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME); // 2020-07-08T08:59:50.152
// 自定义格式
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String s5 = localDateTime.format(dtf); // 2020-07-08 08:59:50
LocalDateTime localDateTime = LocalDateTime.now(); // 2020-07-08T09:11:51.902
LocalDateTime localDateTime1 = localDateTime.plusDays(1); // 2020-07-09T09:11:51.902
LocalDateTime localDateTime2 = localDateTime.plus(2, ChronoUnit.DAYS); // 2020-07-10T09:11:51.902
LocalDateTime localDateTime3 = localDateTime.minusDays(1); // 2020-07-07T09:11:51.902
LocalDateTime localDateTime4 = localDateTime.plusYears(1); // 2021-07-08T09:11:51.902
LocalDateTime localDateTime5 = localDateTime.plusMonths(1); // 2020-08-08T09:11:51.902
LocalDateTime localDateTime6 = localDateTime.plusHours(1); // 2020-07-08T10:11:51.902
LocalDateTime localDateTime7 = localDateTime.plusMinutes(1); // 2020-07-08T09:12:51.902
LocalDateTime localDateTime8 = localDateTime.plusSeconds(1); // 2020-07-08T09:11:52.902
package com.dosrain.shunc.trial.date;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
public class DateUtil {
/**
* Date 转换为 LocalDate
*
* @param date
* @return
*/
public static LocalDate date2LocalDate(Date date) {
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime.toLocalDate();
}
/**
* LocalDate 转换为 Date
*
* @param localDate
* @return
*/
public static Date localDate2Date(LocalDate localDate) {
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* Date 转换为 LocalDateTime
*
* @param date
* @return
*/
public static LocalDateTime date2LocalDateTime(Date date) {
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime;
}
/**
* LocalDateTime 转换为 Date
*
* @param localDateTime
* @return
*/
public static Date localDateTime2Date(LocalDateTime localDateTime) {
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* 格式化日期类型为字符串(LocalDate粒度)
*
* @param date
* @param pattern
* @return
*/
public static String formatDate(Date date, String pattern) {
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
LocalDate localDate = localDateTime.toLocalDate();
return localDate.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* 解析字符串为日期类型(LocalDate粒度)
*
* @param dateString
* @param pattern
* @return
*/
public static Date parseDate(String dateString, String pattern) {
LocalDate localDate = LocalDate.parse(dateString, DateTimeFormatter.ofPattern(pattern));
Instant instant = localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* 格式化日期类型为字符串(LocalDateTime粒度)
*
* @param date
* @param pattern
* @return
*/
public static String formatDateTime(Date date, String pattern) {
Instant instant = date.toInstant();
LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* 解析字符串为日期类型(LocalDateTime粒度)
*
* @param dateString
* @param pattern
* @return
*/
public static Date parseDateTime(String dateString, String pattern) {
LocalDateTime localDateTime = LocalDateTime.parse(dateString, DateTimeFormatter.ofPattern(pattern));
Instant instant = localDateTime.atZone(ZoneId.systemDefault()).toInstant();
return Date.from(instant);
}
/**
* 取得指定日期所在周的周一LocalDate类型
*
* @param localDate
* @return
*/
public static LocalDate getThisWeekMonday(LocalDate localDate) {
localDate = localDate.plusDays(1 - localDate.getDayOfWeek().getValue());
return localDate;
}
/**
* 取得上周一的日期字符串
*
* @param pattern
* @return
*/
public static String getLastWeekMonday(String pattern) {
LocalDate localDate = LocalDate.now();
localDate = localDate.minusDays(7);
localDate = getThisWeekMonday(localDate);
return localDate.format(DateTimeFormatter.ofPattern(pattern));
}
/**
* 取得上月1日的日期字符
*
* @param pattern
* @return
*/
public static String getLastMonthFirstDay(String pattern) {
LocalDate localDate = LocalDate.now();
localDate = localDate.minusMonths(1);
localDate = localDate.minusDays(localDate.getDayOfMonth() -1);
return localDate.format(DateTimeFormatter.ofPattern(pattern));
}
public static void main(String[] args) {
System.out.println(DateUtil.formatDate(new Date(), "yyyy-MM-dd"));
System.out.println(DateUtil.parseDate("2021-08-12", "yyyy-MM-dd"));
System.out.println(DateUtil.formatDateTime(new Date(), "yyyy-MM-dd HH:mm:ss"));
System.out.println(DateUtil.parseDateTime("2021-08-12 09:52:03", "yyyy-MM-dd HH:mm:ss"));
System.out.println(DateUtil.getThisWeekMonday(LocalDate.now()));
System.out.println(DateUtil.getLastWeekMonday("yyyy-MM-dd"));
System.out.println(DateUtil.getLastMonthFirstDay("yyyy-MM-dd"));
}
}