mirror of
https://gitee.com/dromara/hutool.git
synced 2025-05-04 04:37:59 +08:00
fix code
This commit is contained in:
parent
0964424e74
commit
c0811cc4c4
@ -8,6 +8,7 @@ import cn.hutool.core.date.chinese.LunarInfo;
|
||||
import cn.hutool.core.date.chinese.SolarTerms;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
|
||||
@ -47,20 +48,23 @@ public class ChineseDate {
|
||||
* @param date 公历日期
|
||||
*/
|
||||
public ChineseDate(Date date) {
|
||||
this(LocalDateTimeUtil.ofDate(date.toInstant()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过公历日期构造
|
||||
*
|
||||
* @param localDate 公历日期
|
||||
* @since 5.7.22
|
||||
*/
|
||||
public ChineseDate(LocalDate localDate) {
|
||||
// 公历
|
||||
final DateTime dt = DateUtil.beginOfDay(date);
|
||||
gyear = dt.year();
|
||||
gmonthBase1 = dt.monthBaseOne();
|
||||
gday = dt.dayOfMonth();
|
||||
gyear = localDate.getYear();
|
||||
gmonthBase1 = localDate.getMonthValue();
|
||||
gday = localDate.getDayOfMonth();
|
||||
|
||||
// 求出和1900年1月31日相差的天数
|
||||
final long time = dt.getTime();
|
||||
int offset = (int) ((time / DateUnit.DAY.getMillis()) - LunarInfo.BASE_DAY);
|
||||
if(time > 0 && (time % DateUnit.DAY.getMillis()) > 0){
|
||||
// 在GMT+0800时区或非UTC时区,1970-01-02的时间戳小于一天的毫秒数,导致减法后为0,之后的农历总会少一天
|
||||
// 此处判断是否有余数,如果有则非UTC时间,此时将时间差算为一天。
|
||||
offset++;
|
||||
}
|
||||
int offset = (int) (localDate.toEpochDay() - LunarInfo.BASE_DAY);
|
||||
|
||||
// 计算农历年份
|
||||
// 用offset减去每农历年的天数,计算当天是农历第几天,offset是当年的第几天
|
||||
|
@ -182,6 +182,8 @@ public class LocalDateTimeUtil {
|
||||
|
||||
if (temporalAccessor instanceof LocalDate) {
|
||||
return ((LocalDate) temporalAccessor).atStartOfDay();
|
||||
} else if(temporalAccessor instanceof Instant){
|
||||
return LocalDateTime.ofInstant((Instant) temporalAccessor, ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
return LocalDateTime.of(
|
||||
@ -209,6 +211,8 @@ public class LocalDateTimeUtil {
|
||||
|
||||
if (temporalAccessor instanceof LocalDateTime) {
|
||||
return ((LocalDateTime) temporalAccessor).toLocalDate();
|
||||
} else if(temporalAccessor instanceof Instant){
|
||||
return of(temporalAccessor).toLocalDate();
|
||||
}
|
||||
|
||||
return LocalDate.of(
|
||||
|
@ -26,11 +26,12 @@ import java.time.temporal.UnsupportedTemporalTypeException;
|
||||
public class TemporalAccessorUtil extends TemporalUtil{
|
||||
|
||||
/**
|
||||
* 安全获取时间的某个属性,属性不存在返回0
|
||||
* 安全获取时间的某个属性,属性不存在返回最小值,一般为0<br>
|
||||
* 注意请谨慎使用此方法,某些{@link TemporalAccessor#isSupported(TemporalField)}为{@code false}的方法返回最小值
|
||||
*
|
||||
* @param temporalAccessor 需要获取的时间对象
|
||||
* @param field 需要获取的属性
|
||||
* @return 时间的值,如果无法获取则默认为 0
|
||||
* @return 时间的值,如果无法获取则获取最小值,一般为0
|
||||
*/
|
||||
public static int get(TemporalAccessor temporalAccessor, TemporalField field) {
|
||||
if (temporalAccessor.isSupported(field)) {
|
||||
|
@ -1,5 +1,7 @@
|
||||
package cn.hutool.core.date.chinese;
|
||||
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 阴历(农历)信息
|
||||
*
|
||||
@ -15,7 +17,7 @@ public class LunarInfo {
|
||||
/**
|
||||
* 1900-01-31,农历正月初一
|
||||
*/
|
||||
public static final long BASE_DAY = -25537;
|
||||
public static final long BASE_DAY = LocalDate.of(BASE_YEAR, 1, 31).toEpochDay();
|
||||
|
||||
/**
|
||||
* 此表来自:https://github.com/jjonline/calendar.js/blob/master/calendar.js
|
||||
|
@ -1,13 +1,16 @@
|
||||
package cn.hutool.core.date;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
|
||||
public class LocalDateTimeUtilTest {
|
||||
|
||||
@ -217,4 +220,11 @@ public class LocalDateTimeUtilTest {
|
||||
final int weekOfYear2 = LocalDateTimeUtil.weekOfYear(date1.atStartOfDay());
|
||||
Assert.assertEquals(5, weekOfYear2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ofTest2(){
|
||||
final Instant instant = DateUtil.parse("2022-02-22").toInstant();
|
||||
final LocalDateTime of = LocalDateTimeUtil.of((TemporalAccessor) instant);
|
||||
Console.log(of);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user