Java language time date study

xiaoxiao2021-03-06  43

1, specific class (and abstract class relative) 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 For specific subclasses, Java.util.gregoriancalendar specific classes can be instantiated, but abstract classes are not. You must first implement a specific subclass of abstract classes. Date class began evolving from Java Development Kit (JDK) 1.0, then it Only a number of methods for acquiring or set 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 this article. It. This improvement is designed to better handle the internationalization 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) 1970, January 1 00:00:00 This moment or after the number of milliseconds, create a date object, let us see a date object with the current date and time of the system and Returns a simple example of a long integer. This time is often referred to as a system time for 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, it is about 6:50 in the afternoon, above The result is shown on the system output device is 1001803809710. 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 is internally used internally. The CurrentTimeMillis () method comes from the system to obtain a date. So, now we already know how to get milliseconds from January 1, 1970. How can we display this date with a user understanding? Here the class java.text.SIMPLEDATEFORMAT and its abstract base class java.text.d Ateformat is derived. Second, the custom format of date data 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 you pass the format string "EEE-MMMM-DD-YYYY" by the constructor of SimpleDateFormat, we can specify the format you want. You should be See, the ASCII character in the format string tells the formatting function which part of the date data is displayed. Eeee is the week, MMMM is the month, and the DD is the day. YYY is the year. The number of characters determines how the date is formatted Transfer "EE-MM-DD-YY" will display SAT-09-29-01. Please check the full instructions of Sun's Web Site Get date formatting options .. Third, resolve text data into date object assumes that we have A text string contains a formatted date object, and we want to resolve this string and create a date object from text date data. We will re-format string "mm-dd-yyyy"

Call the SimpleDateFormat class, but this time, we use formatted resolution rather than generating a text date data. Our example, display the text string "9-29-2001" and create a value of 001736000000 Example: import java.text.SIMPORPATEFORMAT; 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 the text . version of the date // We have to perform the parse method in a // try-catch construct in case dateStringToParse // does not contain a date in the format we are expecting Date date = bartDateFormat.parse (dateStringToParse);. / / Count.Println;} catch (except.Println); }}} 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.getDatetimeInstanc e () allows us to obtain standard date formatting procedures with several different methods. In the following example, we have obtained four built-in date formatting procedures. They include a short, medium, long and date format full 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; system.out.println (fuldateformat.format (date));}} Note We are on getDatetimeInstance, SYSTEM.OUT.FORMAT (DATE);}} Two values ​​are passed in each call. The first parameter is the date style, and the second parameter is a time style. They are all basic data type int (integer). Considering readability, we use DateFormat Conflicts provided: Short, Medium, Long, and Full. To know more methods and options for getting time and date formatting procedures, see the interpretation on Sun Web site. When running our example, it The following content will be output 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, we can now format and create a date object, but how can we set and get a specific part of date data, such as hours, days, or minutes? How do we do this at the date? Some additions or subtracts the value? The answer is to use the Calendar class. As we mentioned earlier, the method in the Calendar class replaces the way to be spit in the DATE class. Suppose you want to set, get, and manipulate One day of a date object, one month or one day or a week. In order to demonstrate this process, we will use the specific subclava.util.gregoriancalendar. Consider the following example, it calculates the tenth of the following Friday is No. 13 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 (DateFo Rmat.full); // Create Our Gregorian Calendar. Gregoriancalendar Cal = New Gregoriancalendar (); // set the date and time @ 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 "After setting day of week to y Friday:" DateFormat.Format (Cal.getTime ())); int friday13counter = 0; while (Friday13counter <= 10) {/ g to the next friendy by adding 7 days. CAL. Add (Gregoriancalendar.day_of_month, 7);

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

New Post(0)