【131天】尚学堂高淇Java300集视频精华笔记(65-66)
第65集:常用類Date類的使用JDk源碼分析
Date時間類(java.util.Date)
在標準Java類庫中包含一個Date類。它的對象表示一個特定的瞬間,精確到毫秒。
Date()分配一個Date對象,并初始化此對象為當前的日期和時間精確到毫秒。
public Date() {this(System.currentTimeMillis());}Date(long?date)?分配?Date?對象并初始化此對象,以表示自從標準基準時間(稱為“歷元(epoch)”,即?1970?年?1?月?1?日?00:00:00?GMT)以來的指定毫秒數。?
/*** Allocates a <code>Date</code> object and initializes it to* represent the specified number of milliseconds since the* standard base time known as "the epoch", namely January 1,* 1970, 00:00:00 GMT.** @param date the milliseconds since January 1, 1970, 00:00:00 GMT.* @see java.lang.System#currentTimeMillis()*/public Date(long date) {fastTime = date; }boolean?after(Date?when)?測試此日期是否在指定日期之后。
public boolean after(Date when) {return getMillisOf(this) > getMillisOf(when); }boolean?before(Date?when)??測試此日期是否在指定日期之前。
public boolean before(Date when) {return getMillisOf(this) < getMillisOf(when); }Boolean?equals(Object?obj)?比較兩個日期的相等性。
public boolean equals(Object obj) {return obj instanceof Date && getTime() == ((Date) obj).getTime(); }Long?getTime()返回自?1970?年?1?月?1?日?00:00:00?GMT?以來此?Date?對象表示的毫秒數。
public long getTime() {return getTimeImpl(); }private final long getTimeImpl() {if (cdate != null && !cdate.isNormalized()) {normalize();}return fastTime; }void setTime(long time)返回
public void setTime(long time) {fastTime = time;cdate = null; }String?toString()?把此?Date?對象轉換為以下形式的?String:dow?mon?dd?hh:mm:ss?zzz?yyyy?其中:?dow?是一周中的某一天?(Sun,?Mon,?Tue,?Wed,?Thu,?Fri,?Sat)。?
MAC導入類的快捷鍵:command+shift+O
查看API文檔大家可以看到很多方法過時了,JDK1.1之前的Date包含了:日期操作、字符串轉化成時間對象、時間對象。1.1之后,日期操作使用:Canlendar類來,字符串轉化成為時間對象使用DateFormat。
示例代碼
public class Test065 {public static void main(String[] args){Date d = new Date();//得到當前時間的毫秒數long a = System.currentTimeMillis();System.out.println(d);System.out.println(a);Date d2 = new Date(1000);System.out.println(d2.toString());//自動加了時區System.out.println(d2.toGMTString());//標準格林尼治時間,已不建議使用d2.setTime(232323423);System.out.println(d2.getTime());System.out.println(d.getTime()<d2.getTime());//等同于beforeSystem.out.println(d.before(d2));System.out.println(d.getTime()>d2.getTime());//等同于afterSystem.out.println(d.after(d2));Date date1 = new Date();System.out.println(date1.toString());long i = date1.getTime();System.out.println(i);Date date2 = new Date(i-1000);Date date3 = new Date(i+1000);System.out.println(date1.after(date2));System.out.println(date1.before(date2));System.out.println(date1.equals(date2));System.out.println(date1.after(date3));System.out.println(date1.before(date3));System.out.println(date1.equals(date3));System.out.println(new Date(1000L*60*60*24*365*39L).toString());} }第66集:常用類DateFormat和SimpleDateFormat時間和字符串的互相轉換
DateFormat和SimpleDateFormat類的作用:將時間對象與指定格式的字符串進行互相轉化。
DateFormat是一個抽象類,一般使用其子類SimpleDateFormat類。
格式化字符含義
示例代碼
package com.test066;import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date;public class Test066 {public static void main(String[] args) {Date a = new Date(123124124L);System.out.println("直接輸出Date對象的結果:\t" + a);//System.out.println(a.toString()); DateFormat b = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String str = b.format(a);System.out.println("使用DateFormat類的format方法將Date對象處理成格式化字符串的結果:\t" + str);String str2 = "1977-7-7";DateFormat c = new SimpleDateFormat("yyyy-MM-dd");try {Date d = c.parse(str2);System.out.println("使用DateFormat類的parse方法將格式化字符串處理成Date對象的結果:\t" + d);} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}} }總結
以上是生活随笔為你收集整理的【131天】尚学堂高淇Java300集视频精华笔记(65-66)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 13.ThreadPoolExecuto
- 下一篇: java美元兑换,(Java实现) 美元