常用日期总结:
Date date = new Date();
LocalDate localDate = date.toInstant()
.atZone(ZoneId.systemDefault()).toLocalDate();
LocalDateTime localDateTime = date.toInstant()
.atZone(ZoneId.systemDefault()).toLocalDateTime();
System.out.println("localDate = " + localDate);
System.out.println("localDateTime = " + localDateTime);
Date from = Date
.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
System.out.println("from = " + from);
String -> LocalDateTime:
String timeStr = "2022/6/20"
LocalDate parse = LocalDate.parse(timeStr, DateTimeFormatter.ofPattern("yyyy/M/dd"));
LocalDateTime now = LocalDateTime.now();
String s = now.toString();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String now = LocalTime.now().atDate(LocalDate.now()).format(formatter);
LocalDate localDate = LocalDate.now();
String localDate = localDate.toString();
LocalDate localDate = LocalDate.of(year,month,day);
int year = localDate.getYear();
int year = localDate.get(ChronoField.YEAR);
Month month = localDate.getMonth();
int month1 = localDate.get(ChronoField.MONTH_OF_YEAR);
int day = localDate.getDayOfMonth();
int day1 = localDate.get(ChronoField.DAY_OF_MONTH);
DayOfWeek dayOfWeek = localDate.getDayOfWeek();
int dayOfWeek1 = localDate.get(ChronoField.DAY_OF_WEEK);
localDate = localDate.plusYears(1);
localDate = localDate.plus(1, ChronoUnit.YEARS);
localDate = localDate.minusMonths(1);
localDate = localDate.minus(1, ChronoUnit.MONTHS);
localDate = localDate.minusDays(1);
localDate = localDate.minus(1, ChronoUnit.DAYS);
localDate = localDate.withYear(2020);
localDate = localDate.with(ChronoField.YEAR, 2020);
package cn.nice.h2.base
import cn.hutool.core.date.DateUnit
import cn.hutool.core.date.DateUtil
import cn.hutool.core.lang.Assert
import org.junit.Test
import java.text.ParseException
import java.text.SimpleDateFormat
import java.time.*
import java.time.format.DateTimeFormatter
import java.util.Date
/**
* @Description localDate : 主要用于处理日期的 java8 特性之一
* @Author nice
* @Date 2021/9/22 4:33 下午
*/
public class LocalDateDemo {
@Test
public void test1(){
System.out.println(LocalDateTime.now().toEpochSecond(ZoneOffset.ofHours(8)))
System.out.println("!!!!!!!")
LocalDate localDate = LocalDate.now()
// System.out.println(localDate.toString())
LocalDateTime localDateTime = LocalDateTime.now()
// System.out.println(localDateTime)
// System.out.println(localDateTime.toLocalTime())
System.out.println(localDateTime.toString())
LocalDate localDate1 = LocalDate.of(2020,12,3)
// System.out.println(localDate1)
System.out.println(localDate1.getYear())
System.out.println(localDate1.getMonth().getValue())
DayOfWeek dayOfWeek = localDate.getDayOfWeek()
// System.out.println(dayOfWeek.getValue())
// System.out.println(localDate.getMonth().getValue())
Boolean isLeapYear = localDate.isLeapYear()
/**
* with开头的方法,我的理解是将参数替换localDate中的对应
* 属性,重新计算得到新的日期。
* 将参数中的"日"替换localDate中的"日"
* 将参数中的天数替换localDate中的天数
* 将参数中的"月"替换localDate中的"月"
* 将参数中的"年"替换localDate中的"年"
* */
localDate = LocalDate.of(2019,1,7)
System.out.println(localDate.withDayOfMonth(2))
System.out.println(localDate.withDayOfYear(40))
System.out.println(localDate.withMonth(2))
System.out.println(localDate.withYear(2020))
Period weeks = Period.ofWeeks(2)
Period years = Period.ofYears(2)
System.out.println(localDate.minus(weeks))
System.out.println(localDate.plus(weeks))
/**
* localDate.minusDays(long days)
* localDate.minusWeeks(long weeks)
* localDate.minusMonths(long months)
* localDate.minusYears(long years)
* localDate.plusDays(long days)
* localDate.plusWeeks(long weeks)
* localDate.plusMonths(long months)
* localDate.plusYears(long years)
* */
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
String now = LocalTime.now().atDate(LocalDate.now()).format(formatter)
System.out.println(LocalTime.now().atDate(LocalDate.now()).format(formatter))
System.out.println(LocalDate.now().atTime(LocalTime.now()).format(formatter))
String s = localDateTime.format(formatter)
System.out.println(s)
}
/** 使用转换日期判定日期是否符合正确日期 */
public static boolean isValidDate(String str) {
boolean convertSuccess=true
// 指定日期格式为四位年/两位月份/两位日期,注意yyyy/MM/dd区分大小写;
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd")
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false)
format.parse(str)
} catch (ParseException e) {
// e.printStackTrace()
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
convertSuccess=false
}
return convertSuccess
}
/**
*
* LocalDateTime 为java8的新特性之一
* LocalDateTime.now() 获得当前时间
* java.time.Duration duration = java.time.Duration.between(LocalDateTime startTime, LocalDateTime endTime )
* 例如: duration.toMinutes() //两个时间差的分钟数
* toNanos()//纳秒
* toMillis()//毫秒
* toMinutes()//分钟
* toHours()//小时
* toDays()//天数
* */
public static Long durationDay(String startDateStr,String endDateStr) {
if(startDateStr.length() >= 10){
startDateStr = startDateStr.substring(0,10)
}
if(endDateStr.length() >= 10){
endDateStr = endDateStr.substring(0,10)
}
startDateStr += " 00:00:00"
endDateStr += " 00:00:00"
// LocalDateTime必须是yyyy-MM-dd HH:mm:ss
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
LocalDateTime startDate = LocalDateTime.parse(startDateStr,dateTimeFormatter)
LocalDateTime endDate = LocalDateTime.parse(endDateStr,dateTimeFormatter)
return Duration.between(startDate, endDate).toDays()
}
public static Long DateUtilDurationDay(String startDateStr,String endDateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd")
try {
// 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期,比如2007/02/29会被接受,并转换成2007/03/01
format.setLenient(false)
return DateUtil.between(format.parse(startDateStr), format.parse(endDateStr), DateUnit.DAY)
} catch (ParseException e) {
// e.printStackTrace()
// 如果throw java.text.ParseException或者NullPointerException,就说明格式不对
Assert.isFalse(false,"传递日期有问题")
}
return 0l
}
public static void main(String[] args) {
System.out.println(DateUtilDurationDay("2021-01-01","2021-01-03"))
}
}