-------------------------------------------------- ------------------------------
Http://www.chinaunix.net
Author: echoguo Posted: basic but very important part 2004-01-15 12:46:25 Java language Calendar (calendar), Date (date), and DateFormat (Date Format) form a Java standard date is Commercial logic calculates a key part. All developers should be able to calculate future dates, custom date display formats, and analyze the text data into date objects. We wrote two articles, this is the first, we will Probably the date, date format, date analysis and date calculation. We will discuss the following categories: 1, specific classes (and abstract classes) java.util.date 2, abstract class java.text.dateFormat and it A concrete subclass, java.text.SIMPLEDATEFORMAT 3, an abstract class java.util.calendar and a specific subclass, Java.util.gregoriancalendar specific classes can be instantiated, but abstract classes are not. You must first implement abstraction A specific subclass of the class. The DATE class began evolving from the Java Development Pack (JDK) 1.0, which only contains several ways to get or set a date data, such as month, day, and year. These The method is now criticized and has been transferred to the Calendar class, and we will discuss it in this article. This improvement is to better handle the internationalization of date data. Like the JDK 1.1, Date The class is actually just a parcel class, which contains a long integer data, indicating that from GMT (GMT time) 1970, January 1 00:00:00, or after the number of milliseconds 1. Creating a date object allows us to see a date object using the current date and time of the system and return a long integer. This time is often referred to as a system time of the Java Virtual Machine (JVM) host environment. IMPORT Java.util.date; public class dateexample1 {public static void main (string [] args) {// get the system date / time date date = new date (); system.out.println (Date.getTime ()); }} On Saturday, September 29, 2001, approximately 6:50 in the afternoon, the above example is 100 on the system output device is 100 1803809710. In this example, it is worth noting that we use the Date constructor to create a date object, this constructor does not accept any parameters. This constructor uses the System.CurrentTimeMillis () method internally to obtain the date from the system. So, now we already know how to get milliseconds since January 1, 1970. How can we display this date with a format that users understand? Here, Java.Text.SIMPLEDATEFORMAT and it Abstract base class Java.Text.dateFormat is derived. Second, the custom format of date data If we want to customize the format of date data, for example, Saturday - September-2001. The following example shows how to complete this job: 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, parsing text data into date object assumes that 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 be format again The string "MM-DD-YYYY" calls the SimpleDateFormat class, but this time, we use formatted to resolve instead of generating a text date data. Our example, displayed below, will resolve text string "9-29-2001 "" And create a date object having a value of 001736000000. Example: import java.text.SIMPLICTEFORMAT; import java.util.date; public class dateexample3 {public static void main (string [] args) {// create a date formatter 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. // We Have to Perform The Parse Method In A // Try-Catch Construct In Case DateStringtopivalSe // Does Not Contain A Date In The Format We are expecting. Date Date = bartdateFormat.Parse (datest Ringtoparse); // now send the pased date as a long value // to the system outprint. System.out.println (Date.getTime ());} catch (exception ex) {system.out.println (ex.getMessage ();}}} 5. Since we can generate and analyze custom date formats, let's take a look at how to use the built-in formatting process. Method DateFormat.getDateTimeInstance () Let us get a variety of different methods to obtain standard date formatting processes. In the following example, we have obtained four built-in date formatting procedures. 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; system.out.println (longdateformat.format (date)); system.out.println (fuldateformat.format (date)) }} Note We pass two values in each call to getDateTimeInstance. The first parameter is a date style, and the second parameter is a time style. They are all basic data type Int (integer). Consideration To readability, we use the constants provided by the DateFormat class: Short, Medium, long, and full. To know more methods and options for getting time and date formatting procedures, see the interpretation of Sun Web Sites. When running our example, it will output the following to the standard output device: 9/29/01 8:44 PM Sep 29, 2001 8:44:45 Pm September 29, 2001 8:44:45 PM Edt Saturday , September 29, 2001 8:44:45 PM EDT 6, Calendar Class We now have formatted and created a date object, but how can we set and get specific parts of date data, such as hours, days, or Minute? How do we add or subtract at the date of the date? The answer is to use the Calendar class. As we mentioned earlier, the method in the Calendar class replaces the method of being saved in the DATE class. Suppose You want to set up, get, and manipulate the various parts of a date object, One month or a week day. In order to demonstrate this process, we will use the specific subclavab java.util.gregoriancalendar. Consider the following example, it calculates the following 10th Friday is 13th. 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.FULL); // Create our Gregorian Calendar. Gregoriancalendar Cal = new gregoriancalendar (); // set the date and time system & s date and time cal.settime (new date ()); system.out.println ("System Date:" DateFormat.Format (Cal.getTime ()))); // set the day of week to y Friday Cal.Set (Gregoriancalendar.day_of_week, gregoriancalendar.friday; system.out.println ("