Use Java Swing as a calendar control program

Source: Internet
Author: User
Tags abstract dateformat

A date is one of the most basic forms of data, so many languages or databases provide support for dates, or libraries that are built into or offered in this category. Java is no exception; the Java application programming interface provides a way to describe, compute, contrast, display, and analyze dates in a cross-platform, stand-alone language. In this article, I'm going to talk about the custom control of swing, which allows you to visually acquire time information instead of knocking on commands. In this way, you can avoid invalid time information, so that your users enjoy the visual way.

Dates in Java

Java describes the method of date and time using the traditional date command or some of the Java libraries. Regardless of its name, the class of dates does not have the concept of a date in the ordinary sense (day, month, year, etc.) technically, it is only a result of the accumulation of one out of 10,000 seconds from the start of the January 1, 1970 00:00:00 GMT. Although these date-related classes have various methods of dealing with dates, this is not enough.

To facilitate the resolution of date problems, JDK 1.1 introduces an abstract class Java.util.Calendar. A subclass of the exact date of calculation uses a pointer to a specific date based on the calendar system. This is because the calendar system has always been a number of ways to calculate their own dates (such as the Gregorian calendar, Roman, lunar calendar).

The Java application programming interface provides classes called Java.util.GregorianCalendar to process date information based on the solar system.

Ultimately, a class named Java.text.DateFormat is an abstract processing format that interprets date classes in a way that is independent of language. This way depends on the conventions, the rules, and the location of known language requirements. A specific subclass called DateFormat does the actual work. For example, a subclass named Java.text.SimpleDateFormat allows you to analyze or set a date format based on an existing pattern.

Overall, these three most basic classes (date, Calendar, and DateFormat) provide a powerful and flexible system for dealing with dates. It may seem a bit confusing at first, but once you understand it, you'll see that the structure is very meaningful and has a strong logical background behind it. (Look at the JDK API documentation and you'll get a lot of documents and examples)

Custom-made date control

Although Java provides a way to manipulate date data, it seems to be a bit of a hassle if you let you deal with three different classes. Sometimes you need to use a lot of methods to accomplish a simple task with many entities. In addition to those, swing does not provide an entry date component. Of course, if the use of standard control such as text box is not difficult to complete. However, dates are so common in applications that we often write the same code, input, analyze, and check time over and over again.

For these reasons, I've built a simple, custom control that you can use to access date data. Basically, this is a standard button to display the date (see Figure A).

Figure A

Custom Date Control

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.