The Java language Calendar (Calendar), Date (date), and DateFormat form a basic but very important part of the Java standard. Date is a critical part of business logic. All developers should be able to calculate Future date, custom date display format, and analyze the text data into date objects. We wrote two articles, this is the first, we will probably learn the date, date format, date analysis and date. We will discuss the following categories: 1, specific classes (and abstract classes) java.util.date 2, abstract class java.text.dateFormat and one of its specific subclasses, Java.Text.SIMPLEDATEFORMAT 3, abstract class Java. Util.Calendar and one of its specific subclasses, Java.util.gregoriancalendar specific classes can be instantiated, but abstract classes can't. You must first implement a specific subclass of abstract classes. Date class from Java Development Pack (JDK) 1.0 begins to evolve, then it only contains several methods of acquiring or sets a date data, such as month, day, and year. These methods are now criticized and have been transferred to the Calendar class. We will further discuss it in this article. This improvement is to better handle the international format of date data. Like JDK 1.1, the Date class is actually just a parcel class, which contains a long integer Data, indicating that from GMT (GMT time) in 1970, January 1 00:00:00 This moment or after the number of milliseconds experienced later. First, create a date object R let us see the current use of the system Date and time create a date object and return a long integer simple example. This time is usually called the Java Virtual Machine (JVM) Host Environment System Time. IMPORT JAVA.UTIL.DATE; PUBLIC CLASS DATEXAMPLE1 {Public Static Void Main String [] args) {// get the system date / time date date = new date (); system.out.println (Date.getTime ());}} On September 29, 2001, it is about 6:50, the above example is shown on the system output device is 1001803809710. In this example, it is worth noting that we make Create a date object with the Date constructor, this constructor does not accept any parameters. The constructor uses the System.CurrentTimeMillis () method internally to obtain a date from the system. So, now we already know how to get from 1970 The number of millisecities that have been experienced on January 1. How can we display this date with a user understanding? Here the class java.text.SIMPLEDATEFORMAT and its abstract base class java.text.dateFormat is derived The use of the field. Second, the custom format of the date data is, if we want to customize the format of the date data, for example, Saturday - September-2001. The following example shows how to complete this work: Import Java.Text.SIMPLEDATEFORMAT; Import Java .util.Date; public class DateExample2 {public static void main (String [] args) {SimpleDateFormat bartDateFormat = new SimpleDateFormat ( "EEEE-MMMM-dd-yyyy"); Date date = new Date (); System.out.println (BartdateFormat.Format (Date));}} As long as the format string "eEe-mmmm-dd-yyyy" is passed by the constructor of SimpleDateFormat
We can specify the format you want. You should be able to see, the ASCII character in the format string tells the formatting function which part of the date data is displayed. Eeee is the month, the mmmm is the month, DD is the day, yyyy is Year. The number of characters determines how the date is formatted. Pass "EE-MM-DD-YY" will display SAT-09-29-01. Please check the completeness of the Sun's Web site acquisition date formatting option. Indicates. III, analyze the text data into date object R Suppose we have a text string containing a formatted date object, and we want to resolve this string and create a date object from text date data. We will once again Format string "MM-DD-YYYY" calls SimpleDateFormat class, but this time, we use formatted to resolve instead of generating a text date data. Our example, displayed below, will resolve text strings "9-29- "" "" "" "" " that can parse dates of // the form MM-dd-yyyy SimpleDateFormat bartDateFormat = new SimpleDateFormat ( "MM-dd-yyyy");. // Create a string containing a text date to be parsed String dateStringToParse = "9-29. -2001 "; Try {// Parse the text version of the date. // we have to performtoparse //@ not contain a date in the format we are expecting Date date = bartDateFormat.parse (dateStringToParse);. // Now send the parsed date as a long value // to the system output System.out.println (date.getTime (). );} Catch (exception ex) {system.out.println (ex.getMessage ());}}} 5. Since we can generate and analyze custom date formats, let us come Take a look at how to use the built-in formatting process. Method DateFormat.getDateTimeInstance () allows us to obtain standard date formatting procedures with several different methods. In the following example, we have obtained four built-in dates Formatting process. They include a short, medium, long, and complete date format. IMPORT JAVA.TEXT.DATEFORMAT; import java.util.date; public class dateexample4 {public static void main (String [] args {Date Date = New Date ();
DateFormat shortDateFormat = DateFormat.getDateTimeInstance (DateFormat.SHORT, DateFormat.SHORT); DateFormat mediumDateFormat = DateFormat.getDateTimeInstance (DateFormat.MEDIUM, DateFormat.MEDIUM); DateFormat longDateFormat = DateFormat.getDateTimeInstance (DateFormat.LONG, DateFormat.LONG); DateFormat fullDateFormat = DateFormat.getDateTimeInstance (DateFormat.FULL, DateFormat.FULL); System.out.println (shortDateFormat.format (date)); System.out.println (mediumDateFormat.format (date)); System.out.println (longDateFormat. Format (Date)); system.out.println (fulldateformat.format (date));}} Note We pass two values in each call to getDatetimeInstance. The first parameter is the date style, and the second The parameters are time-style. They are all basic data type int (integer). Considering readability, we use the constant for DateFormat classes: Short, Medium, Long, and Full. To know the time and date formatting More methods and options for the process, please see the interpretation on the Sun Web site. When running our example, it will output the following to the standard output device: 9/29/01 8:44 PMSEP 29, 2001 8:44:45 Pmseptember 29, 2001 8:44:45 PM Edtsaturday, September 29, 2001 8:44:45 PM EDT 6. Calendar class R We now be able to format and create a date object, but how we can set and get a specific part of date data , Such as hours, days, or minutes? How do we add or subtract on these parts of the date? The answer is to use the Calendar class. As we mentioned earlier, the method in the Calendar class replaces Date. The method of being spached in the class. Suppose you want to set, get, and manipulate the various parts of a date object, one month or one day or a week. In order to demonstrate this process, we will use specific subclavab Java It considers the following example, it calculates the top 13th of the following 10th. Import java.util.gregoriancalendar; import java.util.date; import java.text.dateFormat; public class dateexample5 {public static Void main (string [] args) {dateformat dateformat = DateFormat.GetdateInstance (DateFormat.ff); // Create Our Gregorian Calendar. Gregoriancalendar Cal = New Gregoriancalendar ();
// set the date and time of @ Date ()); system.out.println ("System Date:" DateFormat.Format ()) ); // Set the day of week to FRIDAY cal.set (GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.FRIDAY); System.out.println ( "After Setting Day of Week to Friday:" dateFormat.format (cal.getTime () )); Int friday13counter = 0; while (Friday13counter <= 10) {/ go to the next friend, by adding 7 days. Cal.Add (Gregoriancalendar.day_of_month, 7); // if the day of month is 13 We Have // Another Friday THE 13th. If (Cal.get (GregorianCalendar.day_of_month) == 13) {Friday13Counter ; System.Out.println (DateFormat.Format ());}}}} in this example We made interesting functions calls: cal.set (Gregoriancalendar.day_of_week, gregoriancalendar.friday); and: Cal.Add (GregorianCalendar. Day_of_month, 7);