2017-09-11 92 views
2

我試圖在歐元和美元之間的特定日期獲得匯率。問題是我很高興交換日期爲:LocalDate{year=2016, month=1, dayOfMonth=8},如果我在查詢中指定日期,則無關緊要。Java Money - 特定日期的貨幣兌換率

這裏是我的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 

謝謝!

+0

你正在輸入哪個'LocalDate'類? –

+0

'LocaleDate'看起來像JDK8中的一個。爲什麼使用用JDK7顯式編譯的backport庫('-bp')? – vl4d1m1r4

+0

我使用java.time.LocalDate作爲導入。因爲如果我在沒有bp的情況下使用依賴於money-api的結果,我得到null。而錯誤的導入並不能解釋爲什麼我在第二個例子中得到一些舊日。如果我改變:.set(org.javamoney.moneta.internal.convert.LocalDate.class,org.javamoney.moneta.internal.convert.LocalDate.from(Calendar.getInstance()))我得到同樣的問題。 – Kiki

回答

1

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

我相信它應該對所有調用工作始終。