Java Time related content learning (1) Calendar

Source: Internet
Author: User
Tags abstract dateformat time zones locale


Java operation date/time, often involves calendar,date,dateformat these classes.



Recently decided to organize these content system, so later use, will be more handy. This chapter focuses on "Java time frame" and "Class Calendar".



In learning the Calendar class, we first have a general idea of it, a framework in mind, and then an example to learn how to use it.



Java Time Architecture Diagram



Java Calendar, date and DateFormat diagram are as follows:






Description



(milliseconds) represents milliseconds.



milliseconds = "actual Time"-"1970-01-01 00:00:00". Calendar and date depend on milliseconds to indicate time.



Calendar represents the date/time.



It is an abstract class that relies on milliseconds. GregorianCalendar is a subclass of the calendar, usually when we get the calendar instance through Calendar.getinstance (), we actually return the GregorianCalendar object.



Calendar and locale are associated, and locale represents a region; Locale values are not the same, and calendar dates/times are different.



Calendar and timezone associations, while timezone represent time zones; different time zones, calendar dates/times are different.



The date/time is indicated by date.



It also relies on milliseconds implementations.



Before JDK 1.1, it was usually through data that was "minutes and seconds". However, because date's associated APIs are not easy to internationalize. Starting with JDK 1.1, you should use the Calendar class to manipulate the time of day and minutes, and you can format and parse the date string through the DateFormat class. The corresponding method in Date is deprecated.



DateFormat is a tool class that formats/resolves date/time.



It is the date formatting tool that helps us format date and then converts date into the string string we want to use.



It is an abstract class. Typically, we get DateFormat instances by getinstance (), Getdateinstance (), and Getdatetimeinstance (), which is actually the returned SimpleDateFormat object.



Calendar Introduction



Calendar definition

publicabstractclass Calendar implements Serializable, Cloneable, Comparable <Calendar> {}
Calendar is an abstract class.

Its implementation uses the factory method in the design pattern. Performance: When we obtain a Calendar instance, Calendar will return the corresponding Calendar object according to the passed parameters. There are two ways to get a Calendar instance: 1) When we get a calendar through Calendar.getInstance (), the default is a GregorianCalendar object that is returned. GregorianCalendar is an implementation class of Calendar, which provides a standard calendar system used by most countries in the world. 2) When we get the calendar through Calendar.getInstance (TimeZone timezone, Locale locale) or Calendar.getInstance (TimeZone timezone) or Calendar.getInstance (Locale locale), it returns "corresponding time zone (zone) or local (local) etc Calendar used ". For example, if it is Japan, the JapaneseImperialCalendar object is returned.

Refer to the following code:

public static Calendar getInstance ()
{
    // Call createCalendar () to create a calendar
    Calendar cal = createCalendar (TimeZone.getDefaultRef (), Locale.getDefault ());
    cal.sharedZone = true;
    return cal;
}
     
     
public static Calendar getInstance (TimeZone zone)
{
    // Call createCalendar () to create a calendar
    return createCalendar (zone, Locale.getDefault ());
}
     
     
public static Calendar getInstance (Locale aLocale) {
    // Call createCalendar () to create a calendar
    Calendar cal = createCalendar (TimeZone.getDefaultRef (), aLocale);
    cal.sharedZone = true;
    return cal;
}
     
public static Calendar getInstance (TimeZone zone,
                   Locale aLocale)
{
    // Call createCalendar () to create a calendar
    return createCalendar (zone, aLocale);
}
     
private static Calendar createCalendar (TimeZone zone,
                   Locale aLocale)
{
    // (01) If the area is "th", return the BuddhistCalendar object
    // (02) If the region is "JP", return the JapaneseImperialCalendar object
    if ("th" .equals (aLocale.getLanguage ())
        && ("TH" .equals (aLocale.getCountry ()))) {
        return new sun.util.BuddhistCalendar (zone, aLocale);
    } else if ("JP" .equals (aLocale.getVariant ())
       && "JP" .equals (aLocale.getCountry ())
       && "ja" .equals (aLocale.getLanguage ())) {
        return new JapaneseImperialCalendar (zone, aLocale);
    }
     
    // (03) Otherwise, return GregorianCalendar object
    return new GregorianCalendar (zone, aLocale);
}
After we get the Calendar instance, we can operate the calendar through a series of methods provided by Calendar.

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.