Analysis of application of data class in Java _jsp programming

Source: Internet
Author: User
Tags dateformat time and date
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.

Import Java.util.Date;

public class DateExample1 {
public static void Main (string[] args) {//self replacement []
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:

Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class DateExample2 {

public static void Main (string[] args) {//self replacement []

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:

Import Java.text.SimpleDateFormat;
Import Java.util.Date;

public class DateExample3 {

public static void Main (String[]args) {//self replacement []
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.

Import Java.text.DateFormat;
Import Java.util.Date;

public class DateExample4 {

public static void Main (string[] args) {//self replacement []
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.