本文整理汇总了Java中javax.money.convert.CurrencyConversion类的典型用法代码示例。如果您正苦于以下问题:Java CurrencyConversion类的具体用法?Java CurrencyConversion怎么用?Java CurrencyConversion使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CurrencyConversion类属于javax.money.convert包,在下文中一共展示了CurrencyConversion类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testProviderComparison
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void testProviderComparison() {
ExchangeRateProvider ecbRateProvider = MonetaryConversions.getExchangeRateProvider("ECB");
ExchangeRateProvider imfRateProvider = MonetaryConversions.getExchangeRateProvider("IMF");
CurrencyConversion ecbDollarConversion = ecbRateProvider.getCurrencyConversion("USD");
CurrencyConversion imfDollarConversion = imfRateProvider.getCurrencyConversion("USD");
try {
// Wait for IMF provider to load
Thread.sleep(10000L);
for (String currency : new String[]{"INR", "CHF", "BRL"}) {
Money money = Money.of(2, currency);
System.out.println("ECB : " + money.with(ecbDollarConversion));
System.out.println("IMF : " + money.with(imfDollarConversion));
assertEquals("Too much difference (ECB/IMF) for " + money,
money.with(ecbDollarConversion).getNumber().doubleValue(), money.with(imfDollarConversion).getNumber().doubleValue(), 0.4d);
}
} catch (Exception e) {
// This test may fail, if the network is slow or not available, so only write the exception as of now...
e.printStackTrace();
}
}
示例2: shouldSetTimeInLocalDateTime
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldSetTimeInLocalDateTime() {
Calendar localDate = GregorianCalendar.getInstance();
localDate.add(Calendar.DAY_OF_YEAR, -80);
localDate.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例3: shouldSetTimeInLocalDateTime
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldSetTimeInLocalDateTime() {
Calendar localDate = new GregorianCalendar(2014, Calendar.JANUARY, 9);
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例4: shouldSetTimeInLocalDateTime2
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldSetTimeInLocalDateTime2() {
LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = HistoricConversionQueryBuilder
.of(EURO).withDay(localDate).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例5: shouldFindFromHistoricalUsingPeriod
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldFindFromHistoricalUsingPeriod() {
LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = HistoricConversionQueryBuilder
.of(EURO).withDaysBetween(localDate, localDate.plusDays(10)).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例6: shouldHaveExchangeRates
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldHaveExchangeRates() {
CurrencyConversion currencyConversion = provider.getCurrencyConversion(DOLLAR);
assertNotNull(currencyConversion);
int count = 0;
for (Currency currency : Currency.getAvailableCurrencies()) {
MonetaryAmount money = Money.of(BigDecimal.ONE, currency.getCurrencyCode());
try {
MonetaryAmount result = currencyConversion.apply(money);
assertTrue(result.getNumber().doubleValue() > 0);
count++;
} catch(Exception e) {
//not a supported currency
}
}
assertTrue(count >=24);
}
示例7: shouldSetTimeInLocalDateTime2
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldSetTimeInLocalDateTime2() {
LocalDate localDate = LocalDate.now().minusDays(90)
.with(TemporalAdjusters.next(DayOfWeek.FRIDAY));
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例8: shouldReturnErrorWhenFindFromHistoricalUsingPeriod
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test(expectedExceptions = MonetaryException.class)
public void shouldReturnErrorWhenFindFromHistoricalUsingPeriod() {
LocalDate localDate = YearMonth.of(2011, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(EURO)
.set(withDaysBetween(localDate.minusDays(1), localDate))
.build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
currencyConversion.apply(money);
fail();
}
示例9: shouldSetTimeInLocalDateTime
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test(expectedExceptions = MonetaryException.class)
public void shouldSetTimeInLocalDateTime() {
LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例10: shouldSetTimeInLocalDateTime
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
@Test
public void shouldSetTimeInLocalDateTime() {
LocalDate localDate = YearMonth.of(2014, Month.JANUARY).atDay(9);
ConversionQuery conversionQuery = ConversionQueryBuilder.of()
.setTermCurrency(EURO).set(localDate).build();
CurrencyConversion currencyConversion = provider
.getCurrencyConversion(conversionQuery);
assertNotNull(currencyConversion);
MonetaryAmount money = Money.of(BigDecimal.TEN, DOLLAR);
MonetaryAmount result = currencyConversion.apply(money);
assertEquals(result.getCurrency(), EURO);
assertTrue(result.getNumber().doubleValue() > 0);
}
示例11: main
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
public static void main(String[] args) {
ExchangeRateProvider imfRateProvider = MonetaryConversions
.getExchangeRateProvider("IMF");
ExchangeRateProvider ecbRateProvider = MonetaryConversions
.getExchangeRateProvider("ECB");
CurrencyUnit real = Monetary.getCurrency("BRL");
CurrencyUnit dollar = Monetary.getCurrency(Locale.US);
CurrencyConversion ecbDollarConvertion = ecbRateProvider
.getCurrencyConversion(dollar);
CurrencyConversion imfDollarConvertion = imfRateProvider
.getCurrencyConversion(dollar);
MonetaryAmount money = Money.of(10, real);
System.out.println(money.with(ecbDollarConvertion));
System.out.println(money.with(imfDollarConvertion));
}
示例12: main
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
public static void main(String... args) {
String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
if (args.length > 0) {
termCurrencyCode = args[0];
}
final MonetaryAmount amt = Money.of(2000, "EUR");
CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
termCurrencyCode, amt.with(conv)));
conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
termCurrencyCode, amt.with(conv)));
System.out.println(MessageFormat.format(
"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
termCurrencyCode, amt.with(MonetaryConversions
.getConversion(ConversionQueryBuilder.of()
.setTermCurrency(termCurrencyCode)
//.set(LocalDate.of(2015, 01, 05))
.build()))));
}
示例13: main
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
public static void main(String... args) {
String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE;
if (args.length > 0) {
termCurrencyCode = args[0];
}
final MonetaryAmount amt = Money.of(2000, "EUR");
CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB");
System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}",
termCurrencyCode, amt.with(conv)));
conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF");
System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}",
termCurrencyCode, amt.with(conv)));
System.out.println(MessageFormat.format(
"2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}",
termCurrencyCode, amt.with(MonetaryConversions
.getConversion(ConversionQueryBuilder.of()
.setTermCurrency(termCurrencyCode)
.set(LocalDate.of(2015, 01, 05)).build()))));
}
示例14: computeNextHopLocalTransferAmount
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
/**
* Given a source transfer, compute the amount (in local units of the destination ledger) that should be transferred
* to the next-hop local ledger.
*
* @param sourceTransfer A {@link Transfer} with information from the incoming source transfer delivered to
* this connector as part of a broader Interledger payment.
* @param destinationLedgerPrefix An {@link InterledgerAddress} prefix for the destination ledger that the next-hop
* tranfser will be delivered on.
*/
@VisibleForTesting
protected BigInteger computeNextHopLocalTransferAmount(
final Transfer sourceTransfer,
final InterledgerAddress destinationLedgerPrefix
) {
Objects.requireNonNull(sourceTransfer);
InterledgerAddress.requireAddressPrefix(destinationLedgerPrefix);
final CurrencyUnit baseCurrencyUnit = this.ledgerPluginManager.getLedgerPlugin(sourceTransfer.getLedgerPrefix())
.map(LedgerPlugin::getLedgerInfo)
.map(LedgerInfo::getCurrencyUnit)
.orElseThrow(() -> new LedgerPluginNotConnectedException(sourceTransfer.getLedgerPrefix()));
final CurrencyUnit terminatingCurrencyUnit = this.ledgerPluginManager.getLedgerPlugin(destinationLedgerPrefix)
.map(LedgerPlugin::getLedgerInfo)
.map(LedgerInfo::getCurrencyUnit)
.orElseThrow(() -> new LedgerPluginNotConnectedException(destinationLedgerPrefix));
// This method does not catch the CurrencyConversionException because there's nothing to be done if that occurs. It
// means that FX is not configured between the source/destination ledgers.
return Optional.ofNullable(this.fxEngine.getExchangeRate(baseCurrencyUnit, terminatingCurrencyUnit))
.map(fxRate -> {
final MonetaryAmount sourceTransferMonetaryAmount = Money.of(sourceTransfer.getAmount(), baseCurrencyUnit);
final CurrencyConversion conversion = MonetaryConversions.getConversion(terminatingCurrencyUnit);
final MonetaryAmount terminatingMonetaryAmount = sourceTransferMonetaryAmount.with(conversion);
return terminatingMonetaryAmount;
})
// Convert from a MonetaryAmount back to an Integer...
.map(MonetaryAmount::getNumber)
.map(numberValue -> numberValue.numberValueExact(BigInteger.class))
// This is allowed because if fxEngine.getExchangeRate can't find a rate, it will throw an exception!
.get();
}
示例15: main
import javax.money.convert.CurrencyConversion; //导入依赖的package包/类
public static void main(String[] args) {
CurrencyUnit dollar = Monetary.getCurrency("USD");
CurrencyUnit real = Monetary.getCurrency("BRL");
MonetaryAmount money = FastMoney.of(10, dollar);
MonetaryAmount money2 = FastMoney.of(10, real);
ExchangeRateProvider provider = MonetaryConversions.getExchangeRateProvider(ExchangeRateType.ECB);
CurrencyConversion currencyConversion = provider.getCurrencyConversion(dollar);
MonetaryAmount result = currencyConversion.apply(money2);
MonetaryAmount monetaryAmount = money.add(result);
}