|
| 1 | +/** |
| 2 | + * Copyright (c) 2012, 2020, Anatole Tresch, Werner Keil and others by the @author tag. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.javamoney.examples.console.java10.convert; |
| 17 | + |
| 18 | +import org.javamoney.moneta.Money; |
| 19 | + |
| 20 | +import javax.money.MonetaryAmount; |
| 21 | +import javax.money.convert.ConversionQueryBuilder; |
| 22 | +import javax.money.convert.CurrencyConversion; |
| 23 | +import javax.money.convert.MonetaryConversions; |
| 24 | + |
| 25 | +import java.text.MessageFormat; |
| 26 | +import java.time.LocalDate; |
| 27 | + |
| 28 | +/** |
| 29 | + * Showing accessing exchange rates and doing conversions. |
| 30 | + */ |
| 31 | +public class ConversionExample { |
| 32 | + private static final String DEFAULT_TERM_CURRENCY_CODE = "CHF"; |
| 33 | + |
| 34 | + public static void main(String... args) { |
| 35 | + String termCurrencyCode = DEFAULT_TERM_CURRENCY_CODE; |
| 36 | + if (args.length > 0) { |
| 37 | + termCurrencyCode = args[0]; |
| 38 | + } |
| 39 | + final MonetaryAmount amt = Money.of(2000, "EUR"); |
| 40 | + CurrencyConversion conv= MonetaryConversions.getConversion(termCurrencyCode, "ECB"); |
| 41 | + System.out.println(MessageFormat.format("2000 EUR (ECB)-> {0} = {1}", |
| 42 | + termCurrencyCode, amt.with(conv))); |
| 43 | + conv= MonetaryConversions.getConversion(termCurrencyCode, "IMF"); |
| 44 | + System.out.println(MessageFormat.format("2000 EUR (IMF)-> {0} = {1}", |
| 45 | + termCurrencyCode, amt.with(conv))); |
| 46 | + |
| 47 | + System.out.println(MessageFormat.format( |
| 48 | + "2000 EUR (ECB, at 5th Jan 2015)-> {0} = {1}", |
| 49 | + termCurrencyCode, amt.with(MonetaryConversions |
| 50 | + .getConversion(ConversionQueryBuilder.of() |
| 51 | + .setTermCurrency(termCurrencyCode) |
| 52 | + .set(LocalDate.of(2015, 01, 05)).build())))); |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments