Organize Java Basics--date & Time1

Source: Internet
Author: User
Tags time and date

The Java.util package provides a date class to encapsulate the current day and time provides 2 functions to instantiate a Date object
1.Date ()
2.Date (long millisec)//parameter is the number of milliseconds since 1970/01/01
Date Comparison
Java uses the following three ways to compare two dates:
1. Use the GetTime () method to get two dates (the number of milliseconds that have elapsed since January 1, 1970), and then compare the two values.
2. Use the method before (), after (), and Equals ().
3. Using the CompareTo () method, which is defined by the comparable interface, the Date class implements this interface.
format dates with SimpleDateFormat
SimpleDateFormat is a class that formats and parses dates in a locale-sensitive manner. SimpleDateFormat allows you to select any user-defined date-time format to run.

import java.util.*;import java.text.*;public class DateDemo {   public static void main(String args[]) {       Date dNow = new Date( );      SimpleDateFormat ss = new SimpleDateFormat ("E yyyy.MM.dd ‘at‘ hh:mm:ss a zzz");      System.out.println("Current Date: " +ss.format(dNow));   }}输出结果:Current Date:星期日 2018.01.14 at 00:29:06 上午 CST

Formatted encoding of date and time

Letters Description
G ERA markers
Y Four-bit year
M Month
D Text
H 12-hour system
H 24-hour system
M Score of
S Seconds
S Milliseconds
E Day of the Week
D The days of the year
F Weeks of the week in one months
W The first few weeks of the year
W The first week of one months
A a.m/p.m
K Hours of the day 1-24
K a.m./p.m. (0~11) format hours
Z Time
Text delimiter
‘‘ Single quotation marks

Format dates with printf

translator description
c include all date and time information /td>
F "year-month-day" format
D Month/day/year format
R "HH:MM:SS PM" format (12 o'clock)
T "HH:MM:SS" format (24 o'clock)
R "hh:mm" format (24 o'clock-year)
import java.util.Date;public class DateDemo {  public static void main(String args[]) {      Date date = new Date();    System.out.printf("全部日期和时间信息:%tc%n",date);              System.out.printf("年-月-日格式:%tF%n",date);      System.out.printf("月/日/年格式:%tD%n",date);      System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date);      System.out.printf("HH:MM:SS格式(24时制):%tT%n",date);      System.out.printf("HH:MM格式(24时制):%tR",date);    }}输出结果:全部日期和时间信息:星期日 一月 14 00:44:02 CST 2018年-月-日格式:2018-01-14月/日/年格式:01/14/18HH:MM:SS PM格式(12时制):12:44:02 上午HH:MM:SS格式(24时制):00:44:02HH:MM格式(24时制):00:44F:\>

Repeat to provide the date, can be formatted:

import java.util.Date;  public class DateDemo {    public static void main(String args[]) {       Date date = new Date();              System.out.printf("%1$s %2$tB %2$td %2$tY",  "Due date:", date);             System.out.printf("%s %tB %<te %<tY", "Due date:", date);   }}输出结果:Due date: 一月 14 2018Due date: 一月 14 2018%1$s>>"Due date:" 输出第一个字符串:Due date:%2$tB %2$td %2$tY>>date 输出第2个字符串date中的月日年

**date the method that can be called after the object is created:

Method Description
Boolean after (date date) Returns False if the Date object calling this method returns true after the specified date
Boolean before (date date) Returns False if the Date object calling this method returns true before the specified date
Object Clone () Returns a copy of this object
int CompareTo (date date) Compares the date object when this method is called and the specified day. Both return 0 when they are equal. The calling object returns a negative number before the specified date. The calling object returns a positive number after the specified date.
int compareTo (Object obj) If obj is a date type, the operation is equivalent to CompareTo (date). Otherwise it throws classcastexception.
Boolean equals (Object date) Returns True when the Date object calling this method is equal to the specified date, otherwise false
Long GetTime () Returns the number of milliseconds that this Date object represents since January 1, 1970 00:00:00 GMT.
int Hashcode () Returns the hash code value for this object.
void SetTime (long time) Set the time and date for the number of milliseconds after 00:00:00 GMT from January 1, 1970.
String toString () Convert this Date object to the following form of String:dow mon dd hh:mm:ss zzz yyyy where: Dow is one of the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat).

**

Organize Java Basics--date & Time1

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.