Number Formats Number formats vary in different cultures
Number Formats • Number formats vary in different cultures • Group (e. g. thousands) and decimal separators e. g. 1, 234. 56 (US) vs 1 234, 56 (France) • Negative numbers e. g. -123 (US) vs 123 - (Arabic) • Number formats are locale-sensitive. • Number formats should be presented correctly for a user’s / customer’s locale.
Currency • Different name • Different symbol • Fractional currency? – Dollars, Cents • Decimal System? – Typically, however… – Mauritania • Ouguiya is divided into 5 khoum – Madagascar • Malegasy ariary is divided into 5 iraimbilanja • Example of the importance of correct currency formats: – Cost of an article of clothing: $123, 56 [English (United States)] – Cost of a car: $25. 345, 99 [Portuguese (Brazil)]
Currency Sign • • dollar sign U+0024 $ pound sign U+00 A 3 £ yen sign U+00 A 5 ¥ yen character U+5186 円 euro sign U+20 AC € thai baht sign U+0 E 3 F ฿ french franc sign U+20 A 3 ₣ lira sign U+20 A 4 ₤ • Source: http: //www. xencraft. com/resources/multi-currency. html
Currency Placement • Canadian French 123, 45 $ – Currency is a suffix • Canadian English $123. 45 – Currency is a prefix • Canadian Inuktitut $123. 45 – Currency in Nunavut is written same as English. • Source: http: //www. xencraft. com/resources/multi-currency. html
Fractional Separators • Portuguese Escudos 12$34 ESC – Fractional separator is dollar sign • German Deutsch Marks 12, 34 DM – Fractional separator is comma • Canadian Dollars (English) $12. 34 – Fractional separator is full-stop • Canadian Dollars (French) 12, 34 $ – Fractional separator is comma • Swiss Numbers 12'345, 67 – Fractional separator is comma • Swiss Francs SFr 12'345. 67 – Fractional separator is full-stop • Source: http: //www. xencraft. com/resources/multi-currency. html
Grouping • British 1, 234, 567, 890. 12 – Groups of 3 • Chinese, Japanese 12億 3456万 7890. 12 or 12億 3, 456万 7, 890. 12 – Groups of 4, separated by ideographic characters and optionally a thousands separator. • Indian 1, 23, 45, 67, 890. 12 – One group of 3, then groups of 2 • German 1. 234. 567. 890, 12 – Groups of 3, separator is full-stop • Swiss 1'234'567'890, 12 – Groups of 3, separator is apostrophe • French 1 234 567 890, 12 – Groups of 3, separator is a thin non-breaking space • Source: http: //www. xencraft. com/resources/multi-currency. html
Other issues • Rounding – Round up? Round down? – Rounding should follow local conventions – Largely application specific… • List Separators – 1. 23, 4. 56, 7. 89 – 1, 23, 4, 56, 7, 89 – 1 234 5 678 9 123 • Number Width – Different currencies will require different number width fields.
Java Number Formatting • Java Class Number. Format • Number. Format is the abstract base class for all number formats. • This class provides the interface formatting and parsing numbers. • Number. Format also provides methods for determining which locales have number formats, and what their names are.
Java Number Formatting cont. • static Number. Format get. Currency. Instance() – Returns a currency format for the current default locale. • static Number. Format get. Currency. Instance(Locale in. Locale) – Returns a currency format for the specified locale. • static Number. Format get. Instance() – Returns the default number format for the current default locale. • static Number. Format get. Instance(Locale in. Locale) – Returns the default number format for the specified locale. • static Number. Format get. Integer. Instance() – Returns an integer number format for the current default locale. • static Number. Format get. Integer. Instance(Locale in. Locale) – Returns an integer number format for the specified locale. • static Number. Format get. Number. Instance() – Returns a general-purpose number format for the current default locale. • static Number. Format get. Number. Instance(Locale in. Locale) – • Returns a general-purpose number format for the specified locale. static Number. Format get. Percent. Instance() – Returns a percentage format for the current default locale. • static Number. Format get. Percent. Instance(Locale in. Locale) – Returns a percentage format for the specified locale.
Java Number Formatting cont. • Number to String Number. Format nf = Number. Format. get. Number. Instance(); // Or use a specific locale // get. Integer. Instance(Locale in. Locale) double quantity = 12345. 678; String str. Quantity = nf. format(quantity); System. out. println(str. Quantity); 12, 345. 678
Java Number Formatting cont. • String to Number. Format nf = Number. Format. get. Number. Instance(); // Or use a specific locale // get. Integer. Instance(Locale in. Locale) String str. Quantity = 12345. 678; Number num = nf. parse(str. Quantity);
Java Number Formatting cont. • Alignment, particularly important for currency int column. Width = 10; double number = 23321. 123; String. Buffer str. Number = new String. Buffer(); Field. Position pos = new Field. Position(Number. Format. INTEGER_FIELD); nf. format(number, str. Number, pos); // Retrieve the updated index to the decimal point. index = pos. get. End. Index(); int pad = column. Width - index; char[] pad. Chars = new char[pad]; for (int x = 0; x< pad; x++) { pad. Chars[x] = ' '; } str. Number. insert(0, pad. Chars); println(str. Number. to. String());
Java Number Formatting cont. • Java Class Decimal. Format • Decimal. Format is a concrete subclass of Number. Format that formats decimal numbers. • A Decimal. Format comprises a pattern and a set of symbols. • The pattern may be set directly using apply. Pattern(), or indirectly using the API methods. • When using the Number. Format factory methods, the pattern and symbols are read from localized Resource. Bundles.
Number Formatting Patterns Symbol 0 #. , E Location Localized? Meaning Number Yes Digit, zero shows as absent Number Yes Decimal separator or monetary decimal separator Number Yes Minus sign Number Yes Grouping separator Number Yes Separates mantissa and exponent in scientific notation. Need not be quoted in prefix or suffix. ; Subpattern boundary Yes Separates positive and negative subpatterns % Prefix or suffix Yes Multiply by 100 and show as percentage u 2030 Prefix or suffix Yes Multiply by 1000 and show as per mille ¤ (u 00 A 4) Prefix or suffix No Currency sign, replaced by currency symbol. If doubled, replaced by international currency symbol. If present in a pattern, the monetary decimal separator is used instead of the decimal separator. ' Prefix or suffix No Used to quote special characters in a prefix or suffix, for example, "'#'#" formats 123 to "#123". To create a single quote itself, use two in a row: "# o''clock".
• A Decimal. Format pattern contains a positive and negative subpattern. • Each subpattern has a prefix, numeric part, and suffix. • For example, "#, ##0. 00; (#, ##0. 00)". • BNF pattern : = subpattern{; subpattern} subpattern : = {prefix}integer{. fraction}{suffix} prefix : = '\u 0000'. . '\u. FFFD' - special. Characters suffix : = '\u 0000'. . '\u. FFFD' - special. Characters integer : = '#'* '0' fraction : = '0'* '#'*
Example Patterns • • The 0 symbol shows a digit or 0 if no digit present Number. Format formatter = new Decimal. Format("000000"); String s = formatter. format(-1234. 567); // -001235 Notice that the number was rounded up • The # symbol shows a digit or nothing if no digit present formatter = new Decimal. Format("##"); s = formatter. format(-1234. 567); // -1235 s = formatter. format(0); // 0 formatter = new Decimal. Format("##00"); s = formatter. format(0); // 00 • The. symbol indicates the decimal point formatter = new Decimal. Format(". 00"); s = formatter. format(-. 567); // -. 57 formatter = new Decimal. Format(". ######"); s = formatter. format(-1234. 567); // -1234. 567 formatter = new Decimal. Format("#. 000000"); s = formatter. format(-1234. 567); // -1234. 567000
Example Patterns Cont. • The , symbol is used to group numbers formatter = new Decimal. Format("#, ###"); s = formatter. format(-1234. 567); // -1, 235 s = formatter. format(-1234567. 890); // -1, 234, 568 • The ; symbol is used to specify an alternate pattern for negative values formatter = new Decimal. Format("#; (#)"); s = formatter. format(-1234. 567); // (1235) • The ' symbol is used to quote literal symbols formatter = new Decimal. Format("'#'#"); s = formatter. format(-1234. 567); // -#1235 formatter = new Decimal. Format("'abc'#"); s = formatter. format(-1234. 567); // -abc 1235
Dates • This is based on the "Java Internationalization" book • Dates come in many formats, even in the US: – 10/4/99; October 4, 1999; 4 Oct 99; etc • In different languages and locales these are expressed differently – e. g. 4 octobre 1999 in France • The names of the days and months change • Capitalization rules differ: – January in English, janvier in French • Order of components varies: – day/month in Aus, month/day in the US • What date is this? – 03/04/02
Java Date Formatting • Date. Format class • The standard Java class for handling dates and times in different locales • The static method Date. Format. get. Date. Instance() will get a date format object • The parameters are the date format (short, medium, long, full) and a locale Date. Format df = Date. Format. get. Date. Instance(Date. Format. FULL, Locale. FRANCE); • To get a formatted string out of this, pass in a Date object to format() String result = df. format(new Date());
Java Date Formatting • The same class is used to format times using Date. Format. get. Time. Instance() • . . . and get both a time and date formatter by Date. Format. get. Date. Time. Instance()
Display length • SHORT is completely numeric – 12. 13. 52 or 3: 30 pm • MEDIUM is longer – Jan 12, 1952 • LONG is longer still – January 12, 1952 or 3: 30: 32 pm • FULL is pretty completely specified – Tuesday, April 12, 1952 AD or 3: 30: 42 pm PST.
Example Date. Format output U. S. Locale German Locale DEFAULT 3: 58: 45 PM 15: 58: 45 SHORT 3: 58 PM 15: 58 MEDIUM 3: 58: 45 PM 15: 58: 45 LONG 3: 58: 45 PM PDT 15: 58: 45 GMT+02: 00 FULL 3: 58: 45 oclock PM PDT 15. 58 Uhr GMT+02: 00
Java Date Formatting cont. • Defining your own format • Simple. Date. Format is a concrete class formatting and parsing dates in a locale-sensitive manner. • get. Time. Instance, get. Date. Instance, or get. Date. Time. Instance in Date. Format provides formatters for default date formatting. • Simple. Date. Format allows user-defined patterns for datetime formatting. • However, you may modify the format pattern using the apply. Pattern methods as desired.
Date Formatting Patterns Letter Date or Time Component Presentation Examples G Era designator Text AD [AD / CE or BC/BCE? ] y Year 1996; 96 M Month in year Month July; Jul; 07 w Week in year Number 27 W Week in month Number 2 D Day in year Number 189 d Day in month Number 10 F Day of week in month Number 2 E Day in week Text Tuesday; Tue a Am/pm marker Text PM H Hour in day (0 -23) Number 0 k Hour in day (1 -24) Number 24 K Hour in am/pm (0 -11) Number 0 h Hour in am/pm (1 -12) Number 12 m Minute in hour Number 30 s Second in minute Number 55 S Millisecond Number 978 z Time zone General time zone Pacific Standard Time; PST; GMT-08: 00 Z Time zone RFC 822 time zone -0800
Date Formatting Patterns • "yyyy. MM. dd G 'at' HH: mm: ss z“ -> 2001. 07. 04 AD at 12: 08: 56 PDT • "EEE, MMM d, ''yy" • "h: mm a" • "hh 'o''clock' a, zzzz" • "K: mm a, z" • "yyyyy. MMMMM. dd GGG hh: mm aaa" • "EEE, d MMM yyyy HH: mm: ss Z" • "yy. MMdd. HHmmss. Z" -> -> Wed, Jul 4, '01 12: 08 PM -> -> 12 o'clock PM, Pacific Daylight Time 0: 08 PM, PDT -> 02001. July. 04 AD 12: 08 PM -> Wed, 4 Jul 2001 12: 08: 56 -0700 -> 010704120856 -0700
Parsing Dates while ((date. String != null) && (date. String. length() > 0)) { try { Date date = format. parse(date. String); System. out. println("Original string: " + date. String); System. out. println("Parsed date : " + date. to. String()); System. out. println(); // Skip a line } catch(Parse. Exception pe) { System. out. println("ERROR: could not parse date in string "" + date. String + """); } // Read another string System. out. print("ENTER DATE STRING: "); date. String = reader. read. Line(); }
Calendars • Solar Calendar – Solar calendars assign a date to each solar day. A day may consist of the period between sunrise and sunset, with a following period of night. • Julian Calendar – It has a regular year of 365 days divided into 12 months, and a leap day is added every 4 years, hence the average Julian year is 365. 25 days. – The calendar remained in use into the 20 th century in some countries and is still used by many national Orthodox churches. – Unfortunately, Earth's tropical year is a little less than 365. 25 days (it is approximately 365. 2422 days), so this calendar, slowly drifted out of sync with the seasons. Hence, the Gregorian calendar was later adopted by most of the West, starting in 1582, and it has since become the world's dominant civic calendar.
Calendars cont. • Gregorian Calendar – Defacto International standard – Lunar calendars – A lunar calendar is a calendar whose date indicates the moon phase. – Islamic calendar • A lunisolar calendar is a calendar whose date indicates both the moon phase and the season. – – Hebrew calendar Chinese calendar Tibetan calendar and some Hindu calendars.
Calendars cont. • Fiscal calendars – A fiscal calendar fixes each month at a specific number of weeks to facilitate comparisons from month to month and year to year. – January always has exactly 5 weeks (Sunday through Saturday), February has 4 weeks, March has 4 weeks, etc. – ISO 8601
Java Calendar Class • A Date object represents a specific instant in time with millisecond precision. • Whereas, a Calendar is an abstract base class for converting between a Date object and a set of integer fields such as YEAR, MONTH, DAY, HOUR, and so on. • Subclasses of Calendar interpret a Date according to the rules of a specific calendar system. • The Java platform provides one concrete subclass of Calendar: Gregorian. Calendar
Calendar Examples • Computing a persons age Calendar date. Of. Birth = new Gregorian. Calendar(1972, Calendar. JANUARY, 27); Calendar today = Calendar. get. Instance(); int age = today. get(Calendar. YEAR) date. Of. Birth. get(Calendar. YEAR); // Add the tentative age to the date of birth to get this year's birthday date. Of. Birth. add(Calendar. YEAR, age); // If this year's birthday has not happened yet, subtract one from age if (today. before(date. Of. Birth)) { age--; }
Calendar Examples cont. • Before / After Calendar xmas = new Gregorian. Calendar(1998, Calendar. DECEMBER, 25); Calendar newyears = new Gregorian. Calendar(1999, Calendar. JANUARY, 1); // Determine which is earlier boolean b = xmas. after(newyears); b = xmas. before(newyears); // false // true // Get difference in milliseconds long diff. Millis = newyears. get. Time. In. Millis()-xmas. get. Time. In. Millis(); • // Get difference in seconds long diff. Secs = diff. Millis/(1000); // 604800 // Get difference in minutes long diff. Mins = diff. Millis/(60*1000); // 10080 // Get difference in hours long diff. Hours = diff. Millis/(60*60*1000); // 168 // Get difference in days long diff. Days = diff. Millis/(24*60*60*1000); // 7 Note: Is there a better way of doing this?
Calendar Examples cont. • Day of the week • The day-of-week is an integer value where 1 is Sunday, 2 is Monday, . . . , and 7 is Saturday Calendar xmas = new Gregorian. Calendar(1998, Calendar. JANUARY, 25); int day. Of. Week = xmas. get(Calendar. DAY_OF_WEEK); // 6=Friday Calendar cal = new Gregorian. Calendar(2003, Calendar. JANUARY, 1); day. Of. Week = cal. get(Calendar. DAY_OF_WEEK); // 4=Wednesday
Time Zones
Time Zones cont. • Solar time. • When the sun reaches its highest point in the sky, it is noon. Different cities had different times… • As telecommunications improved and with the expansion of the railways this became increasingly awkward. • Time zones are generally centered on meridians of a longitude that is a multiple of 15° thus making neighboring time zones one hour apart. • However, the one hour separation is not universal and, as the map demonstrates. • All time zones are defined relative to Coordinated Universal Time (UTC). • The reference point for Time Zones is the Prime Meridian (longitude 0°) which passes through the Royal Greenwich Observatory in Greenwich, London, England. • Greenwich Mean Time (GMT).
Time Zones cont. • Los Angeles, California, USA: UTC − 8 – (e. g. if it is 12: 00 UTC, then it is 04: 00 in Los Angeles) • Toronto, Ontario, Canada: UTC − 5 – (e. g. if it is 11: 00 UTC, then it is 06: 00 in Toronto) • Stockholm, Sweden: UTC + 1 – (e. g. if it is 12: 00 UTC, then it is 13: 00 in Stockholm) • Melbourne, Japan: UTC + 10 – (e. g. if it is 11: 00 UTC, then it is 21: 00 in Tokyo) • Note: The time zone adjustment for a specific location may vary due to the use of daylight saving time. • e. g. New Zealand which is usually UTC + 12, observes a one-hour daylight saving time adjustment during the southern hemisphere summer resulting in a local time of UTC + 13!
Daylight savings • Pros – Energy conservation – “United States Department of Transportation studies showed that DST reduces the country's electricity usage by one percent during each day DST is in effect. ” – Increased opportunities for outdoor activities & decreased crime. • Cons – Agriculture – the cows don’t follow it.
Java Time Zones • Java Class Time. Zone • Time. Zone represents a time zone offset, and also figures out daylight savings. • Simple. Time. Zone is a concrete subclass of Time. Zone that represents a time zone for use with a Gregorian calendar. • The class holds an offset from GMT, called raw offset, and start and end rules for a daylight saving time schedule.
Java Time Zones cont. • Get the current time in Hong Kong Calendar cal = new Gregorian. Calendar(Time. Zone. get. Time. Zone("Hongkong")); int hour 12 = cal. get(Calendar. HOUR); // 0. . 11 int minutes = cal. get(Calendar. MINUTE); // 0. . 59 int seconds = cal. get(Calendar. SECOND); // 0. . 59 boolean am = cal. get(Calendar. AM_PM) == Calendar. AM; • Get the current hour-of-day at GMT cal. set. Time. Zone(Time. Zone. get. Time. Zone("GMT")); int hour 24 = cal. get(Calendar. HOUR_OF_DAY); // 0. . 23 • Get the current local hour-of-day cal. set. Time. Zone(Time. Zone. get. Default()); hour 24 = cal. get(Calendar. HOUR_OF_DAY); // 0. . 23
Java Time Zones cont. • Does the time zone have a daylight savings time period? boolean has. DST = tz. use. Daylight. Time(); • Is the time zone currently in a daylight savings time? boolean in. DST = tz. in. Daylight. Time(today);
- Slides: 40