Common Date and Time classes in Java

Source: Internet
Author: User


Date: the most basic date and time class, returns the number of milliseconds of a relative date. Accurate to milliseconds, but does not support date internationalization and Time Zone display.
Calender class: a more powerful time class than date. It is an abstract class and provides regular date modification and international support.
Gregoriancalendar class: provides a method for processing dates. It is a subclass of the calendar class and is useful for determining a leap year.
Dateformat: it can accept string input and output, and supports formatting date/time information.
Simpledateformat: more powerful Date and Time formatting class, custom format date and time.
Java. util. locale class: describes specific geographical, political, and cultural regions. locale objects mainly encapsulate "region" and "language type.

Use Case column:
1. Get the current time

Public String getcurtime () // obtain the current time
{
Date Now = new date ();
Return now. gethours () + ":" + now. getminutes () + ":" + now. getseconds ();
}
Public String getcurdate () // obtain the current date
{
Calendar Cal = calendar. getinstance ();
Return Cal. get (calendar. year) + "year" + (Cal. get (calendar. month) + 1) + "month" + Cal. get (calendar. date) + "day ";
}

2. Obtain the time before January 1, January.
Calendar = calendar. getinstance ();
Calendar. Add (calendar. Month,-1 );

3. Obtain the time from a year ago
Calendar = calendar. getinstance ();
Calendar. Add (calendar. Year,-1 );

4. Get the number of milliseconds of the relative date
Calendar calendar = calendar. getinstance ();
Long nowtime = calendar. gettime (). gettime ();

5. Convert string to date
String strdate = "2011-08-13 ";
Simpledateformat sdateformat = new simpledateformat ("yyyy-mm-dd ");
Date sdate = sdateformat. parse (strdate );

6. Format and display date data
Date dt_in: date type data
Boolean bshowtimepart_in: whether to display the time part
@ Return string formatted Date Format
*/
Public String doformatdate (Java. util. Date dt_in, Boolean bshowtimepart_in ){
If (bshowtimepart_in)
Return (New simpledateformat ("yyyy-mm-dd hh: mm: SS"). Format (dt_in );
Else
Return (New simpledateformat ("yyyy-mm-dd"). Format (dt_in );
}

7. Determine a leap year
Use the java. util. gregoriancalendar class: a subclass of the calendar class. It is useful to have a method for determining the leap year.
Public void testgregoriancalendar (){
Gregoriancalendar GC = new gregoriancalendar ();
System. Out. println (GC. isleapyear (2008 ));
System. Out. println ("------------------------");
}

8. Internationalization,Information is usually used to display date, number, or text information in a region or language-related manner in internationalization and localization programs.
Public void testlocale (){
Locale defaultlocale = locale. getdefault (); // the country and region of the Current Machine
System. Out. println (defaultlocale );
System. Out. println (defaultlocale. getlanguage (); // The name of the language in English
System. Out. println (defaultlocale. getcountry (); // name of the country in English
System. Out. println (defaultlocale. getdisplayname (); // language name (Country name)
System. Out. println (defaultlocale. getdisplaylanguage (); // language name
System. Out. println (defaultlocale. getdisplaycountry (); // country name
System. Out. println ("--------------------------");
}

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.