Print the current moment of yesterday: import Java.util.Calendar;
/** */ /**
* Print the current moment of yesterday
* @author Administrator
*
*/
public class yesterdaycurrent ... {
/** *//**
* @param args
*/
public static void Main (string[] args) ... {
TODO auto-generated Method Stub
Calendar cal=calendar.getinstance ();
Cal.add (Calendar.date,-1);
System.out.println (Cal.gettime ());
}
}
The calendar (Calendar) of the Java language, date (date), and DateFormat (date format) Form a basic but very important part of the Java standard. The date is a key part of the business logic calculation. All developers should be able to calculate future dates, customize the display format for dates, and parse text data into date objects. We have written two articles, this is the first, we will approximate the study date, date format, date parsing and date calculation.
We'll discuss the following classes:
Specific classes (as opposed to abstract classes) java.util.Date
Abstract class Java.text.DateFormat and a specific subclass of it, Java.text.SimpleDateFormat
Abstract class Java.util.Calendar and a specific subclass of it, Java.util.GregorianCalendar
The concrete class can be instantiated, but the abstract class cannot. You must first implement a specific subclass of the abstract class.
The date class evolved from Java Development Kit (JDK) 1.0, when it contained only a few ways to get or set the various parts of a date's data, such as month, day, and year. These methods have now been criticized and moved to the Calendar class, which we will discuss further in this article. This improvement is designed to better handle the internationalized format of date data. As in JDK 1.1, the Date class is actually just a wrapping class that contains a long integer data that represents the number of milliseconds from GMT (Greenwich Mean Time) 1970, January 1 00:00:00 or later.
Create a Date Object
Let's look at a simple example of using the system's current date and time to create a Date object and return a long integer. This time is often referred to as the system time for the Java Virtual Machine (JVM) host environment.
The following is a reference fragment: 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 ()); } } |
In Saturday, September 29, 2001, the afternoon is about 6:50, and the example shown on the system output device is 1001803809710. In this example, it is worth noting that we have used the date constructor to create a date object that does not accept any arguments. And this constructor uses the System.currenttimemillis () method internally to get the date from the system.
So now we know how to get the number of milliseconds we've experienced since January 1, 1970. How can we display this date in a user-understood format? Here the class Java.text.SimpleDateFormat and its abstract base class Java.text.DateFormat come in handy.
Custom format for date data
If we want to customize the format of date data, for example Saturday-September-29th-2001. The following example shows how to do this:
The following is a reference fragment: 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" to the SimpleDateFormat constructor, we can specify the format we want. You should be able to see that the ASCII character in the format string tells the Format function which part of the date data is shown below. 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. Passing "Ee-mm-dd-yy" displays sat-09-29-01. See the full instructions for the Sun's web site to get date formatting options.
Parse text data into date objects
Let's say we have a text string that contains a formatted date object, and we want to parse the string and create a Date object from the text date data. We'll call the SimpleDateFormat class again with the format string "mm-dd-yyyy", but this time we use formatting parsing instead of generating a text date data. Our example, shown below, will parse the text string "9-29-2001" and create a Date object with a value of 001736000000.
Example program:
The following is a reference fragment: 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 is 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 ()); } } } |
Use the standard date format process
Now that we've been able to generate and parse custom date formats, let's look at how to use the built-in formatting process. Method Dateformat.getdatetimeinstance () allows us to get the standard date format process in several different ways. In the following example, we get four built-in date formatting processes. They include a short, medium, long, and full date format.
The following is a reference fragment: 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 (Fulldateformat.format (date)); } } |
Notice that we have passed two values in each call to Getdatetimeinstance. The first parameter is the date style, and the second parameter is the time style. They are all basic data type int (integer). Given the readability, we used the constants provided by the DateFormat class: Short, MEDIUM, LONG, and full. For more methods and options to get the time and date formatting process, see the explanations on the Sun's web site.
When running our example program, it will output the following to the standard output device:
9/29/01 8:44 PM
SEP, 2001 8:44:45 PM
September, 2001 8:44:45 PM EDT
Saturday, September, 2001 8:44:45 PM EDT
Calendar class
We now have the ability to format and create a Date object, but how can we set and get a specific part of the date data, such as hours, days, or minutes? How do we add or subtract values from 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 reviled method in the date class.
Suppose you want to set, get, and manipulate the various parts of a Date object, such as a one-month day or a one-week day. To demonstrate this process, we will use the specific subclass Java.util.GregorianCalendar. Consider the following example, which calculates that the tenth Friday below is number 13th.
The following is a reference fragment: 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 of our 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 Friday by adding for 7 days. Cal.add (Gregoriancalendar.day_of_month, 7); If the day of month is we have Another Friday the 13th. if (Cal.get (gregoriancalendar.day_of_month) = = 13) { friday13counter++; System.out.println (Dateformat.format (Cal.gettime ())); } } } } |
In this example we made an interesting function call:
Cal.set (Gregoriancalendar.day_of_week,
Gregoriancalendar.friday);
And:
Cal.add (Gregoriancalendar.day_of_month, 7);
The Set method allows us to adjust our time to Friday by simply setting the day of the week for this field. Note that here we use constant Day_of_week and Friday to enhance the readability of the code. The Add method allows us to add a numeric value to the date. All the complex calculations of the run year are handled automatically by this method.
The output of our example is:
System Date:saturday, September 29, 2001
When we set it to Friday, it became: 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
Time is in your hands.
With examples of these Date and calendar classes, you should be able to use Java.util.Date, Java.text.SimpleDateFormat, and Java.util.GregorianCalendar to create many methods.
In the following article, we will discuss the use techniques of more advanced date and Calendar classes, including time zones and internationalized formats. We'll also look at the difference between two date classes Java.sql.Date and Java.util.Date.