Java's date processing (reproduced)

zhaozj2021-02-16  167

Summary 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. 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 runtime 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 nowlong = now.getime (); system.out.println ("value is" nowlong);}} When I run this program, I get it 972, 568, 255, 150. Quickly confirm this number, at least in a reasonable range: It is less than 31 years, this value is reasonable relative to the time that I wrote this article on January 1, 1970. 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, using 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 parameter, using the default format getDateInstance () method. 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.

Below is an example of several short, medium, long, and full type: 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 output is as follows : (Default) Today Is Nov 8, 2000 (Short) Today IS 11/8/00 (Medi UM) Today IS NOV 8, 2000 (long) Today Is November 8, 2000 (FULL) Today IS Wednesday, November 8, 2000 The same program, after using the default settings on my computer, change the area to Sweden, Output as follows: (Default) Today IS 2000-NOV-08 (Short) 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 that Sweden's month is not capitalized (although November is still november). There is also the same, Long and Full versions are the same in Swedish, but US 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 ("unable to parse" ds);}}} Create an arbitrary The Parse () method is useful when the date is available. 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. GregorianCalendar class Create a way to use the Gregoriancalendar class representing any date of 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. Pushing in this class 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 represents DECEMBER 17, 1903 (remember, in the short format, 11 represents december) Gregoriancalendar firstflight = New Gregoriancalendar (1903, 11, 17); In the previous section, you learned to convert Date objects to strings . 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 create a representative of current time The instance of the GregorianCalendar class is very useful. You can simply use the Gregoriancalendar constructor that does not have a parameter, like this: Gregoriancalendar thisday = new gregoriancalendar (); an example of an output today's date, using the Gregoriancalendar object: import java.util. *; Transs 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 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' is an important number, using this program to calculate the date of the Philes Fogg from the date of the day after 80 days after October 2, 1872: Import java.util. *; Import java.text. * ; 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 on a date Increase the number of days is a universal operation: Figure 3 days, library can be borrowed 21 days, and stores often need to sell items within 30 days. The following program demonstrates the annual calculation of use: 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 Year Mortgage Amortized On " S);}} add () an important side effect is its changed 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 are turned into 2001 (because the two objects point to the same DATE, and Date has 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

Now, you can easily modify it to handle the time period you need, like the library borrow, video tape lease and mortgage calculation.

First, ReviewDates class shown below: import java.util *; import java.text *; public class ReviewDates {private GregorianCalendar firstDay, oneDay, oneWeek, oneMonth, 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 " ss);

转载请注明原文地址:https://www.9cbs.com/read-7881.html

New Post(0)