0

我正在尝试使用 java money-api 和 moneta 存储库进行简单的货币转换。

<dependency>
    <groupId>javax.money</groupId>
    <artifactId>money-api</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>org.javamoney</groupId>
    <artifactId>moneta</artifactId>
    <version>1.4.2</version>
</dependency>
IMFHistoricRateProvider imf = new IMFHistoricRateProvider();

LocalDate now = LocalDate.now();
ExchangeRate exr = null;
long start = System.currentTimeMillis();
LocalDate date = LocalDate.parse("2021-05-25", DateTimeFormatter.ISO_LOCAL_DATE);
exr = imf.getExchangeRate(ConversionQueryBuilder.of().setBaseCurrency("EUR").setTermCurrency("USD").set(LocalDate.class, date).build());
System.out.println(exr.getFactor() + " ==" + (System.currentTimeMillis()-start));

这里 exr.getFactor() 方法给出了给定日期的汇率,但它在一个状态下挂了几个小时。

PF日志在其挂起。

May 27, 2021 4:59:26 PM org.javamoney.moneta.spi.loader.LoadDataLoaderService execute
INFO: Read data from: [https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y]
May 27, 2021 4:59:26 PM org.javamoney.moneta.convert.imf.IMFAbstractRateProvider newDataLoaded
INFO: Loaded IMFHistoricRateProvider exchange rates for days:50
May 27, 2021 4:59:26 PM org.javamoney.moneta.spi.loader.LoadDataLoaderService execute
INFO: New data successfully loaded from: [https://www.imf.org/external/np/fin/data/rms_five.aspx?tsvflag=Y]

它应该给出过去 X 本地日期从 EUR 到 USD 的汇率。

参考:https ://dzone.com/articles/looking-java-9-money-and (汇率)

4

1 回答 1

0

我已将依赖关系从 moneta 更改为更具体的汇率提供商 moneta-convert-ecb

<dependency>
    <groupId>javax.money</groupId>
    <artifactId>money-api</artifactId>
    <version>1.1</version>
</dependency>
<dependency>
    <groupId>org.javamoney.moneta</groupId>
    <artifactId>moneta-core</artifactId>
    <version>1.4.2</version>
</dependency>
<dependency>
    <groupId>org.javamoney.moneta</groupId>
    <artifactId>moneta-convert-ecb</artifactId>
    <version>1.4.2</version>
</dependency>

也变了

IMFHistoricRateProvider imf = new IMFHistoricRateProvider();

ExchangeRateProvider ecb = new ECBHistoricRateProvider();

尽管此修复工作有效,但它只是将提供者从 IMF 更改为 ECB。

注意:IMF 的行为仍然相同。

于 2021-06-14T12:54:22.533 回答