Java 中日期差距的年份計算(實歲計算)

最近處理一個涉及實歲計算的問題,ChatGPT 給我這個程式碼,我好奇了一下裡面年份計算邏輯,所以讀了一下原始碼

int age = Period.between(birthDate, currentDate).getYears();

java.time.Period#between
計算主要是依靠 LocalDate#until

public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive) {
    return startDateInclusive.until(endDateExclusive);
}

java.time.LocalDate#until(java.time.chrono.ChronoLocalDate)
會先計算月份差距,其中用來相減的月份是透過 LocalDate#getProlepticMonth 取得,getProlepticMonth 邏輯比較簡單,是 年 * 12 + 月 – 1,-1 是因為日期在當月表示還不足一個月

private long getProlepticMonth() {
    return (year * 12L + month - 1);
}

月份差距計算後,會再單獨將日相減,如果月份差距大於 0 且結束日小於開始日,表示有一個月份差距不滿一個月,月份差距需要 -1
(例子:1年1月15日 和 1年2月1日,不能算為有一個月差距)
如果開始日期大於結束日期,月份差距小於 0 且結束日大於開始日,一樣表示有一個月份差距不滿一個月,月份差距負數要加回一
(例子:1年2月15日 和 1年1月20日,不能算為有一個月差距)

最終結果的年份差距就是用月份差距 / 12 取整數

@Override
public Period until(ChronoLocalDate endDateExclusive) {
    LocalDate end = LocalDate.from(endDateExclusive);
    long totalMonths = end.getProlepticMonth() - this.getProlepticMonth();  // safe
    int days = end.day - this.day;
    if (totalMonths > 0 && days < 0) {
        totalMonths--;
        LocalDate calcDate = this.plusMonths(totalMonths);
        days = (int) (end.toEpochDay() - calcDate.toEpochDay());  // safe
    } else if (totalMonths < 0 && days > 0) {
        totalMonths++;
        days -= end.lengthOfMonth();
    }
    long years = totalMonths / 12;  // safe
    int months = (int) (totalMonths % 12);  // safe
    return Period.of(Math.toIntExact(years), months, days);
}

已上年份的部分就結束了,但這裡日差距的算法也滿有趣,月份差距大於 0 且結束日小於開始日,會將開始日期加上月份差距去計算日差距

例:1年1月15日 和 1年2月1日,「1年1月15日」+0月還是「1年1月15日」,計算日差距為 17

例:1年1月15日 和 1年3月1日,「1年1月15日」+1月是「1年2月15日」,計算日差距為 14(28-15+1)

public LocalDate plusMonths(long monthsToAdd) {
    if (monthsToAdd == 0) {
        return this;
    }
    long monthCount = year * 12L + (month - 1);
    long calcMonths = monthCount + monthsToAdd;  // safe overflow
    int newYear = YEAR.checkValidIntValue(Math.floorDiv(calcMonths, 12));
    int newMonth = Math.floorMod(calcMonths, 12) + 1;
    return resolvePreviousValid(newYear, newMonth, day);
}

結論

JDK 的 method 計算邏輯符合我們一般認知的,到了生日那天就是滿 X 歲,如果是網路上有一種用毫秒計算,然後一年以 365 天計算,遇到年份中有跨到閏年,一定會有誤差。

Spring Boot 3.1 Data JPA – show entity to table names

import jakarta.annotation.PostConstruct;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl;
import org.hibernate.persister.entity.AbstractEntityPersister;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Repository
public class ShowTableNameRepositoryImpl {

    @PersistenceContext
    private EntityManager entityManager;

    @PostConstruct
    public void init() {
        Map<String, String> tableNames = new HashMap<>();
        MappingMetamodelImpl metamodel = (MappingMetamodelImpl) entityManager.getEntityManagerFactory().getMetamodel();
        metamodel.entityPersisters().forEach((k, v) -> {
            tableNames.put(v.getEntityName(), ((AbstractEntityPersister) v).getVersionedTableName());
        });
        log.info("tableNames: {}", tableNames);
    }

}

Gradle 產生含 pom.xml 的 Jar 檔

build.gradle 加這些:

[groovy]
plugins {
id ‘maven-publish’
}

publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}

jar {
into("META-INF/maven/$project.group/$project.name") {
from { generatePomFileForMavenJavaPublication }
rename ".*", "pom.xml"
}
}
[/groovy]

components.java 不是指 .java 檔,不用改它

然後跑 gradle publishToMavenLocal(我現在是只先做到產生 Jar 再自己上傳到 packagecloud)

其他細節參考 https://docs.gradle.org/current/userguide/publishing_maven.html

 

Spring Cloud AWS – Assume Role & Proxy configuration for STS & SQS

[java]
@Configuration
public class AppConfig {
private static final String SESSION_NAME = "sample";

@Value("${proxy.host:#{null}}")
private String proxyHost;

@Value("${proxy.port:0}")
private int proxyPort;

@Value("${cloud.aws.region.static}")
private String region;

@Bean
@Primary
public AWSCredentialsProvider awsCredentialsProvider(
@Value("${cloud.aws.credentials.accessKey}") String accessKey,
@Value("${cloud.aws.credentials.secretKey}") String secretKey,
@Value("${cloud.aws.role}") String role) {

AWSSecurityTokenService stsClient = AWSSecurityTokenServiceClientBuilder.standard()
.withRegion(region)
.withClientConfiguration(clientConfiguration())
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKey, secretKey)))
.build();

return new STSAssumeRoleSessionCredentialsProvider
.Builder(role, SESSION_NAME)
.withStsClient(stsClient)
.build();
}

@Bean
public QueueMessageHandlerFactory queueMessageHandlerFactory() {
QueueMessageHandlerFactory factory = new QueueMessageHandlerFactory();
MappingJackson2MessageConverter messageConverter = new MappingJackson2MessageConverter();

messageConverter.setStrictContentTypeMatch(false);
factory.setArgumentResolvers(Collections.singletonList(new PayloadArgumentResolver(messageConverter)));
return factory;
}

@Bean(name = "amazonSQS")
public AmazonSQSAsync amazonSQSAsyncClient(AWSCredentialsProvider awsCredentialsProvider) {
return AmazonSQSAsyncClientBuilder.standard()
.withRegion(region)
.withCredentials(awsCredentialsProvider)
.withClientConfiguration(clientConfiguration())
.build();
}

@Bean
ClientConfiguration clientConfiguration() {
ClientConfiguration clientConfiguration = new ClientConfiguration();

if (!StringUtils.isEmpty(proxyHost)) {
clientConfiguration.setProxyHost(proxyHost);
clientConfiguration.setProxyPort(proxyPort);
}

return clientConfiguration;
}
}
[/java]

Azure Functions 的 Java 啟動表現頗慘

先看圖

簡直慘不忍睹,問了微軟,他們建議是用 App Service 方案開啟 Always On

看 App Service 方案定價,似乎就等於租了一個虛擬機

感覺和 AWS Lambda 落差滿大,AWS Lambda 可是真的按使用計價,而且沒有這樣的 overhead

另外 Azure Functions 的 C# 表現,看起來也不如 AWS Lambda 穩定