Learn the date and time processing in Java and the preparation of java calendar applet

Java in Java The util package provides a date class that encapsulates the current date and time.

The date class supports two constructors. The first constructor initializes the current date and time of the object.

The constructor under date () receives a parameter equal to the number of milliseconds that have elapsed since midnight on January 1, 1970

Once a date object is available, you can call any of the following supported methods to use time:

This will produce the following results:

Date comparison there are three ways to compare two dates:

Formatting dates using simpledateformat

Simpledateformat is a concrete class that is used locally to format and convert dates. Simpledateformat allows you to select the user-defined mode as date time format. For example:

This will produce the following results:

Simple dateformat format code to specify the time format, use the time pattern string. In this mode, all ASCII letters are reserved as mode letters, which are defined as follows:

This will produce the following results:

It is not a good practice to provide multiple formatting of dates. A format string can indicate the index of the parameter to be formatted. The index must immediately follow% and must be terminated by $. For example:

This will produce the following results:

Alternatively, the < flag can be used. Then it indicates that the same parameter should be used again according to the above format specification. For example:

Date and time conversion characters

The string conversion date simpledateformat class has some additional methods, such as parse (), which attempts to convert a string according to the format of the object stored in a given simpledateformat. For example:

The running example of the above program will produce the following results:

Hibernate for a period of time. You can hibernate for any period of time, from one millisecond to the whole life cycle of your computer. For example, the following program will sleep for 10 seconds:

This will produce the following results:

Measure execution time. Sometimes, the time point that may need to be measured is in milliseconds. So let's rewrite the above example again:

This will produce the following results:

Gregoriancalendar class gregoriancalendar is a specific implementation of calendar class, that is, the familiar implementation of normal Gregorian Gregorian calendar. Instead of discussing the calendar class in this tutorial, look at the standard Java documentation.

The calendar's getInstance () method returns a gregoriancalendar initialized with the current date and time default locale and time zone. Gregorian calendar defines two fields: AD and BC. These represent the two eras defined in the Gregorian calendar.

There are also gregoriancalendar objects with several constructors:

This will produce the following results:

Calendar applet

Let's look at a calendar applet. Here we use the traditional MVC structure to design three classes: calendarviewer, calendarcontroller and calendarmodel. CalendarViewer. Java mainly processes the UI, follows the existing code, arranges it and extracts the business logic to focus on the display layer processing. CalendarViewer. java

The UI structure is mainly divided into three parts, corresponding to the top, middle and bottom three panels in the figure.

The processing of event listening is completed by the following methods.

Event based UI updates are done by the following two methods.

Create a two-dimensional array to represent the monthly days of leap year and non leap year respectively. The main methods are:

Kimlarson formula

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>