5

The Chinese currency has the ISO 4217 code CNY. Since free global trading in that currency is restricted though, there's a second 'offshore' currency equivalent, called CNH. Wikipedia has a bit of summary of this all.

CNH isn't in ISO 4217, but I'd like to be able to use it in my app without having to write my own Currency class. Presumably there's some kind of list somewhere inside the JVM install. How do I go about adding additional currency codes?

EDIT: See this question for dealing with this in Java 7

Community
  • 1
  • 1
sharakan
  • 6,821
  • 1
  • 34
  • 61

1 Answers1

5

Looks like support for this was added with Java 7.

For earlier versions, you could use an equivalent Currency class of your own devising, or less happily, replace the default java.util.Currency class (or java.util.CurrencyData, which contains the raw data) in your classpath (whitepaper).

Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
  • Got any pointers as to how to swap out a standard JVM class with a custom one? – sharakan Apr 03 '12 at 18:36
  • I added a link, though I'd consider using your own Currency class before replacing the platform one. – Michael Brewer-Davis Apr 03 '12 at 19:06
  • For what it's worth, I was thinking that replacing java.util.Currency would be better, because it'll work nicely with third party libraries (eg Hibernate). But if there's serious reasons why it's a 'less happy' solution...? – sharakan Apr 03 '12 at 19:34
  • 1
    Fixed the link. The replacement class is just trickier to implement and deploy. – Michael Brewer-Davis Apr 03 '12 at 20:37
  • That looks like just what I had in mind, thanks for the help (and the warning). – sharakan Apr 04 '12 at 13:40
  • 1
    Even with Java 7, this may be hard. The Java 7 mechanism associates a single currency with each country. You wouldn't be able to have China having CNY and CNH at the same time. This is a shame. I have a need to represent fractional denominations like pence sterling and US cents as currencies, and the same problem would make it awkward to use the Java 7 mechanism for this. – Tom Anderson Apr 20 '12 at 14:24
  • @TomAnderson Thanks for the heads up. For my use case, I don't really care about the association with Locale or anything similar, so hopefully that'll save me from that difficulty when we upgrade. – sharakan Jun 14 '12 at 21:00
  • 1
    @TomAnderson I figured out a way to do it in Java 7 if you're interested: http://stackoverflow.com/questions/12432527/how-do-i-add-a-new-currency-to-java-util-currency-for-an-existing-country-code-i – sharakan Sep 17 '12 at 13:53
  • Link dead...again. – rpax Nov 20 '17 at 16:14