Days and hours of operation

Source: Internet
Author: User
Tags date format dateformat locale relative string

Getting, setting, and formatting dates in Java
1 Java provides 3 date classes: Date, calendar, and DateFormat.
The date () method is used primarily to create date objects and obtain dates;
The Calendar () method is primarily used to get and set the date;
The DateFormat () method is primarily used to create date formatters and then convert dates to various date format string outputs by the formatter.
2 The base date specified in the Java language is Greenwich Mean Time 1970.1.1.00:00:00, and the current date is converted from the number of milliseconds experienced by the base date.
3 The Datefomat class is in the Java.text package, and the date and calendar classes are in the Java.util package.
4) Examples are as follows:
Import java.util.*;
Import java.text.*;

public class Displaydate {
public static void Main (string[] args) {
Date today;
Calendar now;
DateFormat F1,f2;
String s1,s2;

System.out.println ("\ n show the relevant usage of the date class");

Today = new Date ();
System.out.println ("new Date () = \ T" + today);

System.out.println ("\ n show various date formats with DateFormat class");

Show various date formats
F1 = Dateformat.getdateinstance ();
S1 = F1.format (today);
System.out.println ("dateformat.getdateinstance () = t" +s1);

F1 = Dateformat.getdateinstance (Dateformat.long,locale.china);
S1 = F1.format (today);
System.out.println ("dateformat.getdateinstance (Dateformat.long,locale.china) = \ T" + S1);

F1 = Dateformat.getdateinstance (Dateformat.medium,locale.china);
S1 = F1.format (today);
System.out.println ("dateformat.getdateinstance (Dateformat.medium,locale.china) = \ T" + S1);

F1 = Dateformat.getdateinstance (Dateformat.short,locale.china);
S1 = F1.format (today);
System.out.println ("dateformat.getdateinstance (Dateformat.short,locale.china) = \ T" + S1);

System.out.println ("\ n show various time formats with DateFormat class");

Show various time formats
F1 = Dateformat.gettimeinstance ();
S1 = F1.format (today);
System.out.println ("dateformat.gettimeinstance () = t" +s1);

F1 = Dateformat.gettimeinstance (Dateformat.long,locale.china);
S1 = F1.format (today);
System.out.println ("dateformat.gettimeinstance (Dateformat.long,locale.china) = \ t" +s1);

F1 = Dateformat.gettimeinstance (Dateformat.medium,locale.china);
S1 = F1.format (today);
System.out.println ("dateformat.gettimeinstance (Dateformat.medium,locale.china) = \ t" +s1);

F1 = Dateformat.gettimeinstance (Dateformat.short,locale.china);
S1 = F1.format (today);
System.out.println ("dateformat.gettimeinstance (Dateformat.short,locale.china) = \ t" +s1);

System.out.println ("Show relative time usage for calendar");

now = Calendar.getinstance ();
Today = Now.gettime ();
System.out.println ("Calendar.getinstance (). getTime () = \ T" + today.tostring ());

}
}

The results of the program operation are shown as follows:

Show the relative usage of the date class
New Date () = Fri may 13:29:32 CST 2003

displaying various date formats with the DateFormat class
Dateformat.getdateinstance () = 2003-5-2
Dateformat.getdateinstance (Dateformat.long,locale.china) = May 2, 2003
Dateformat.getdateinstance (Dateformat.medium,locale.china) = 2003-5-2
Dateformat.getdateinstance (Dateformat.short,locale.china) = 03-5-2

Displaying various time formats with the DateFormat class
Dateformat.gettimeinstance () = 13:29:32
Dateformat.gettimeinstance (Dateformat.long,locale.china) = 01:29 P.M. 32 seconds
Dateformat.gettimeinstance (Dateformat.medium,locale.china) = 13:29:32
Dateformat.gettimeinstance (Dateformat.short,locale.china) = 1:29

Show relative time usage for calendar
Calendar.getinstance (). GetTime () = Fri may 13:29:33 CST 2003



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.