Java Money - currency conversion rate on specific date

筅森魡賤 提交于 2019-12-10 18:24:48

问题


I'm trying to get exchange rate on specific date between EUR and USD. The issue is I'm allways getting exchange dare for date: LocalDate{year=2016, month=1, dayOfMonth=8} and it does not matter if I specify date in query.

Here are my maven dependencies:

        <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>

Source code:

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);

    }
}

And the output from 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

Thanks!


回答1:


This API is currently obscure. If you get:

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

It will use following method:

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

As you can see, the conversionQuery is used to get the provider, not the conversion object itself. It's always returned for query with only currency set, never with date.

To make it working use:

MonetaryConversions.getExchangeRateProvider().getCurrencyConversion(conversionQuery)

I believe it should work consistently for all invocations.



来源:https://stackoverflow.com/questions/46150919/java-money-currency-conversion-rate-on-specific-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!