javase personal garbage review notes 07 Date class (if you want to use it in the future, check the api document hahaha)

Use the current date and time to initialize the object.
The
second constructor of Date() receives a parameter, which is the number of milliseconds since January 1, 1970.
Date(long millisec) After the
Date object is created, you can call the following methods.
1 boolean after(Date date)
If the Date object that calls this method returns true after the specified date, otherwise it returns false.
2 boolean before(Date date)
returns true if the Date object calling this method is before the specified date, otherwise it returns false.
3 Object clone()
returns a copy of this object.
4 int compareTo(Date date)
compares the Date object when this method is called with the specified date. Return 0 when the two are equal. The calling object returns a negative number before the specified date. The calling object returns a positive number after the specified date.
5 int compareTo(Object obj)
If obj is Date type, the operation is equivalent to compareTo(Date). Otherwise it throws ClassCastException.
6 boolean equals(Object date)
returns true when the Date object calling this method is equal to the specified date, otherwise it returns false.
7 long getTime()
returns the number of milliseconds represented by this Date object since January 1, 1970 00:00:00 GMT.
8 int hashCode()
Returns the hash code value of this object.
9 void setTime(long time)

Set the time and date in time milliseconds since 00:00:00 GMT on January 1, 1970.
10 String toString()
converts this Date object into a String of the following form: dow mon dd hh:mm:ss zzz yyyy where: dow is the day of the week (Sun, Mon, Tue, Wed, Thu, Fri, Sat) .
example:

import java.util.Date;
  
public class DateDemo {
    
    
   public static void main(String args[]) {
    
    
       // 初始化 Date 对象
       Date date = new Date();
        
       // 使用 toString() 函数显示日期时间
       System.out.println(date.toString());
   }
}

以上实例编译运行结果如下:

Mon May 04 09:51:52 CDT 2013

Use SimpleDateFormat to format dates

import  java.util.*;
import java.text.*;
 
public class DateDemo {
    
    
   public static void main(String args[]) {
    
    
 
      Date dNow = new Date( );
      SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss");
 
      System.out.println("当前时间为: " + ft.format(dNow));
   }
}

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

The compilation and running results of the above examples are as follows:

The current time is: 2018-09-06 10:16:34

Formatting dates using printf The
printf method can easily format time and date. Use the two-letter format, which starts with %t and ends with one of the letters in the table below.

import java.util.Date;
 
public class DateDemo {
    
    
 
  public static void main(String args[]) {
    
    
     // 初始化 Date 对象
     Date date = new Date();
 
     //c的使用  
    System.out.printf("全部日期和时间信息:%tc%n",date);          
    //f的使用  
    System.out.printf("年-月-日格式:%tF%n",date);  
    //d的使用  
    System.out.printf("月/日/年格式:%tD%n",date);  
    //r的使用  
    System.out.printf("HH:MM:SS PM格式(12时制):%tr%n",date);  
    //t的使用  
    System.out.printf("HH:MM:SS格式(24时制):%tT%n",date);  
    //R的使用  
    System.out.printf("HH:MM格式(24时制):%tR",date);  
  }
}
/*
全部日期和时间信息:星期一 九月 10 10:43:36 CST 2012  
年-月-日格式:2012-09-10  
月/日/年格式:09/10/12  
HH:MM:SS PM格式(12时制):10:43:36 上午  
HH:MM:SS格式(24时制):10:43:36  
HH:MM格式(24时制):10:43 

Java sleep (sleep)
sleep() makes the current thread enter a stagnant state (blocking the current thread), giving up the use of the CPU, the purpose is to prevent the current thread from occupying the CPU resources obtained by the process alone, so as to leave a certain time for other threads to execute Opportunity.

import java.util.*;
  
public class SleepDemo {
    
    
   public static void main(String args[]) {
    
    
      try {
    
     
         System.out.println(new Date( ) + "\n"); 
         Thread.sleep(1000*3);   // 休眠3秒
         System.out.println(new Date( ) + "\n"); 
      } catch (Exception e) {
    
     
          System.out.println("Got an exception!"); 
      }
   }
}
/*
Thu Sep 17 10:20:30 CST 2015
Thu Sep 17 10:20:33 CST 2015

Measuring time
The following example shows how to measure time intervals (in milliseconds)

import java.util.*;
  
public class DiffDemo {
    
    
 
   public static void main(String args[]) {
    
    
      
         long start = System.currentTimeMillis( );
         System.out.println(new Date( ) + "\n");
         Thread.sleep(5*60*10);
         System.out.println(new Date( ) + "\n");
         long end = System.currentTimeMillis( );
         long diff = end - start;
         System.out.println("Difference is : " + diff);
     
   }
}
/*
Fri Jan 08 09:48:47 CST 2016

Fri Jan 08 09:48:50 CST 2016

Difference is : 3019

Calendar class
Create a Calendar object representing the current date of the system

Calendar c = Calendar.getInstance();//The default is the current date

Create a Calendar object
with a specified date. To use the Calendar class to represent a specific time, you need to create a Calendar object first, and then set the year, month, and day parameters in the object to complete.

//Create a Calendar object representing June 12, 2009
Calendar c1 = Calendar.getInstance();
c1.set(2009, 6-1, 12);

Calendar c1 = Calendar.getInstance();
// 获得年份
int year = c1.get(Calendar.YEAR);
// 获得月份
int month = c1.get(Calendar.MONTH) + 1;
// 获得日期
int date = c1.get(Calendar.DATE);
// 获得小时
int hour = c1.get(Calendar.HOUR_OF_DAY);
// 获得分钟
int minute = c1.get(Calendar.MINUTE);
// 获得秒
int second = c1.get(Calendar.SECOND);
// 获得星期几(注意(这个与Date类是不同的):1代表星期日、2代表星期1、3代表星期二,以此类推)
int day = c1.get(Calendar.DAY_OF_WEEK);

Example:

import java.util.*;
  
public class GregorianCalendarDemo {
    
    
 
   public static void main(String args[]) {
    
    
      String months[] = {
    
    
      "Jan", "Feb", "Mar", "Apr",
      "May", "Jun", "Jul", "Aug",
      "Sep", "Oct", "Nov", "Dec"};
      
      int year;
      // 初始化 Gregorian 日历
      // 使用当前时间和日期
      // 默认为本地时间和时区
      GregorianCalendar gcalendar = new GregorianCalendar();
      // 显示当前时间和日期的信息
      System.out.print("Date: ");
      System.out.print(months[gcalendar.get(Calendar.MONTH)]);
      System.out.print(" " + gcalendar.get(Calendar.DATE) + " ");
      System.out.println(year = gcalendar.get(Calendar.YEAR));
      System.out.print("Time: ");
      System.out.print(gcalendar.get(Calendar.HOUR) + ":");
      System.out.print(gcalendar.get(Calendar.MINUTE) + ":");
      System.out.println(gcalendar.get(Calendar.SECOND));
      
      // 测试当前年份是否为闰年
      if(gcalendar.isLeapYear(year)) {
    
    
         System.out.println("当前年份是闰年");
      }
      else {
    
    
         System.out.println("当前年份不是闰年");
      }
   }
}

/*运行实例 »
以上实例编译运行结果如下:

Date: Apr 22 2009
Time: 11:25:27
当前年份不是闰年

Guess you like

Origin blog.csdn.net/qq_45864370/article/details/108546970