The part relating to dates and numbers of API in Java (for certification Programmer) covers the following classes:
- Date
- Calendar
- Local
- DateFormat
- NumberFormat
these we can add the use of TimeZone for personal knowledge.
We recognize the potential and capabilities of each class:
Date
The Date class is almost completely deprecata; viene utilizzata per fare da tramite tra Calendar e DateFormat; non usatela mai direttamente (se i metodi sono deprecati un motivo ci sarà).
Calendar
Calendar si ottiene tramite Factory Method (Calendar.getInstance(););
I metodi per ottenerla permettono di indicare un Locale , una TimeZone o entrambe; importante notare come il Locale non sia più modificabile in seguito, laddove esiste il metodo setTimeZone(..) per modificare la TimeZone .
Facile fare confusione per via di questi two objects and trying to better understand how they work:
Local:
indicates the location data for information such as first day of the week , number of weeks in and so forth does not affect in any way Calendar time zone;
TimeZone:
The timezone is used internally by the following methods:
- get
- September (...) (...)
If we execute The following code
Calendar it = Calendar.getInstance (TimeZone.getTimeZone ("Europe / Rome"));
us Calendar = Calendar.getInstance (TimeZone.getTimeZone ("America / Los_Angeles"));
System.out.println (it.get (Calendar.HOUR_OF_DAY));
System.out.println ( us.get (Calendar.HOUR_OF_DAY));
get
23
14
get because the method is affected by timezone. If instead eseguissimo
Calendar it = Calendar.getInstance (TimeZone.getTimeZone ("Europe / Rome"));
us Calendar = Calendar.getInstance (TimeZone.getTimeZone ("America / Los_Angeles"));
System.out.println (it.getTime ());
System.out.println (us . getTime ());
would get
Tue Oct 19 23:51:28 EDT 2010
Tue Oct 19 23:51:28 EDT 2010
this shows how get and September are affected by timezone, while getTime () not.
Calendar of important methods are:
- September and get
- add
- roll
All four methods have the signature: add (int field, int amount) , where field indicates the type of field to work with, and amount the value of change, for example:
it.add (Calendar.DAY_OF_MONTH, 1);
System.out.println (it.getTime ());
it. add (Calendar.MONTH, 1);
System.out.println (it.getTime ());
it.add (Calendar.YEAR, 1);
System.out.println (it.getTime ());
and related:
Wed Oct 20 00:11:12 EDT 2010
Thu Oct 21 00:11:12 EDT 2010
Sun November 21 00:11:12 CET 2010
Mon November 21 00:11:12 CET 2011
method setLenient (boolean) makes the calculations more relaxed, so we
Calendar now = Calendar.getInstance (); now. set (2010, 12, 32); now.setLenient (false) System.out.println (now.get (Calendar.DAY_OF_MONTH)); / / exception java.lang.IllegalArgumentException: MONTH now.setLenient (true) ; System.out.println (now.get (Calendar.DAY_OF_MONTH)) / / correct
Local
indicates a specific geographical area, with values \u200b\u200bof their language, has two constructors main
- Local (String language);
- Local (String language, String country);
and there are also constant for each defined language and County system, the space is used to identify a place, and with Format allows you to display localized data (in the correct format, not with different values). The static method
Locale.getAvailableLocales () can get all defined on the local machine.
DateFormat
has three forms of Factory Method, depending on what you want to be given: date, time, date and time, we see directly the most complete of the three:
DateFormat df = DateFormat.getDateTimeInstance (DATESTYLE int, int timeStyle, Local aLocale)
the DateFormat is obtained at the site indicated, and shows more or less information (depending on style), we see in action:
Date d = Calendar.getInstance (). getTime ();
DateFormat dfS = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.ITALY);
DateFormat dfD = DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.ITALY);
DateFormat dfL = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.ITALY);
DateFormat dfF = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, Locale.ITALY);
System.out.println(dfS.format(d));
System.out.println (dfD.format (d));
System.out.println (dfL.format (d));
System.out.println (dfF.format (d));
and its results:
20/10/10 00:03
20-Oct 2010 00:03:02
October 20, 2010 00:03:02 EDT
Wednesday, October 20, 2010 12:03:02 BST
as we see, are all shown at the site, we try to run one on a different local:
DateFormat = dfLUSA DateFormat.getDateTimeInstance (DateFormat.LONG, DateFormat.LONG, Locale.US)
System.out.println (dfLUSA.format (d));
and result:
October 20, 2010 12:04:57 AM EDT
Thus we see that the room varies only the type of data display, even if we change the time value, just use the TimeZone, just as with the Calendar:
DateFormat dfLUSA DateFormat.getDateTimeInstance = (DateFormat.LONG, DateFormat.LONG, Locale.US)
System.out.println (dfLUSA.format (d));
dfLUSA.setTimeZone (TimeZone.getTimeZone (" America / Los_Angeles "));
System.out.println (dfLUSA.format (d));
and Results:
October 20, 2010 12:07:05 AM EDT
October 19, 2010 3:07:05 PM PDT
The reverse logic applies to the method DateFormat.Parse (String), which takes a string and returns a Date associated, we see some examples:
DateFormat DateFormat.getDateTimeInstance dfs = (DateFormat.SHORT, DateFormat.SHORT, Locale.ITALY)
dfS.parse ds = Date ("20/10/10 12:11" ) / / returns a valid Date
Date dfS.parse ds2 = ("Wed Oct 20 00:13:55 EDT 2010") / / throw exception java.text.ParseException: Unparseable dates "Wed Oct 20 00:13:55 PDT 2010"
Pay attention to:
dfLUSA.parse (October 19, 2010 3:16:12 PM PDT ") / / correct
dfL.parse (October 19, 2010 3:16:12 PM PDT ") / / exception
since October is not recognized in the local Italian.
A utility method is setLenient (true) that makes parsing more relaxed, if we try to parse the date 40/12/2010 we get
NumberFormat
N umberFormat formatting includes three categories:
- Numbers
NumberFormat = nFit NumberFormat.getNumberInstance (Locale.ITALY)
NumberFormat NFuse NumberFormat.getNumberInstance = (Locale.US)
NumberFormat = nfJA NumberFormat.getNumberInstance (Locale.JAPAN)
System.out.println (nfIT.format (12345.67890));
System.out.println (nfUS.format (12345.67890));
System.out.println (nfJA.format (12345.67890));
12,345.679 12,345.679
12,345.679
V ediamo now that the differences relate to the character of the thousands and decimal
- Currencies
number format nfIT = NumberFormat.getCurrencyInstance (Locale.ITALY);
number format nfUS = NumberFormat.getCurrencyInstance (Locale.US);
number format nfJA NumberFormat.getCurrencyInstance = (Locale . JAPAN);
System.out.println (nfIT.format (12345.67890));
System.out.println (nfUS.format (12345.67890));
System.out.println ( nfJA.format (12345.67890));
€ 12,345.68
$ 12,345.68
? 12.346
Here we have two new features: the symbol of currency changes (and in the case of Japan we did not on the system), and in some cases we can not have decimal values \u200b\u200b(as in Italy, when we had the Lira)
- Percentages
NumberFormat = nFit NumberFormat.getPercentInstance (Locale.ITALY) NumberFormat = NumberFormat.getPercentInstance NFuse (Locale.US)
NumberFormat = nfJA NumberFormat.getPercentInstance (Locale.JAPAN)
System.out.println (nfIT.format (12345.67890));
System.out.println (nfUS.format (12345.67890));
System.out.println (nfJA.format (12345.67890));
1,234,568% 1,234,568% 1,234,568%
The changes are equal to those numbers;
Even if the numbers we have a method NumberFormat.Parse (String) which follows the same logic DateFormat.Parse (String)
0 comments:
Post a Comment