JAva 8のLocalDateメソッドの使い方研究
前言:字はすべて血のようで、数日苦労するのは普通ではありませんて、数日使ってLocalDateの方法を研究して、その中に自分の思考が少なくありません
説明リンク:java.util.timeパッケージの説明
説明リンク:java.util.timeパッケージの説明
/**
*
*/
package com.ljh.myo2o.dao;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.time.temporal.TemporalQueries;
import java.time.temporal.ValueRange;
import java.util.Arrays;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @description:
* @author :
* @CreateTime:2018-11-23 02:23:41
*/
public class DateAndTimeDemo {
private Logger logger = LoggerFactory.getLogger(DateAndTimeDemo.class);
/**
* discription:
*
* @author:
* @createTime:2018-11-23 02:23:41
* @param args
*/
public static void main(String[] args) {
//
// LocalDate:
// toString() , yyyy-MM-dd
LocalDate today = LocalDate.now();
// : 2018-11-23
System.out.println(" : " + today);
// LocalDate
// DateTimeException
// , Month
LocalDate specifiedDate = LocalDate.of(2018, 1, 1);
LocalDate sDate = LocalDate.of(2018, Month.JANUARY, 1);
// equals ,LocalDate equals
System.out.println(specifiedDate.equals(sDate));// true
// 340 ,
LocalDate date = LocalDate.ofYearDay(2018, 340);
System.out.println(date);// 2018-12-06
// , ,
int dayOfYear = date.getDayOfYear();
int dayOfMonth = date.getDayOfMonth();
System.out.println(dayOfYear + "__" + dayOfMonth);
// java.util.time
// , :plus/minus/firstMonthOfQuarter
DayOfWeek dayOfWeek = date.getDayOfWeek();
Month month = date.getMonth();
//
int monthValue = month.getValue();
int monthValue2 = date.getMonthValue();
//
date.lengthOfMonth();
month.length(date.isLeapYear());// leap year
// EpochDay: , 0 1970-01-01 ,
long epochDayNums = date.toEpochDay();
LocalDate.ofEpochDay(epochDayNums);
// Chronology:
// LocalDate dateNow = date.getChronology().dateNow();
// TemporalAccessor
// LocalDate from = LocalDate.from(TemporalAccessor temporal);
// System.out.println(LocalDateTime.now().toString().replace("T", " "));
// ,
// TemporalQuery q=LocalDate::from;
//
LocalDate parse1 = LocalDate.parse("2018-11-11");
LocalDate parse2 = LocalDate.parse("2018-11-11", DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(parse1);
System.out.println(parse2);
// ,
boolean supportedChronoField = date.isSupported(ChronoField.DAY_OF_MONTH);
boolean supportedChronoUnit = date.isSupported(ChronoUnit.YEARS);
System.out.println(supportedChronoField);
System.out.println(supportedChronoUnit);
//
ValueRange range = date.range(ChronoField.DAY_OF_MONTH);
System.out.println(range);// 1 - 31
// int i = date.get(ChronoField.DAY_OF_MONTH);//6
//
//
/*
* for (ChronoField c : ChronoField.values()) { if (date.isSupported(c))
* { if ("EpochDayProlepticMonth".contains(c.toString())) {
* System.out.println(date.getLong(c)+"--"+c); }else {
* System.out.println(date.get(c)+"--"+c); }
*
* } }
*/
Arrays.asList(ChronoField.values()).forEach((c) -> {
if (date.isSupported(c)) {
if ("EpochDayProlepticMonth".contains(c.toString())) {
System.out.println(date.getLong(c) + "--" + c);
} else {
System.out.println(date.get(c) + "--" + c);
}
}
});
// with
/*
* LocalDate with(TemporalAdjuster adjuster) LocalDate
* with(TemporalField field, long newValue) LocalDate withYear(int year)
* LocalDate withMonth(int month) LocalDate withDayOfYear(int dayOfYear)
* LocalDate final , 7 result =
* localDate.with(JULY).with(lastDayOfMonth());
*/
// LocalDate withDayOfMonth =
// date.with(Month.JULY).withDayOfMonth(Month.JULY.length(date.with(Month.JULY).isLeapYear()));
LocalDate lastDateOfJuly = date.with(Month.JULY);
lastDateOfJuly = lastDateOfJuly.withDayOfMonth(lastDateOfJuly.lengthOfMonth());
System.out.println(lastDateOfJuly);
/*
* // these two lines are equivalent
* Period period = start.until(end);
* Period.between(start, end);
*/
/*
* plus minus : Period Duration TemporalAmount
* ChronoUnit TemporalUnit LocalDate plus(TemporalAmount
* amountToAdd) LocalDate plus(long amountToAdd, TemporalUnit unit)
* LocalDate plusYears(long yearsToAdd) LocalDate plusMonths(long
* monthsToAdd) LocalDate plusWeeks(long weeksToAdd) LocalDate
* plusDays(long daysToAdd)
*/
Period period = Period.between(LocalDate.of(2018, 12, 7), LocalDate.of(2018, 12, 31));
LocalDate plusDate = date.plus(period);
System.out.println(plusDate);
System.out.printf("LocalDate precision is %s%n",
date.query(TemporalQueries.precision()));
/*
*
StringBuilder sb = new StringBuilder();// Appendable
// Appendable,
Formatter formatter = new Formatter(sb, Locale.CHINA);
// : %[argument_index$][flags][width][.precision]conversion
formatter.format("%4$2s%3$2s%2$2s%1$2s", "a", "b", "c", "d");
System.out.println(formatter.out());
formatter.format(Locale.PRC, "e = %+.4f", Math.E);
System.out.println(formatter.out());
formatter.close();
String format = String.format("hello,%s","world!");
System.out.println(format);
System.out.format("hello,%s","world!");*/
//
LocalDate newDate =(LocalDate)date.adjustInto(LocalDate.of(2018, 12, 31));
System.out.println("newDate:"+newDate);
//
LocalDate with = LocalDate.now().with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(with);// 25 , 2018-11-26
// 2019-01-30
long until = LocalDate.now().until(LocalDate.of(2019, 1, 30), ChronoUnit.DAYS);
System.out.println(until);//66
//until
Period period2 = LocalDate.now().until(LocalDate.of(2019, 1, 30));
System.out.println(period2.getDays());// 5, bug
/*
* , DateTimeFormatter.format()
* public String format(DateTimeFormatter formatter) {
Objects.requireNonNull(formatter, "formatter");
return formatter.format(this);
}
*/
// ,
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd",Locale.CHINA);
String format = LocalDate.now().format(formatter);
System.out.println(format);
/*
* atTime : ,
* LocalDateTime atTime(LocalTime time)
* LocalDateTime atTime(int hour, int minute)
* LocalDateTime atTime(int hour, int minute, int second)
* LocalDateTime atTime(int hour, int minute, int second, int nanoOfSecond)
* OffsetDateTime atTime(OffsetTime time)
*/
LocalDateTime atTime = LocalDate.now().atTime(10, 25);
System.out.println(atTime);//2018-11-25T10:25
// T, ,
String format2 = atTime.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
System.out.println(format2);//2018-11-25T10:25:00
// , T,
// ,
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String format3 = atTime.format(dtf);
System.out.println(format3);//2018-11-25 10:25:00,
//
LocalDateTime atStartOfDay = LocalDate.now().atStartOfDay();
String format4 = atStartOfDay.format(dtf);
System.out.println(format4);//2018-11-25 00:00:00
/*
* int compareTo(ChronoLocalDate other) boolean equals(Object obj)
* ,
* equals compareTo
*/
if (LocalDate.now().equals(LocalDate.of(2018, 11, 25))) {
System.out.println(true);
}
if (LocalDate.now().compareTo(LocalDate.of(2018, 11, 25))==0) {
System.out.println(true);
}
/*
* is :
* boolean isAfter(ChronoLocalDate other)
* boolean isBefore(ChronoLocalDate other)
* boolean isEqual(ChronoLocalDate other)
* compareTo equals
*/
// : , ,
}
}