Learning date, date format, date analysis and date calculation

xiaoxiao2021-03-06  38

How to get three ways

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; // read the system in three different ways datetime public class JavaReadSysTime {public static void main (String [] args) {// SimpleDateFormat Date String Stryearmonth = New SimpleDateFormat ("YY-MM-DD HH: MM: SS") .format (new java.util.date ()) .tostring (); System.out.println ("Current Date & Time = " stryearmonth;

// Calendar Calendar Cal = Calendar.GetInstance (); int nyear = Cal.get (Calendar.Year); int nmonth = Cal.get (Calendar.Mont) 1; int NDATE = Cal.get (Calendar.date); System.out.println ("The current date is:" NYEAR "year" nMonth "month" NDATE "day");

INT nDAY_OF_WEEK = CAL.GET (Calendar.day_of_week); System.out.Println ("Today is the" Today " NDAY_WEEK " Day ");

INT NWEEK_OF_MONTH = CAL.GET (Calendar.Week_OF_MONTH); System.out.println ("This week is the first month nweek_of_month " week ");

INT NHOUR = CAL.GET (Calendar.Hour); int nminute = Cal.Get (Calendar.minute); int nsecond = Cal.get (Calendar.Second); int nmillisecond = Cal.get (Calendar.MilliseCond); System. Out.println ("Current time is:" NMINUTE "分" NSecond "Second" NMilliseCond "Mix");

// Date - Date Date = New Date (); int NY = Date.GetyEar () 1900; int NM = Date.getMonth () 1; int Nd = Date.getdate (); system. Out.println ("The current date is:" NY "year" nm "month" ND "day");}}

-------------------------------------------------- ----------------------------------------- Java language Calendar (Calendar) Date, and DateFormat form a basic but very important part of the Java standard. Date is a key part of business logic. All developers should be able to calculate future dates, custom dates. The format is displayed, and the text data is parsed into a date object. We wrote two articles, this is the first one, we will probably learn the date, date format, date of resolution and date. We will discuss the following: 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. [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, about 6:50 in the afternoon, the result of the above example 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 uses the System.CurrentTimeMillis () method to get a date 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 abstraction The base class java.text.dateFormat 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 working: [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 -YYYYYY "); Date Date = new date (); system.out.println (bartdateformat.format (date));

}} [/ code: 1: AD22B58018] As long as the format string "EEE-MMMM-DD-YYY" is passed by the constructor of SimpleDateFormat, we can specify the format you want. You should be able to see, the format string ASCII character tells the formatting function which part of the date data is displayed. Eeee is the week, mmmm is the month, DD is the day, YYY is the year. The number of characters determines how the date is formatted. "EE-MM -dd-yy "will display SAT-09-29-01. Please check the full instruction of Sun's Web Site Get date formatting options .. Third, parsing 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 once again call the SimpleDateFormat class in the format string "MM-DD-YYYY", but this time we use Format is analyzed instead of generating a text date data. Our example is displayed below, the text string "9-29-2001" and create a date object having a value of 001736000000. Example: [Code: 1: 10D3268D34 ] import java.text.SimpleDateFormat; 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);. // 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 ());}}} [/ code: 1: 10d3268d34] 5. Since we can generate and resolve the use of standard date formatting Customized date format, let us 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 example below, We have obtained four built-in date formatting processes. 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 and get specific parts 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 the method of being saved in the DATE class. Suppose you Want to set, get, and manipulate a date object, more than one month or a week. In order to demonstrate this process, we will use the specific subclava.util.gregoriancalendar. Consider the following example, It calculates the top 13, the top 13 is 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 our calendar // to the system & s date and Time Cal.Settime (new 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 friendy 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 ());}}}} [/ code: 1: 1: 041aeb23d1] In this example, we made interesting functions calls: cal.set (Gregoriancalendar.day_of_week, gregoriancalendar.friday; and: cal.add (Gregoriancalendar.day_of_month, 7); SET method allows us to make weeks Which day in this day is to adjust our time to Friday. Note that we use constant day_of_week and yDAY to enhance the readability of the code. The Add method allows us to add a value on the date. Run all the complexity The calculation is automatically handled by this method. The output of our example is: System Date: Saturday, September 29, 2001 When we set it into Friday: Friday, September 28, 2001 Friday, September 13, 2002 Friday, December 13, 2002 Friday, June 13, 2003 Fri Day, February 13, 2004 Friday, August 13, 2004 Friday, May 13, 2005 Friday, January 13, 2006 Friday, October 13, 2006 Friday, April 13, 2007 Friday, July 13, 2007 Friday, June 13, 2008 -------------------------------------------------- ----- Date of winning this week: Calendar Cal = Calendar.GetInstance (); int indexofweek = CAL.GET (Calendar.day_of_week); // Calendar.day_of_week: Sunday = 1, Saturday = 7 C .get (Calendar.day_of_Week) {CAL.Add (Calendar.date, -6);} else {Cal.Add (Calendar.date, 2 - IndexOfweek);

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; // read the system in three different ways datetime public class JavaReadSysTime {public static void main (String [] args) {// SimpleDateFormat Date String Stryearmonth = New SimpleDateFormat ("YY-MM-DD HH: MM: SS") .format (new java.util.date ()) .tostring (); System.out.println ("Current Date & Time = " stryearmonth); // Calendar Calendar Cal = Calendar.GetInstance (); int nyear = Cal.get (Calendar.Year); int nMonth = Cal.get (Calendar.mont) 1; int NDATE = Cal.Get Calendar.date); System.out.println ("The current date is:" Nyear "year" nmonth "month" NDATE "day");

INT nDAY_OF_WEEK = CAL.GET (Calendar.day_of_week); System.out.Println ("Today is the" Today " NDAY_WEEK " Day ");

INT NWEEK_OF_MONTH = CAL.GET (Calendar.Week_OF_MONTH); System.out.println ("This week is the first month nweek_of_month " week ");

INT NHOUR = CAL.GET (Calendar.Hour); int nminute = Cal.Get (Calendar.minute); int nsecond = Cal.get (Calendar.Second); int nmillisecond = Cal.get (Calendar.MilliseCond); System. Out.println ("Current time is:" NMINUTE "分" NSecond "Second" NMilliseCond "Mix");

// Date - Date Date = New Date (); int NY = Date.GetyEar () 1900; int NM = Date.getMonth () 1; int Nd = Date.getdate (); system. Out.println ("The current date is:" NY "year" nm "month" ND "day");}}

-------------------------------------------------- ----------------------------------------- Java language Calendar (Calendar) Date, and DateFormat form a basic but very important part of the Java standard. Date is a key part of business logic. All developers should be able to calculate future dates, custom dates. The format is displayed, and the text data is parsed into a date object. We wrote two articles, this is the first one, we will probably learn the date, date format, date of resolution and date. We will discuss the following: 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. [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, about 6:50 in the afternoon, the result of the above example 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 uses the System.CurrentTimeMillis () method to get a date 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 abstraction The base class java.text.dateFormat 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 working: [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 -YYYYYY "); Date Date = new date (); system.out.println (bartdateformat.format (date));

}} [/ code: 1: AD22B58018] As long as the format string "EEE-MMMM-DD-YYY" is passed by the constructor of SimpleDateFormat, we can specify the format you want. You should be able to see, the format string ASCII character tells the formatting function which part of the date data is displayed. Eeee is the week, mmmm is the month, DD is the day, YYY is the year. The number of characters determines how the date is formatted. "EE-MM -dd-yy "will display SAT-09-29-01. Please check the full instruction of Sun's Web Site Get date formatting options .. Third, parsing 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 once again call the SimpleDateFormat class in the format string "MM-DD-YYYY", but this time we use Format is analyzed instead of generating a text date data. Our example is displayed below, the text string "9-29-2001" and create a date object having a value of 001736000000. Example: [Code: 1: 10D3268D34 ] import java.text.SimpleDateFormat; 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);. // 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 ());}}} [/ code: 1: 10d3268d34] 5. Since we can generate and resolve the use of standard date formatting Customized date format, let us 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 example below, We have obtained four built-in date formatting processes. 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 and get specific parts 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 the method of being saved in the DATE class. Suppose you Want to set, get, and manipulate a date object, more than one month or a week. In order to demonstrate this process, we will use the specific subclava.util.gregoriancalendar. Consider the following example, It calculates the top 13, the top 13 is 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 our calendar // to the system & s date and Time Cal.Settime (new 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 friendy 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 ());}}}} [/ code: 1: 1: 041aeb23d1] In this example, we made interesting functions calls: cal.set (Gregoriancalendar.day_of_week, gregoriancalendar.friday; and: cal.add (Gregoriancalendar.day_of_month, 7); SET method allows us to make weeks Which day in this day is to adjust our time to Friday. Note that we use constant day_of_week and yDAY to enhance the readability of the code. The Add method allows us to add a value on the date. Run all the complexity The calculation is automatically handled by this method. The output of our example is: System Date: Saturday, September 29, 2001 When we set it into Friday: Friday, September 28, 2001 Friday, September 13, 2002 Friday, December 13, 2002 Friday, June 13, 2003 Fri Day, February 13, 2004 Friday, August 13, 2004 Friday, May 13, 2005 Friday, January 13, 2006 Friday, October 13, 2006 Friday, April 13, 2007 Friday, July 13, 2007 Friday, June 13, 2008 -------------------------------------------------- ----- Date of winning this week: Calendar Cal = Calendar.GetInstance (); int indexofweek = CAL.GET (Calendar.day_of_week); // Calendar.day_of_week: Sunday = 1, Saturday = 7 C .get (Calendar.day_of_week) {CAL.Add (Calendar.date, -6);} else {Cal.Add (Calendar.date, 2 - IndexOfweek);} system.out.println "Now:" Cal.getTime ());

SimpleDateFormat DF = New SimpleDateFormat ("YYYY-MM-DD"); string snow = df.format (cal.gettime ()); system.out.println ("Format Now:" snow); if it is just a simple date difference Differently D. GetTime () to remove 3600000 * 24 is the fastest, but for some Locale, the date calculation before the 16th century, pay attention to some issues when converting into Calendar. In most countries, Julian Calendar was replaced by Gregorian Calendar on October 15, 1582, because Julian Calendar and the LEAP Year of the Gregor Calenda (a total of more than 400 years), which in order to smoothly from Julian Calendar Transition to the Gregor, followed on October 4, 1582, which is directly on October 15, 1582, and the intermediate date is gone! If you use Locale happened to some countries that have been relatively late in the Gregor, for example, in Belgium, the public calendar is used in 1584, which disappears will not be the same.

Of course, if your program won't operate such an abnormal date, you don't have to care about too many localers, and Gregoriancalendar is so complex.

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

New Post(0)