[Reproduced] http://blog.9cbs.net/cocia/
Calculate how Java date learning how to create and use the date of use No matter whether you are dealing with financial deals or planning a next step, you have to know how to build, use and display the date in Java. This requires you to simply see the corresponding class API reference: a date you can create 3 related classes. This article tells you what you want to know. (3,000 words) Author: Robert Nielsen Translation: Cocia Lin
Java statistics represent the date of milliseconds from January 1, 1970. That is, for example, January 2, 1970 is 86,400,000 milliseconds after January 1. Similarly, December 31, 1969 was 86,400,000 milliseconds before January 1, 1970. Java's Date class uses the long type record these milliseconds. Because long is a symbol integer, the date can be followed before January 1, 1970. The maximum positive value and maximum negative value indicated by long types can be easily representative of 290,000,000, which is suitable for most people's time requirements. The DATE DATE class can be found in the java.util package, indicating a specified time with a value of a long type. Its useful constructor is DATE (), which creates a object that represents the time of the creation. The getTime () method returns the long value of the DATE object. In the following program, I use the DATE () constructor to create an object that represents the run time, and use the getTime () method to find the number of milliseconds represented by this date:
Import java.util. *;
Public class now {public static void main (String [] args) {date now = new date (); long nolong = now.getime (); system.out.println ("value is" nowlong);}} After running this program, I get 972, 568, 255, 150. Quickly confirm this number, at least in a reasonable range: It is less than 31 years, this value is reasonable to the time of the article on January 1, 1970, is reasonable of. The computer is this millisecond value indication time, people are reluctant to say "I will see you at 996, 321, 998, 34." Fortunately, Java provides a way to convert Date objects to strings, indicating into a traditional form. We discuss the DateFormat class in the next section, which is intuitive to create a date string. One goal of the DateFormat class DateFormat class is to create a string that people can identify. However, because of the difference in language, not all people want to see the dates of strictly identical format. The French prefer to see "25 Decembre 2000,", but Americans are used to see "DECEMBER 25, 2000." So after a DateFormat is created, this object contains the date of display format. If you use the user's computer area to set the default format, you can create a DateFormat object as follows: Use the getDateInstance () method:
DateFormat DF = DateFormat.getdateInstance (); DateFormat class can be found in the Java.Text package. Convert into strings You can use the format () method to convert the Date object as a string. The following sample program illustrates this problem:
Import java.util. *; import java.text. *; public class nowString {public static void main (String [] args) {Date Now = new Date (); DateFormat DF = DateFormat.getdateInstance (); string s = df .format (now); System.out.println ("Today IS" S);}} In the above code, there is no parameters, using the getDateInstance () method using the default format. Java also offers several selection date formats, you can get it using the overloaded getDateInstance (int style). For convenience, DateFormat provides several preset constants where you can use these constant parameters. Here are examples of several short, medium, long, and full types:
Import java.util. *; import java.text. *;
Public class styledemo {public static void main (String [] args) {date now = new date ();
DateFormat df = DateFormat.getDateInstance (); DateFormat df1 = DateFormat.getDateInstance (DateFormat.SHORT); DateFormat df2 = DateFormat.getDateInstance (DateFormat.MEDIUM); DateFormat df3 = DateFormat.getDateInstance (DateFormat.LONG); DateFormat df4 = DateFormat. getDateInstance (DateFormat.Full); string s = df.format (now); string s1 = df1.format (now); string s2 = df2.format (now); string s3 = df3.format (now); string s4 = DF4.FORMAT (now);
System.out.println ("(Default) Today is" S); System.out.Println ("Short) Today Is" S1); System.out.Println ("(Medium) Today IS" S2) System.out.Println ("(long) Today is" S3); system.out.println ("(full) Today is" S4);}} The program is as follows:
(DEFAULT) Today Is Nov 8, 2000 (SHORT) Today IS 11/8/00 (Medium) Today Is Nov 8, 2000 (Long) Today Is November 8, 2000 (FULL) Today Is Wednesday, November 8, 2000 Similar After the program, after using the default settings, the change area is set to Sweden, the output is as follows: (default) Today IS 2000-11-08 (Medium) Today IS 2000- NOV-08 (long) Today Is Den 8 November 2000 (FULL) Today Is Den 8 November 2000 From here, you can see, Sweden's month is not capitalized (although November is still november). Also, long and full version Swedish is the same, but American English is different. In addition, interesting is the Wednesday, Onsdag, Swedish words, is not included in the full date, but English is included. Note that you can use the getDateInstance () method to change the language of the DateFormat instance; however, in the above example, it is done by changing the area setting of the control panel of Windows 98. The regional settings of different places are different, and the results are different. It is good, and there is not enough. Java programmers should understand these. One benefit is that the Java programmer can only write a line of code to display the date, and the same procedure in different parts of the world will have a date format. But this is also a disadvantage that when the programmer wants to display the same format - this also has it, for example, mixed output text and date in the program, if the text is English, we don't want the date format to be other Format, like German or Spanish. If the programmer depends on the date format, the date format will be different depending on the area setting of the computer where the runtime is located. The parsing string passes the PARSE () method, and DateFormat can create a DATE object in a string. This method can throw Parsexception, so you must use the appropriate exception handling technology. The following example creates a DATE object through a string:
Import java.util. *; import java.text. *;
Public class parseexample {public static void main (string [] args) {string DS = "november 1, 2000"; DATEFORMAT DF = DateFormat.getdateInstance (); try {date d = df.Parse (DS);} catch (ParseException e) {system.out.println ("Unable to Parse" DS);}}} is useful when creating an arbitrary date. I will create an arbitrary date through another way. At the same time, you will see how basic date calculations, such as another day after 90 days. You can use the GregorianCalendar class to complete this task. The GregorianCalendar class creates a way to use the GregorianCalendar class for a way to use the GregorianCalendar class, which is included in the java.util package:
Gregoriancalendar (int year, int month, int Date) Tets the month's representation, January is 0, February is 1, and so on, it is December 11. Because most people are accustomed to using words rather than using numbers, this program may be more easy to read, parent Calendar uses constants to represent months: January, February, and more. So, create Wilbur and Orville's date (December 17, 1903), you can use: Gregoriancalendar firstflight = New Gregoriancalendar (1903, Calendar.December, 17); for clear consideration, you should use the front form. However, you should also learn how to read the short format below. The following example also indicates that DECEMBER 17, 1903 (remember, in the short format, 11 means a decEmber)
Gregoriancalendar firstflight = New Gregoriancalendar (1903, 11, 17); In the previous section, you learned to convert the Date object to the string. Here you can do the same thing; but first, you need to convert the GregorianCalendar object to Date. To do this, you can use the getTime () method to inherit from its parent Calendar. The getTime () method returns the corresponding DATE object of GregorianCalendar. You can create a GregorianCalendar object, convert to the DATE object, get and output a process such as the corresponding string. Here is an example:
Import java.util. *; import java.text. *;
PUBLIC CLASS flight {
public static void main (String [] args) {GregorianCalendar firstFlight = new GregorianCalendar (1903, Calendar.DECEMBER, 17); Date d = firstFlight.getTime (); DateFormat df = DateFormat.getDateInstance (); String s = df.format (d); System.out.Println ("First Flight WAS" S);}} Sometimes it is useful to create an instance of the GregorianCalendar class representing the current time. You can simply use the GregorianCalendar constructor without parameters, like this:
Gregoriancalendar thisday = new gregoriancalendar (); an example of output today, using GregorianCalendar object:
Import java.util. *; import java.text. *;
class Today {public static void main (String [] args) {GregorianCalendar thisday = new GregorianCalendar (); Date d = thisday.getTime (); DateFormat df = DateFormat.getDateInstance (); String s = df.format (d); System.out.println ("Today Is" s);}} Note that the Date () constructor and the GregorianCalendar () constructor are very similar: all objects are created, and the conditions are simple, representing today. Date Processing the GregorianCalendar class provides a process of processing dates. A useful method is add (). Using the add () method, you can increase the year, the number, the number of days to the date object. To use the add () method, you must provide the quantity to increase to increase. Some useful fields are DATE, MONTH, YEAR, and WEEK_OF_YEAR. The following program uses the add () method to calculate a date in the next 80 days. In Jules'
public class World {public static void main (String [] args) {GregorianCalendar worldTour = new GregorianCalendar (1872, Calendar.OCTOBER, 2); worldTour.add (GregorianCalendar.DATE, 80); Date d = worldTour.getTime (); DateFormat DF = DateFormat.getdateInstance (); string s = df.format (d); System.out.Println ("80 day trip will end" s);}} This example is imagined, but adds to a date The number of days is a universal operation: DVD can rent 3 days, library can borrow a book 21 days, stores often need to sell items within 30 days. The following program demonstrates the use annual calculation:
Import java.util. *; import java.text. *;
public class Mortgage {public static void main (String [] args) {GregorianCalendar mortgage = new GregorianCalendar (1997, Calendar.MAY, 18); mortgage.add (Calendar.YEAR, 15); Date d = mortgage.getTime (); DateFormat DF = DateFormat.getdateInstance (); string s = df.format (d); system.out.println ("15 years Mortgage Amortized on" s);}} ADD () an important side effect is its change Date. Sometimes it is important to have the original date and the revised date. Unfortunately, you can't create a GregorianCalendar object, set it and the original equivalent (Equal). The reason is that two variables point to the same Date () object address. If the DATE object changes, the two variables point to the changed date object. Instead of this approach, you should create a new object. The following program demonstrates this approach: Import java.util. *; Import java.text. *;
public class ThreeDates {public static void main (String [] args) {GregorianCalendar gc1 = new GregorianCalendar (2000, Calendar.JANUARY, 1); GregorianCalendar gc2 = gc1; GregorianCalendar gc3 = new GregorianCalendar (2000, Calendar.JANUARY, 1); // Three Dates All Equal to January 1, 2000
GC1.Add (Calendar.Year, 1); File: // GC1 and GC2 Are Changed
DateFormat DF = DateFormat.getdateInstance ();
Date D1 = gc1.gettime (); DATE D2 = gc2.gettime (); DATE D3 = GC3.GetTime ();
String S1 = DF.FORMAT (D1); String S2 = DF.FORMAT (D2); String S3 = DF.FORMAT (D3);
System.out.println ("GC1 IS" S1); System.out.Println ("GC2 IS" S2); System.out.Println ("GC3 IS" S3);}}} The program is running, GC1 and GC2 is turned into 2001 (because two objects point to the same date, but Date has been changed). The object GC3 points to a separate date, it is not changed. Calculating a review date In this section, you will see an example in accordance with the real world. This detailed program calculates a specific date. For example, you read this article, you want to remember an impressive knowledge point. If you don't have a photo of memory, you will regularly review these new information, which will help you remember it. About the review system, Kurt Hanks and Gerrel L. Pulsipher discussed in the 5 secrets of their Public Class ReviewDates {Private Gregoriancalendar Firstday, OneQuarter, OneYear; Final Int DateArraysize = 6; ReviewDates (Gregoriancalendar gcdate) {int year = gcdate.get (Gregoriancalendar.Year); int month = gcdate.get (Gregoriancalendar.Month); int Date = gcdate.get (Gregoriancalendar.date); firstDay = new GregorianCalendar (year, month, date); oneDay = new GregorianCalendar (year, month, date); oneWeek = new GregorianCalendar (year, month, date); oneMonth = new GregorianCalendar (year, month, date); oneQuarter = new GregorianCalendar (year, month, date); oneYear = new GregorianCalendar (year, month, date); oneDay.add (GregorianCalendar.DATE, 1); oneWeek.add (GregorianCalendar.DATE, 7); oneMonth.add (GregorianCalendar. Month, 1); OneQuarter.add (Gregoriancalendar.month, 3); ONEYEAR.ADD (Gregoriancalendar.Year, 1); ReviewDates () {this (new gregoriancalendar ()); public void listDates () {DateFormat df = DateFormat.getDateInstance (DateFormat.LONG); Date startDate = firstDay.getTime (); Date date1 = oneDay.getTime (); Date date2 = oneWeek.getTime (); Date date3 = oneMonth. GetTime (); Date Date4 = Onequarter.gettime (); Date Date5 = OneYear.getTime (); String ss = df.format (startdate); string ss1 = df.format (date1); string ss2 = df.format (date2); string ss3 = df.format (date3); string ss4 = df.format (date4); String SS5 = DF.FORMAT (date5); System.out.println ("Start Date IS"; System.out.Println ("Following Review Dates Are:"); System.out.Println (SS1); System.Out.println (SS2); System. Out.println (SS3); System.out.Println (SS4); System.out.println (SS5); System.out.println (); public GregorianCalendar [] getDates () {GregorianCalendar [] memoryDates = new GregorianCalendar [dateArraySize]; memoryDates [0] = firstDay; memoryDates [1] = oneDay; memoryDates [2] = oneWeek; memoryDates [3] = oneMonth; memoryDates [4 ] = onequarter; memorydates [5] = oneYear; returnemorydates;}} public gregoriancalendar getfirstday () {Return this.firstday;} Public Gregoriancalendar getoneday () {returnid.oneday;} Public Gregoriancalendar getOndeek () {Return this.oneweek;} Public Gregoriancalendar getonemonth () {Return this.onemonth;} Public Gregoriancalendar getonequarter () {return this.onequarter;} Public Gregoriancalendar getoneyEAR () {return this.oneyear;}} The following is an example of the review date using the REVIEWDATES class: Import java.util. *; Public class showdates {public static void main (string [] args) {reviewdates rd = new reviewdates (); rd.listdates (); Gregoriancalendar GC = New Gregoriancalendar (2001, Calendar.january, 15); ReviewDates Jan15 = New ReviewDates (GC); Jan15.ListDates ();}} Summary This article describes 3 important classes about date processing: Date, DateFormat, Gregoriancalendar. These classes let you create a date, convert to strings, and calculate the date basic element. Processing the date in Java, this article is just the corner of the iceberg. However, the classes and methods I introduced here are more than just how you learn advanced techniques, these and methods themselves can handle many usual related tasks. Java's time handling ( This article is based on Gregoriancalendar (int year, int month, int limited) First, I should remind each constructor requires the date information (year, month, day) in time information. If you want to say 2:30 p.m. You must point out the date. Similarly, every GregorianCalendar constructor creates a object that uses milliseconds in time. So, if your constructor is only available for the year, month, day parameters, that hours, minutes, seconds and milliseconds will be set to DateFormat and time You can use Static Method GetDateTimeInstance (int DateStyle, int TimeStyle) to create DateFormat Objects to display time and date. This method indicates the date and time format you want. If you prefer to use the default format, you can use getDatetimeInstance () instead of it. You can create a DateFormat object using a static method GetTimeInstance (int TimeStyle) to display the correct time. The following program demonstrates how getDateTimeInstance () and gettimeInstance () work: import java.util *;. import java.text *;. public class Apollo {public static void main (String [] args) {GregorianCalendar liftOffApollo11 = new GregorianCalendar (1969, Calendar.JULY, 16, 9, 32); Date d = liftOffApollo11.getTime (); DateFormat df1 = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM); DateFormat df2 = DateFormat.getTimeInstance (DateFormat.SHORT); String s1 = df1.format (d); String s2 = df2. Format (d); system.out.println (s1); system.out.println (S2);}} On my computer, the above program is shown below: Jul 16, 1969 9:32:00 AM9: 32 AM (Output Differences depending on your area) Calculation interval you may sometimes need to calculate the past time; for example, give you and end time, you want to know the duration of the manufacturing process. A rental company taxes on hours or days, and the calculation time is also useful to them. Similarly, in the financial sector, important payment time is often required. Combine the problem, and human beings calculate time with two methods. You can say that the day has been over to pass 24 hours, or the calendar is turned tomorrow today. We will discuss the two situations we think. Time-segment, situation 1: Strict time in this case, only 24 hours, this day, 60 minutes, this hour is only over, this minute is only in this minute. In this method, 23 hours of time will be considered 0 days. Use this method to calculate the time period, you start from the calculated milliseconds. In order to do this, first transform each date in milliseconds from January 1, 1970. You can subtract the first millisecond value from the second millisecond value. Here is a simple calculation: Import java.util. *; Public class elapsedmillis {public static void main (string [] args) {Gregoriancalendar gc1 = new Gregoriancalendar (1995, 11, 1, 3, 2, 1); Gregoriancalendar gc2 = New Gregoriancalendar (1995, 11, 1, 3, 2, 2); // The Above Two Dates Are One Second Apart Date D1 = GC1.GETTIME (); DATE D2 = GC2.Gettime (); long l1 = D1.Gettime (); long l2 = d2.gettime (); long Difference = L2 - L1; System.out.Println ("ELAPSED MILLISECONDS:" DIFERENCE);}} The above program prints as follows: Elapsed MilliseConds: 1000 This program also brings a little confusion. GREGORIANCALENDAR class getTime () returns a Date object, the GetTime () method of the DATE class returns millisecond value of the long type from January 1, 1970 to this time. Although their method name is the same, the return value is not the same! The following block disconnects to convert milliseconds from seconds: long milliseconds = 1999; long seconds = 1999/1000; this method goes to the fractional part to convert milliseconds to seconds, so 1,999 milliseconds equal to 1 second, 2,000 milliseconds equal to 2 seconds . Calculate larger units - such as days, hours, and minutes - given a time value, you can use the following procedure: 1. Calculate the largest unit, minus the number of seconds of this value 2. Calculate the second largest unit, minus this The number of seconds of the value is 3. Repeat the operation until there is only the second example, if your time is 10,000 seconds, how many hours do you want to know, how many minutes, how many seconds, you start from the largest unit: hours. 10,000 is divided by 3600 (one hour second). Using an integer division, the answer is 2 hours (integer division] to calculate the remaining seconds, 10,000- (3,600 x 2) = 2,800 seconds. So you have 2 hours and 2,800 seconds. 2,800 seconds are converted into minutes, and 2,800 is divided by 60. Using an integer division, the answer is 46.2, 800 - (60 x 46) = 40 seconds. The final answer is 2 hours, 46 minutes, 40 seconds. The following Java program uses the above calculation method: Import java.util. *; public class Elapsed1 {public void calcHMS (int timeInSeconds) {int hours, minutes, seconds; hours = timeInSeconds / 3600; timeInSeconds = timeInSeconds - (hours * 3600); minutes = timeInSeconds / 60; timeInSeconds = timeInSeconds - (minutes * 60) Seconds = timeInseconds; System.out.Println (Hours "Hour (s)" " ") " Seconds " SECOND (S) ");} public static void main (String [] args {ELAPSED1 ELAP = New Elapsed1 (); ELAP.CALCHMS (10000);}} The output is as follows: 2 Hour (s) 46 minute (s) 40 second (s) The above program is even less than an hour, it can be correct Calculation hours. For example, you calculate 1,000 seconds with the above program, output into: 0 Hour (s) 16 minute (s) 40 second (s) to give an example of a real world, the following program calculates that Apollo is flying to the moon to use time : Import java.util. *; Public Class Lunarlanding { Public Long getEletsedSeconds (Gregoriancalendar GC2) {date d1 = gc1.gettime (); Date D2 = gc2.gettime (); long l1 = D1.Gettime (); long l2 = d2.getime (); long difference = Math.abs (L2 - L1); Return Difference / 1000; public void calcHM (long timeInSeconds) {long hours, minutes, seconds; hours = timeInSeconds / 3600; timeInSeconds = timeInSeconds - (hours * 3600); minutes = timeInSeconds / 60; System.out.println (hours "hour (s) " minutes " minute (s) "); public static void main (String [] args) {GregorianCalendar lunarLanding = new GregorianCalendar (1969, Calendar.JULY, 20, 16, 17); GregorianCalendar lunarDeparture = new GregorianCalendar (1969, Calendar.JULY, 21, 13, 54); GregorianCalendar Starteva = New Gregoriancalendar (1969, Calendar.july, 20, 22, 56); Gregoriancalendar Endeva = New Gregoriancalendar (1969, Calendar.july, 21, 1, 9); Lunarlanding apollo = new lunarlanding (); Long EVA = apollo.getelapsedseconds; system.out.print ("EVA DURATION ="); Apollo.calchm (EVA); Long lunarstay = apollo.geting, lunardepartment; system.out.print ("lunar stay ="); apollo.calchm (lunarstay);}} The above program is as follows: EVA DURATION = 2 Hour (s) 13 minute ( S) Lunar stay = 21 Hour (s) 37 Minute (s) So far, our calculated base formula is this: 1 minute = 60 seconds, 1 hour = 60 minutes, 1 day = 24 hours. "1 month =? Day, 1 year =? Day" What should I do? " The number of days in the month is 28, 29, 30, 31; one year can be 365 or 366 days. Therefore, when you try to calculate the month and year of the strict unit, the problem is produced. For example, if you use the average number of days (approximately 30.4375), and calculate the following time laptop: * July 1, 2:00 am to July 31, 10:00 Pm * February 1, 2:00 Am to February 29, 10 : 00 PM The first calculation result is 1 month; the second result is 0 months! So, the month and year in calculating strict units are going to be good. Time period, case 2: Time unit change time unit change is quite simple: If you want to count the number of days, you can simply count the number of changes. For example, if something starts on the 15th, ending on the 17th, after 2 days. (The date is first until 16, then to 17), one step at 3:25 in the afternoon, the end of 4: 10 p.m, last hour, because the hour value has changed once (from 3 to 4). Libraries often use this habitual calculation time. For example, if you pick a book from the library, I can't occupy this book at least 24 hours, I will think that the library will give you a day. Instead, I record the date I borrowed on my account. Date to become next day, I have already set this book for a day, even if I totally less than 24 hours. When the change in units is used to calculate the time period, it usually feels that there is no more than one time unit for time. For example, if 9:00 p.m. I borrowed a library book, I went back at noon next noon, I can calculate this book for a day. However, there is a feeling asking: "1 day and a few hours?" This said that the total will borrow for 15 hours, the answer is 9 hours a day? Therefore, in this article, I will calculate time with a time unit. The time algorithm for unit changes This is how you calculate the time change of the two dates: 1. Make a copy of the two dates. The close () method can make a copy. 2. Use the date copy to set all parts that are less than the time unit to its minimum unit. For example, if the number of days is calculated, then the hour, minute, seconds, and milliseconds are set to 0. In this case, the Clear () method is used to refer to their respective minimum values. 3. Remove the earlier date, add 1 unit you want to calculate, repeat until two dates. The number of you plus 1 is the answer. You can use the Before () and an AFTER () method, they returned to the Boolean value to determine if a date before or after another date. The method of the following class is used to calculate the number of days and the number of months. Import java.util. *; public class elapsedtime { Public int GETDAYS (Gregoriancalendar G1, Gregoriancalendar G2) {INT ELAPSED = 0; Gregoriancalendar GC1, GC2; IF (G2.AFTER (G1)) {GC2 = (Gregoriancalendar) g2.clone (); gc1 = (gregoriancalendar) g1.clone ();} else {GC2 = (GregorianCalendar) g1.clone (); gc1 = (GregorianCalendar g2.clone (); GC1.clear (Calendar.Millisecond); gc1.clear (Calendar.Second); gc1.clear (Calendar.minute); gc1.hur (calendar.Hour_of_day); gc2.clear (Calendar.MilliseCond); gc2.clear (Calendar.Second); gc2.clear (Calendar.minute); gc2.cle (calendar.Hour_of_day); While (GC1.Before (GC2)) {gc1.add (calendar.date, 1); ELAPSED ;} Return Elapsed;} Public int GETMONTHS (Gregoriancalendar G1, Gregoriancalendar G2) {INT ELAPSED = 0; Gregoriancalendar GC1, GC2; IF (G2.AFTER (G1)) {GC2 = (Gregoriancalendar) g2.clone (); gc1 = (gregoriancalendar) g1.clone ();} else {GC2 = (GregorianCalendar) g1.clone (); gc1 = (GregorianCalendar g2.clone (); gc1.clear (Calendar.MilliseCond); gc1.clear (Calendar.Second); gc1.clear (Calendar.minute); gc1.cle (calendar.Hour_of_day); gc1.clear (Calendar.date); GC2.Clear (Calendar.Millisecond); gc2.clear (Calendar.Second); gc2.clear (Calendar.minute); gc2.cle (calendar.Hour_of_day); gc2.clear (Calendar.date); While (GC1.Before (GC2)) {gc1.add (Calendar.month, 1); ELAPSED ;} Return Elapsed;}} You can add additional methods in the above class to handle hours and minutes. Similarly, the algorithm of the calculation time period can be more efficient, especially the time is very different. However, as an introduction purpose, this algorithm has a short and simple advantage. The following example uses the ELAPSEDTIME class to calculate an angel between two dates, and then the number of months: import java.util. *; public class Example {public static void main (String [] args) {GregorianCalendar gc1 = new GregorianCalendar (2001, Calendar.DECEMBER, 30); GregorianCalendar gc2 = new GregorianCalendar (2002, Calendar.FEBRUARY, 1); ELAPSEDTIME ET = New Elapsedtime (); int days = et.getdays (GC1, GC2); int MONTHS = Et.GeTMONTHS (GC1, GC2); System.out.println ("Days =" days); System.out.println ("Months =" MONTHS);}} When calculating, the above program may be useful, for example, the nearest flight. It shows the output below: DAYS = 33months = 2 (OK, there is a bit exaggerated about flights; this day number algorithm is ideal for applications like libraries, what do you see how she should be cautious when working? : You see the time of the time period, you are very careful and careful consideration. This article describes two ideas of usually calculating the time period, but the calculation method of the time period that people can think of is only limited by human imagination. So, when writing a Java program, it is sure that your accuracy can satisfy the people of these programs since the use. Similarly, the thorough test procedure is not important for the process of processing time. Summary This article is based on how the Java time calculation describes how to use GregorianCalendar and DateFormat class to process time issues. You have seen two ways to think about time period issues and two corresponding ways to use Java to handle time problems. The information provided here is found to provide you with a powerful tool for processing time problems in Java.