laitimes

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

When using the Java language to write some small programs, it is always indispensable to deal with time, so how should we write when we need to output datetime in Java? Today Xiaobian will bring you an introduction to big data programming: Java date time article.

<h1 class="pgc-h-arrow-right" data-track="2" > Java Date object</h1>

The Java.util package provides a Date class to encapsulate the current date and time, and the Date class provides two constructors to instantiate the Date object.

1. The first constructor initializes the object with the current date and time.

Date( )

2. The second constructor receives an argument that is the number of milliseconds since January 1, 1970.

Date(long millisec)

3. After creating the date object, you can call the following methods.

<col>

method

description

boolean after(Date date)

If the Date object that calls this method returns true after the specified date, false is returned.

boolean before(Date date)

If the Date object that calls this method returns true before the specified date, false is returned.

Object clone( )

Returns a copy of this object.

int compareTo(Date date)

Compares the Date object at the time of calling this method to the specified date, returning 0 when both are equal; the calling object returns a negative number before the specified date; and the calling object returns a positive number after the specified date.

int compareTo(Object obj)

If obj is of type Date, the operation is equivalent to CompareTo (date); otherwise, it throws a ClassCastException.

boolean equals(Object date)

Returns true when the Date object that calls this method is equal to the specified date, otherwise returns false.

long getTime( )

Returns the number of milliseconds represented by this date object since January 1, 1970, 00:00:00 GMT.

int hashCode( )

Returns the hash code value of this object.

void setTime(long time)

Set the time and date with the number of milliseconds since January 1, 1970 00:00 GMT.

String toString( )

Convert this Date object to String:dow mon dd hh:mm:ss zzz yyyy, where :dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).

<h1 class="pgc-h-arrow-right" data-track="172" >2. Get the current date</h1>

Getting the current date and time in Java is very simple, the toString() method of the Date object is used to print the current date and time as follows:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

Running result:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

<h1 class="pgc-h-arrow-right" data-track="173" >3. Date comparison</h1>

Java uses the following three methods to compare two dates:

(1) Use the getTime() method to get two dates (the number of milliseconds since January 1, 1970) and compare the two values.

(2) Use the before(), after(), and equals() methods. For example, if the 12th number of a month is earlier than the 18th number, it returns true for new Date(99,2,12).before(new Date(99,2,18)).

(3) Use the CompareTo() method, which is defined by the Comparable interface and implemented by the Date class.

<h1 class="pgc-h-arrow-right" data-track="174" >4. Format date with SimpleDateFormat</h1>

SimpleDataFormat is a class that formats and analyzes dates in a locale-sensitive manner. SimpleDataFormat allows you to select any user-defined datetime format to run. For example:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");

This line of code establishes the format of the conversion, where yyyy is the full year, MM is the month, dd is the date, and HH:mm:SS is the hour, minute, and second.

Note: Some formats are uppercase and some are lowercase, e.g. MM is the month and mm is the minute; HH is the 24-hour clock and hh is the 12-hour clock.

<h1 class="pgc-h-arrow-right" data-track="175" >5. Formatted encoding of dates and times</h1>

The time pattern string is used to specify the time format. In this mode, all ASCII letters remain as pattern letters, which are defined as follows:

letter

example

G

Epoch marker

TO

and

Four-digit year

2021

M

month

July or 07

d

The date of one month

15

h

A.M./P.M. (1~12) format hours

12

H

Hours of day (0~23)

21

m

Number of minutes

59

s

Seconds

30

S

The number of milliseconds

234

And

Day of the week

Tuesday

D

Days of the year

360

F

The day of the week for the first few weeks of the month

2 (second Wed. in July)

in

The first few weeks of the year

40

In

Weeks of the month

1

a

A.M./P.M tag

PM

k

Hours of the day (1 to 24)

24

K

A.M./P.M. (0~11) format hours

10

With

time zone

Eastern Standard Time

Text delimiter

Delimiter

apostrophe

`

<h1 class="pgc-h-arrow-right" data-track="176" >6. Format dates using printf</h1>

The printf method makes formatting time and date very easy. Use a two-letter format that begins with %T and ends with a letter in the following table.

Converter

illustrate

c

Includes all date and time information

Saturday Oct 27 14:21:20 CST 2007

"Year-Month-Day" formality

2021-6-28

"Month/Day/Year" formality

6/28/21

r

"HH:MM:SS PM" format (12 time format)

02:25:51 PM

T

"HH:MM:SS" format (24-time format)

14:28:16

R

"HH:MM" format (24-time clock)

14:43

example:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

If you need to provide dates repeatedly, then formatting each section in this way can be a bit complicated. Therefore, you can use a formatted string to indicate the index of the parameter to format.

The index must follow % and must end with $. For example:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

Alternatively, you can use the &lt; flag, which indicates that the previously formatted parameter will be used again. For example:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

Defines the conversion characters for the date format to enable the date to generate a new string from the specified conversion characters. These date converters are as follows:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

<h1 class="pgc-h-arrow-right" data-track="178" >7, parse string as time</h1>

The SimpleDataFormat class also has some additional methods, notably parse(), which attempts to store parsed strings according to the format of a given SimpleDaFormat object. For example:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

<h1 class="pgc-h-arrow-right" data-track="179" >8</h1>

Sleep() puts the current thread into a stalled state (blocking the current thread) and abandons cpu usage, in order not to let the current thread consume the CPU resources obtained by the process alone, thus allowing a certain amount of execution time for other threads.

You can let a program sleep for a millisecond, or you can make it sleep for any amount of time on your computer. For example, the following program will sleep for 3 seconds:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

<h1 class="pgc-h-arrow-right" data-track="180" >9, measuring time</h1>

The following example shows how to measure time intervals in milliseconds:

Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time
Introduction to Big Data Programming: Java Date and Time 1, Java Date Object 2, Get Current Date 3, Date Comparison 4, Format Date 5 with SimpleDateFormat, Format Coding 6 for Date and Time, Format Date 7 with Printf, Parse String for Time 8, Java Sleep 9, Measure Time

The above is all about the introduction to big data programming: Java date time, I hope this article can help you ~ ~

Read on