How to format the date into words
I am trying to convert java.util.Date into string as "Twenty Sixth of October Nineteen Ninety Three" for the date 26-10-1993.
How to do this in a gen开发者_如何学运维eric fashion?
You need to extract the 3 parts of the date (the day, the month, the year), and just convert each respectively.
For the day, you're going to need a number to words converter. You can get a rather general one, or alternatively, since your numeric range is limited, you can write a simpler one that only works for the day range.
For the month, just map it to the month name. This should be straightforward.
For the year, you're going to have to be more specific on what the requirement is. Is 2010
"twenty ten"? What about 2009
? In any case, for y=19xx
, 18xx
, etc, you can convert y % 100
and y / 100
to words and concatenate them.
This is because when y=1234
, y / 100 = 12
and y % 100 = 34
. The /
is the integer division in this context, and %
is the remainder operator. Make sure to handle all the special cases, e.g. when y % 100 == 0
, e.g. 1800
should be eighteen hundred
, 1907
should (perhaps) be nineteen o seven
, etc.
Related questions
- Code Golf: Number to Words
精彩评论