gpt4 book ai didi

Java Money - 特定日期的货币兑换率

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:10:27 26 4
gpt4 key购买 nike

我正在尝试获取特定日期欧元和美元之间的汇率。问题是我总是得到日期的交换敢:LocalDate{year=2016, month=1, dayOfMonth=8} 如果我在查询中指定日期并不重要。

这是我的 Maven 依赖项:

        <dependency>
<groupId>javax.money</groupId>
<artifactId>money-api-bp</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.javamoney</groupId>
<artifactId>moneta-bp</artifactId>
<version>1.1</version>
</dependency>

源代码:

public class Test {
public static void main(String[] args) {
ConversionQuery query = ConversionQueryBuilder.of()
.setProviderName("ECB")
.setBaseCurrency(Monetary.getCurrency("EUR"))
.setTermCurrency(Monetary.getCurrency("USD"))
.set(LocalDate.class, LocalDate.of(2017, Month.SEPTEMBER, 10))
.build();
final ExchangeRateProvider provider = getExchangeRateProvider(query);
final ExchangeRate rate1 = provider.getExchangeRate(query);
System.out.println(rate1);

ExchangeRateProvider ecbExchangeRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");
ExchangeRate rate = ecbExchangeRateProvider.getExchangeRate("EUR", "USD");
System.out.println(rate);

}
}

以及 sout 的输出:

sep. 11, 2017 9:58:29 AM org.javamoney.moneta.internal.convert.ECBAbstractRateProvider newDataLoaded
INFO: Loaded ECBCurrentRateProvider exchange rates for days:1
sep. 11, 2017 9:58:30 AM org.javamoney.moneta.internal.convert.ECBAbstractRateProvider newDataLoaded
INFO: Loaded ECBHistoric90RateProvider exchange rates for days:63
ExchangeRate [base=EUR, factor=1.0861, conversionContext=ConversionContext (
{provider=ECB, rateTypes=[DEFERRED], providerDescription=European Central Bank, days=1, org.javamoney.moneta.internal.convert.LocalDate=LocalDate{year=2016, month=1, dayOfMonth=8}, javax.money.convert.RateType=HISTORIC})]
ExchangeRate [base=EUR, factor=1.0861, conversionContext=ConversionContext (
{provider=ECB, rateTypes=[DEFERRED], providerDescription=European Central Bank, days=1, org.javamoney.moneta.internal.convert.LocalDate=LocalDate{year=2016, month=1, dayOfMonth=8}, javax.money.convert.RateType=HISTORIC})]
ExchangeRate [base=USD, factor=0.9207255317189946, conversionContext=ConversionContext (
{provider=ECB, rateTypes=[DEFERRED], providerDescription=European Central Bank, days=1, org.javamoney.moneta.internal.convert.LocalDate=LocalDate{year=2016, month=1, dayOfMonth=8}, javax.money.convert.RateType=HISTORIC})]
sep. 11, 2017 9:58:35 AM org.javamoney.moneta.internal.convert.ECBAbstractRateProvider newDataLoaded
INFO: Loaded ECBHistoricRateProvider exchange rates for days:4358

谢谢!

最佳答案

此 API 目前尚不清楚。如果你得到:

ConversionQueryBuilder.of().setTermCurrency("EUR").
set(LocalDate.class, LocalDate.of(2018, 1, 1)).build()
MonetaryConversions.getConversion(conversionQuery)

它将使用以下方法:

default CurrencyConversion getConversion(ConversionQuery conversionQuery) {
return getExchangeRateProvider(conversionQuery).getCurrencyConversion(
Objects.requireNonNull(conversionQuery.getCurrency(), "Terminating Currency is required.")
);
}

如您所见,conversionQuery 用于获取提供程序,而不是 转换对象本身。它总是返回以供仅设置货币而不设置日期的查询。

要使其正常工作,请使用:

MonetaryConversions.getExchangeRateProvider().getCurrencyConversion(conversionQuery)

我相信它应该对所有调用始终如一地工作。

关于Java Money - 特定日期的货币兑换率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46150919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com