Learning date, date format, date analysis and date calculation

xiaoxiao2021-03-06  62

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) 1970, January 1 00:00:00 This moment or after the number of milliseconds, create a date object, let us see a current date using the system Create a date object and return a long integer. This time is often referred to as a system time for the Java Virtual Machine (JVM) host environment. [Code: 1: ad22b58018] 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 ());}} [/ code: 1: AD22B58018 On Saturday, September 29, 2001, the afternoon is about 6:50, The result of the surface display 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. And this constructor is used inside. The system.currenttimemillis () method is available from the system. So now we already know how to get milliseconds from January 1, 1970. How can we display this date with a user understanding? Here, Java.Text.SIMPLEDATEFORMAT, and its abstract base class java.text.dateFormat gave the field. Second, the custom format of the date data If we want to customize the format of the date data, for example, Saturday - September -29 Day - 2001. The following example shows how to complete this work: [Code: 1: ad22b58018] 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));}} [/ code: 1: AD22B58018] Just pass the format string by means of SimpleDateFormat "

Eee-mmmm-dd-yyyy, we can specify the format you want. You should be able to see, the ASCII character in the format string tells the format function which part of the display date data. Eeee is the week, MMMM is Month, DD is the day, yyy is the year. The number of characters determines how the date is formatted. Pass "EE-MM-DD-YY" will show SAT-09-29-01. Please check the Sun's Web site Get the complete instructions for the date formatting option. III, parsing the text data into a date object assumes that we have a text string containing a formatted date object, and we want to resolve this string and create one from text date data Date object. We will call the SimpleDateFormat class again with the format string "mm-dd-yyyy", 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 for 001736000000. Example: [Code: 1: 10d3268d34] Import Java.Text.SIMPLICT 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 A e expecting. Date Date = bartdateFormat.Parse (DateStringToparse); // Now send the pased date as a long value // to the system output. system.out.println (Date.getTime ());} catch (Exception EX) {System.out.println (ex.getMessage ());}}} [/ code: 1: 10d3268d34] 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. [Code: 1: 10d3268d34] 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 (fuldateformat.format (date));}} [/ code: 1: 10d3268d34] Note We pass two values ​​in each call to getDateTimeInstance. 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 the DateFormat class for constants: short, medium, long, and full. To know getting Time and date formatting processes more methods and options, see the interpretation on Sun Web site. 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 able to format and create a date Object, but how we can set up and get Take a specific part of the date data, such as hours, days, or minutes? How do we add or lose the value in these parts of the date? The answer is to use the Calendar class. As we mentioned earlier, Calendar class The method replaces the method of being saved in the Date class. Suppose you want to set, get, and manipulate a date object, more than one day or a week. In order to demonstrate this process, we will Use specific subclavab java.util.gregoriancalendar. Consider the following example, it calculates the following 10th Friday is No. 13. [Code: 1: 041aeb23d1] 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 of outtar // to the system & s date and time cal.settime (new date ()); system.out.println ("

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

New Post(0)