Java Date type processing

Source: Internet
Author: User
Tags date now dateformat locale parse string
Java Date type processing
Profile

Whether you're dealing with financial transactions or planning your next move, you need to know how to build, use, and display dates in Java. This requires you to simply check the API reference for the corresponding class: a date can create objects of 3 related classes. This article tells you what you want to know.

Java statistics represents a date from the number of milliseconds since January 1, 1970. That is to say, for example, January 2, 1970, is 86,400,000 milliseconds after January 1. Similarly, December 31, 1969 is 86,400,000 milliseconds before January 1, 1970. The Java date class uses the long type to record these millisecond values. Because Long is a signed integer, the date can be before or after January 1, 1970. The maximum positive and negative values represented by a long type can easily represent 290,000,000 years, which is appropriate for most people's time requirements.

Date class

The date class can be found in the Java.util package, with a long value representing a specified moment. A useful constructor for it is date (), which creates an object that represents the time of creation. The GetTime () method returns a Long value for the Date object. In the following program, I use the date () constructor to create an object that represents the running time of the program, and use the GetTime () method to find the number of milliseconds represented by this date:

Import java.util.*;


public class Now {
public static void Main (string[] args) {
Date now = new Date ();
Long Nowlong = Now.gettime ();
System.out.println ("Value is" + Nowlong);
}
}

When I run this program, I get 972,568,255,150. Quickly confirm this number, at least in a reasonable range: it's less than 31, which is reasonable relative to the time I wrote this article on January 1, 1970. The computer is the millisecond value of time, people do not want to say "I will see you in 996,321,998,34." "Fortunately, Java provides a way to convert date objects to strings, expressed as a traditional form." We'll discuss the DateFormat class in the next section, which visually establishes a date string.
DateFormat class
One goal of the DateFormat class is to create a string that people can recognize. However, because of the language differences, not all people want to see the exact same format of the date. The French prefer to see "Decembre 2000," But Americans are used to seeing "December 25,2000." So after a DateFormat instance is created, this object contains information about the display format of the date. If you use the user's computer locale to set the default format, you can create the DateFormat object as follows, using the Getdateinstance () method:

DateFormat df = dateformat.getdateinstance ();

The DateFormat class can be found in the Java.text package.

Convert to String

You can use the format () method to convert a Date object to a string. The following sample program illustrates this problem:

Import java.util.*;
Import java.text.*;

public class Nowstring {
public static void Main (string[] args) {
Date now = new Date ();
DateFormat df = dateformat.getdateinstance ();
String s = Df.format (now);
System.out.println ("Today is" + s);
}
}

In the above code, the Getdateinstance () method with the default format is shown with no parameters. Java also offers several choice date formats, which you can get by using overloaded getdateinstance (int style). For convenience reasons, DateFormat provides several preset constants that you can use with these constant parameters. Here are a few short, MEDIUM, LONG, and full types examples:

Import java.util.*;
Import java.text.*;

public class Styledemo {
public static void Main (string[] args) {
Date now = new Date ();

DateFormat df = dateformat.getdateinstance ();
DateFormat df1 = dateformat.getdateinstance (Dateformat.short);
DateFormat DF2 = dateformat.getdateinstance (Dateformat.medium);
DateFormat df3 = dateformat.getdateinstance (Dateformat.long);
DateFormat df4 = dateformat.getdateinstance (dateformat.full);
String s = Df.format (now);
String S1 = Df1.format (now);
String s2 = Df2.format (now);
String s3 = Df3.format (now);
String S4 = Df4.format (now);

System.out.println ("(Default) is" + s);
System.out.println ("(short) are" + S1);
System.out.println ("(MEDIUM) is" + S2);
System.out.println ("(LONG) is" + S3);
System.out.println ("(full) are" + S4);
}
}

The program output is as follows:

(Default) Today is Nov 8, 2000
(short) Today is 11/8/00
(MEDIUM) Today is Nov 8, 2000
(LONG) Today is November 8, 2000
(full) Today is Wednesday, November 8, 2000

The same program, after running with the default settings on my computer, changes the locale to Sweden, and the output is as follows:

(Default) Today is 2000-nov-08
(short) Today is 2000-11-08
(MEDIUM) Today is 2000-nov-08
(LONG) Today is den 8 November 2000
(full) Today is den 8 November 2000

From here, you can see that the Swedish month is not uppercase (though November or November). Also, long and full versions are the same in Swedish, but American English is different. In addition, interestingly, the Swedish words of the Wednesday, Onsdag, are not included in the full date, English is included.
Note that you can use the Getdateinstance () method to change the language of the DateFormat instance, but in the example above, it is done by changing the locale of the Windows98 Control Panel. The different locales, the results are different, there are advantages, there are not enough, Java programmers should understand these. One advantage is that Java programmers can display dates in just one line of code, and computers in different parts of the world run the same program with a date format. But it's also a drawback when programmers want to show the same format--and there's a good thing to do, for example, mixing output text and dates in a program, and if the text is in English, we don't want the date format to be another format, like German or Spanish. If the programmer relies on date format programming, the date format will be different depending on the locale of the computer where the program is run.

Parse string
Related Article

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.