Java foundation -Java the Date and Calendar classes

Java language Calendar (calendar), Date (date), and DateFormat (Date Format) form a basic but very important part of the Java standard. Date is a critical part of the business logic calculations. All developers should be able to calculate future date, custom date display format, and parsing the text data into a date object.

Creating a date object

Let's look at a current date and time using the system to create a Date object and returns a long integer. This time is usually referred to as the Java Virtual Machine (JVM) system time of the 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, 6:50 pm about the way, the results example above shows on the system output device is 1001803809710. It is noteworthy that we created using the Date constructor a date object, the constructor does not accept any parameters, and uses this constructor System.currentTimeMillis inside () method to get the date from the system. Now that we know how to get the number of milliseconds from January 1, 1970 began to experience a. How can we understand the users in a format to display the date? Here java.text.SimpleDateFormat class and its abstract base class java.text.DateFormat will come in handy.

Custom format date data

If we want to customize the date format of data, such as Saturday - 9 Month - 29 - 2001 The following example shows how to accomplish this:.

Import java.text.SimpleDateFormat;

Import java.util.Date;

{class DateExample2 public

public static void main (String [] args) {

the SimpleDateFormat bartDateFormat the SimpleDateFormat new new = ( "EEEE-MMMM-dd-YYYY");

a Date = new new DATE a Date ();

System.out.println (bartDateFormat.format ( DATE));

}}

As long as the transfer format string "EEE-MMMM-dd-yyyy " to the constructor SimpleDateFormat, we will be able to specify the format you want. ASCII characters in the format string formatting functions shown below to tell which one portion of the date data. EEEE of the week, MMMM is the month, dd is the day, yyyy is the years. The number of characters determines how the date is formatted. Transfer "EE-MM-dd-yy " is displayed Sat-09-29-01.

The text data is parsed into a Date object

Suppose we have a text string containing a formatted date object, we want to parse the string and create a date object from a text date data. We will once again format string "MM-dd-yyyy" call SimpleDateFormat category. But this time, we use formatted analytical rather than generate a text date data. Our example is shown below, will parse the text string "9-29-2001" and create a value of 001.736 billion target date.

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());

}

} }

Using the standard date format process

now that we can already generate and parse a custom date format, let us take a look at how to use the built-in formatting process. Methods DateFormat.getDateTimeInstance () allows us to get the standard used in several different ways date formatting process. Here's what we get four built-in date formatting process. They include a short, medium, long, and full date format.

the java.text.DateFormat Import;

Import java.util.Date;

public DateExample4 class {

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));

}

}

Note that we each call to getDateTimeInstance in both passing two values: The first parameter is the date style, and the second parameter is the time style. They are the basic data type int (integer). Taking into account the readability, we use the DateFormat class provides the constants: SHORT, MEDIUM, LONG, and FULL.

Running our example procedures, it will output to standard output the following contents:

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

Calendar class

specific part that we are now able to format and create a date object, but how can we set and get it date data, such as hours, days, or minutes? how do we plus or minus the value in these parts of the date? the answer is to use Calendar category.

Suppose you want to set up, acquire, and manipulate the various parts of a date object, for example a month for a day or a day of the week, in order to demonstrate this process, we will use specific subclass java.util.GregorianCalendar. Consider the following example, it is calculated following the 13th tenth Friday.

a java.util.GregorianCalendar Import;

Import java.util.Date;

Import the 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(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 7 days.

cal.add(GregorianCalendar.DAY_OF_MONTH,7);

The month The Day of the If // IS 13 is WE have have

// Another catalog on Friday The 13th.

IF (cal.get (GregorianCalendar.DAY_OF_MONTH) 13 is ==) {friday13Counter ++;

System.out.println (dateFormat.format (cal.getTime () ));

}

}

}

}

in this example we have made interesting function call:

cal.set(GregorianCalendar.DAY_OF_WEEK,

GregorianCalendar.FRIDAY);

和:cal.add(GregorianCalendar.DAY_OF_MONTH,7);

set method allows us to adjust our time on Friday by a simple set day of the week this domain. Here we note that the use of the constants FRIDAY DAY_OF_WEEK and to enhance readability of the code. add method allows us to add value on the date, leap year all the complex calculations by this method of automatic processing.

The output of our example is:

System a Date: Saturday, September 29,2001

When we set it to Friday became:

Catalog on Friday, September 28,2001

catalog on Friday, September 13,2002

catalog on Friday, December 13,2002

catalog on Friday, June 13,2003

catalog on Friday, February 13,2004

catalog on Friday, August 13,2004

catalog on Friday, May 13,2005

catalog on Friday, January 13,2006

catalog on Friday, 13,2006 october

catalog on Friday, April 13,2007

catalog on Friday, July 13,2007

catalog on Friday, June 13,2008

time to grasp in your hands

With these examples of Date and Calendar classes, you should be able to use java.util.Date, java .text.SimpleDateFormat, and java.util.GregorianCalendar create a number of methods.

Reproduced in: https: //www.cnblogs.com/licheng/archive/2008/11/28/1343167.html

Guess you like

Origin blog.csdn.net/weixin_34321977/article/details/92632078