If your Java program displays time and date in different time zones or users of different countries, you need to know some more advanced aspects of the Java date class. In this article, using Java Date and Calendar class, custom and parsing date, we provide an overview of formatting, date data, and date calculations for date, date data. The in-depth understanding of these concepts is critical to discussing these related dates such as time zones, international standard formats, and SQL date data. The classes we discussed herein will include java.text.dateFormat, and java.util.timezone and java.util.locate. We will also discuss how to use a java.sql.date of a java.util.date to extract and save Java date data from the Oracle database. Regional issues Before we internationalize our date data, we need further learning Locale classes, which is java.util.locale. An instance of the Locale class usually contains national and language information. Each of these parts is constituted by a string of two characters, ISO-3166 and language code ISO-639, which is developed by international standardization organization (ISO). Let us create two Locale instances, one of the corresponding US English and the other corresponds to French French. See Table A. Table A Import Java.util.Locale; Public Class Dateexample6 {Public Static Void Main (String [] args) {// Create a Locale for the English Language In The US.LOCALE LOCALEEN = New Locale ("en", "US" ); System.out.println ("Display Name:" localeen.getdisplayName ()); system.out.println ("Country:" localeen.getCountry ()); System.Out.println ("Language:" Localeen.getlanguage ()); // Create a Locale for the French Language in France.locale LocaleFr = New Locale ("fr", "fr"); system.out.println ("/ ndisplay name:" localefr.getdisplayName ()); System.out.println ("Country:" localefr.getCountry ()); System.out.println ("Language:" localefr.getlanguage ()); // Display the english-us locale in Frenchsystem .out.println ("/ Nen Display Name In French:" }} In this example, we use the getDisplayName method to display a more readable text of Locale. You should also notice that when we call getDisplayName in the last time, we passed the French Locale object at the same time when calling getDisplayName to the English Locale object. This allows us to choose the language used to display the Locale object, let us display the content of the French Locale object in English.
Below is the output of this example: Display name: English (United States) Country: USLANGuage: endisplay name: French (FRANCE) Country: FRLANGUAGE: FREN DISPLAY NAME IN FRENCH: Anglais (états-unis) Date formatted using multiple regions Java.util.locale and Java.Text.dateFormat classes We can format the date data to display it to users in another region, fifty France. The example in Table B creates a complete date formatter in English and French. Table B import java.util.Locale; import java.util.Date; import java.text.DateFormat; public class DateExample7 {public static void main (String [] args) {// Get the current system date and time.Date date = New Date (); // Get a France Locale Using A Locale Constant.Locale LocaleFr = locale.france; // Create An English / US Locale Using The Constructor.locale Localeen = New Locale ("En", "US") ; // Get a date time formatter for display in France.DateFormat fullDateFormatFR = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL, localeFR); // Get a date time formatter for display in the USDateFormat fullDateFormatEN = DateFormat.getDateTimeInstance ( DateFormat.full, Localeen; System.out.println ("Locale:" localefr.getdisplayName ()); System.out.Println (fulldateformatfr.format (date); system.out.println (" Locale: " Localeen.getDisPlayName ()); system.out.println (fuldateformaten.format (date));}} This example is: Locale: French (FRANCE) VENDREDI 5 Octobre 2001 21 H 05 GMT-04: 00locale: ENGLISH (U Nited State) Friday, October 5, 2001 9:05:54 PM EDT Note This output includes time zone information: GMT-04: 00 and PM EDT. This time zone is captured in the time zone setting of the human system. You can see that the date is displayed in the format of the user's expectations in that area. Let us wait for the concept of time zone
The time zone Timezone class, an instance of the Java.util.Timezone class contains a time zone offset in microseconds in microseconds, and it also processes daylight saving time than Greenwich Time (GMT). To get a list of all supported access, you can use method Timezone.GetavailableIDs, which will return a string array containing all access area IDs. To know more details about the Timezone class, you can see Sun's Web Site. To demonstrate this concept, we will create three time zone objects. The first object will use getDefault to return time zone data from the system clock; the second and third objects will pass into a time zone string ID. See the code in Table C. Table C import java.util.TimeZone; import java.util.Date; import java.text.DateFormat; import java.util.Locale; public class DateExample8 {public static void main (String [] args) {// Get the system time zone.TimeZone timeZoneFL = TimeZone.getDefault (); System.out.println ( "/ n" timeZoneFL.getDisplayName ()); System.out.println ( "rawOffset:" timeZoneFL.getRawOffset ()); System. out.println ( "Uses daylight saving:" timeZoneFL.useDaylightTime ()); TimeZone timeZoneLondon = TimeZone.getTimeZone ( "Europe / London"); System.out.println ( "/ n" timeZoneLondon.getDisplayName ()); System.out.println ( "rawOffset:" timeZoneLondon.getRawOffset ()); System.out.println ( "Uses daylight saving:" timeZoneLondon.useDaylightTime ()); candle imeZone timeZoneParis = TimeZone.getTimeZone ( "Europe / Paris "); System.out.println (" / n " timezone ()); system.out.println (" Rawoffset: " TimezoneParis.GetRawoffset (); system.out.println (" Uses Daylight saving: " TimezoneParis.usedaylighttime ());}} Output is as follows: Eastern Standard Timera wOffset: -18000000Uses daylight saving: trueGMT 00: 00RawOffset: 0Uses daylight saving: trueCentral European Standard TimeRawOffset: 3600000Uses daylight saving: true As you can see, TimeZone objects to our original offset, which is the difference with GMT The number of microseconds, and will also tell us whether this time zone uses Summertime.
With this information, we can continue to combine time zone objects and date formatts together in other time zones and other languages. The internationalization period shows the time zone conversion let us see an example in which combined international display, time zone and date formatted. Table D shows the current complete date and time in a company in Miami and Paris. For the Miami's office, we will show the complete date and time in each office. For the Office of Paris, we will use French to show full current date and time.
Table D import java.util.TimeZone; import java.util.Date; import java.util.Locale; import java.text.DateFormat; public class DateExample9 {public static void main (String [] args) {Locale localeEN = Locale. US; Locale localeFrance = Locale.FRANCE; TimeZone timeZoneMiami = TimeZone.getDefault (); TimeZone timeZoneParis = TimeZone.getTimeZone ( "Europe / Paris"); DateFormat dateFormatter = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL, localeEN); DateFormat dateFormatterParis = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL, localeFrance); Date curDate = new Date (); System.out.println ( "Display for Miami office."); // Print the Miami time zone display name in EnglishSystem.out.println (timeZoneMiami.getDisplayName (localeEN)); // Set the time zone of the dateFormatter to Miami time zone.dateFormatter.setTimeZone (timeZoneMiami); // Print the formatted date.System.out.println (dateFormatter .format (curdate)); // set the time zone of the date formatter to Paris time zone.dateformatter.Settim eZone (timeZoneParis); // Print the Paris time zone display name in English.System.out.println (timeZoneParis.getDisplayName (localeEN)); // Print the Paris time in english.System.out.println (dateFormatter.format ( curDate)); System.out.println ( "/ nDisplay for Paris office."); // Print the Miami time zone display name in FrenchSystem.out.println (timeZoneMiami.getDisplayName (localeFrance)); // Set the timezone of the // dateFormatterParis to Miami time zone.dateFormatterParis.setTimeZone (timeZoneMiami); // Print the formatted date in French destroyed ystem.out.println (dateFormatterParis.format (curDate));. // Set the timezone of the date formatter to Paris time zone.dateformatterParis.SetTimezone (TimezoneParis);
// Print the Paris time zone display name in French.System.out.println (timeZoneParis.getDisplayName (localeFrance)); // Print the Paris time in French.System.out.println (dateFormatterParis.format (curDate));} } The output of this example is: Display for Miami Office. Eastern Standard Timefriday, October 5, 2001 10:28:02 Pm Edtractay, October 6, 2001 4:28:02 AM CESTDISPLAY for PARIS Office. GMT-05: 00vendredi 5 Octobre 2001 22 H 28 GMT-04: 00GMT 01: 00samedi 6 Octobre 2001 04 h 28 GMT 02: 00 Save and extract date data in a SQL database We will use the next class is java.sql.date It is a subclass of java.util.date but it uses a Java Database Connection (JDBC) method. Let's look at a simple list -last_access's Oracle database, which is created with the following SQL: CREATE TABLE LAST_ACCESS (Last_Hit Date); this form has only one record, create: Insert Into Last_access with the following plug VALUES (SYSDATE); Table E demonstrates how to modify and extract the Last_HIT database field.
Table E import java.sql *;. Import java.text.DateFormat; import java.util.Date; public class DateExample10 {public static void main (String [] args) {// Get a full date formatter.DateFormat dateFormatter = DateFormat .getdatetimeInstance (dateformat.full, dateformat.ful); // Get the system date and time.java.util.date utildate = new date (); // convert it to java.sql.datejava.sql.date date = new Java.sql.date (utildate.gettime ()); // Display the date before storing.system.out.println (dateformatter.format (date)); // save the date to the database.setlasthit (Date); / / Get the date from the database.Date dateFromDB = getLastHit (); // Display the date from the database.System.out.println (dateFormatter.format (dateFromDB));} public static void setLastHit (java.sql.Date date ) {Try {// load the class.class.forname ("Oracle.jdbc.driver.Driver.Oracledriver); // Get a connection = drivermanager.getConnection (// Database URL" JDBC: Oracle: Thin: @localhost: 1521: buzz2 "," Web_site ", // username" Web_site "); // PAS swordtry {/ Get a prepared statement fromthe connection // specifying the update SQL.PreparedStatement ps = connection.prepareStatement ( "update LAST_ACCESS set LAST_HIT ="); try {/ set the date letting JDBC to the work of // formatting the SQL appropriately .ps.setdate (1, date); // Execute the update statement.int irowsupdated = ps.executeUpdate (); system.out.println ("Rows Updated:" iRowSupdated; finally {ps.close (); }} Finally {connection.close ();}} catch (}) {system.out.println ("error:" ex.getMessage ());}} public static java.sql.date getlasthit () {Java . Sql.date returndate = null; try {// load the driver class.class.Forname ("