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 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 of its specific subclasses, Java.util.gregoriancalendar
The specific class can be instantiated, but the abstraction category is not. You must first implement a specific subclass of an abstract class.
The DATE class began evolving from the Java Development Kit (JDK) 1.0, which only contains several ways to obtain or set a date data, such as month, day, and year. These methods are now criticized and have already It was transferred to the Calendar class, and we will discuss it in this article. This improvement is intended to better handle the internationalization of date data. Like the JDK 1.1, the Date class is actually just a parcel class. It contains a long integer data, indicating that from GMT (GMT time) in 1970, January 1 00:00:00, or after the number of milliseconds experienced.
First, create a date object
Let us see a simple example of 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.
[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 above example is shown on the system output device 1001803809710. In this example, it is worth noting that we use the Date constructor to create a Date objects, this constructor does not accept any parameters. This constructor uses the System.CurrentTimeMillis () method internally to obtain a 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 will be derived.
Second, the custom format of date data
If we want to customize the format of the date data, for example, Saturday - September - 29 - 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]
As long as we pass the format string "EEE-MMMM-DD-YYYY" 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 format function below the date of display Which part of the data. Eeee is the week, MMMM is the month, DD is the day, YYYY is the 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 complete instructions for Sun's Web Site Get Date Formatting Options .. Third, parsing text data into date objects
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 call again to format string "mm-dd-yyyy" 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-2001" and create 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 pased.
String DateStringToparse = "9-29-2001";
Try {
// Parse The text Version of the date.
// we have to perform the paramedion in a Parse Method in A
// try-catch construct in case dateStringtopival
// does not contain a date in the format we are evting.
Date Date = BartdateFormat.Parse (dateStringTopival);
// 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]
V. Use of standard date formatting process
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 () allows us to obtain standard date formatting processes with several different ways In the following example, we got four built-in date formatting procedures. 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 a date style, and the second parameter is a time style. They are all basic data type Int (integer). Considering readable Sex, 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 are now able to format and create a date object, but how can we set and get specific parts of date data, such as hours, days, or minutes? How do we add or subtract values in these parts? • The answer is to use the Calendar class. As 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 10th Friday below.
[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 ot calendar
// to the system & s date and time
Cal.SetTime (new date ());
System.out.println ("System Date:" DateFormat.Format (Cal.getTime ());
// 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
// annother friday the 13th.
IF (Cal.get (Gregoriancalendar.day_of_month) == 13) {
Friday13counter ;
System.out.println (DateFormat.format (Cal.getTime ()));
}
}
}
}
[/ Code: 1: 041aeb23d1]
In this example we made interesting functions:
Cal.set (Gregoriancalendar.day_of_week,
Gregoriancalendar.friday);
with:
Cal.Add (Gregoriancalendar.day_of_month, 7);
The SET method allows us to adjust our time to Friday. Note that we use constant day_of_week and y, which uses constants to enhance the readability of the code. Add method allows us to date Adding a value. All complex calculations of the annual years are automatically handled by this method.
The output of our example is:
System Date: Saturday, September 29, 2001
When we set it into Friday, we will become: Friday, September 28, 2001
Friday, September 13, 2002
Friday, December 13, 2002
Friday, June 13, 2003
Friday, 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
Seven, time to master in your hand
With these Date and Calendar classes, you should be able to create many methods using java.util.date, java.text.SIMpleDateFormat, and Java.util.gregorianCalendar.
In the following article, we will discuss the use of higher DATE and Calendar classes, including time zone and international format. We will also examine two date classes java.sql.date and java.util.date the difference.