1. Date類處理日期(不常用的方法)
java.util.Date類主要是表示特定的時間,以毫秒爲單位。
1.1 構造器
1.1.1 不帶參數構造器
public Date() //構造一個Date對象,實質調用System.currentTimeMillis()
輸入:
public class Run {
public static void main(String[] args) {
Date d = new Date();
System.out.println(d);
}
}
輸出:
Tue Nov 14 19:33:05 CST 2017
1.1.2 帶參數構造器
public Date(long date) //構造的是從1970,1/1,08:00:00CST開始date毫秒
輸入:
public class Run {
public static void main(String[] args) {
Date d = new Date(1000);
System.out.println(d);
}
}
輸出:
Thu Jan 01 08:00:01 CST 1970
2.方法
大部分的方法都過時了。
2.1 after方法
public boolean after(Date when)
//Tests if this date is after the specified date.
2.2 before方法
public boolean before(Date when)
//Tests if this date is before the specified date.
2.3 clone方法
public Object clone()
//Return a copy of this date
2.4 compareTo方法
public int compateTo(Date anotherDate)
//Return 0 ,如果兩個日期相等
//Return <0,this Date is before the Date
//Return >0,this Date is after the Date
輸入:
public class Run {
public static void main(String[] args) {
Date d1 = new Date(1000);
Date d2 = new Date();
System.out.println(d2.compareTo(d1));
}
}
輸出:
1
2.5 equals方法
2.6 from方法
public static Date from(Instant instant)
//從 Instant對象獲取Date實例
2.7 getTime方法(重點方法)
該方法與System.currentTimeMillis()方法類似
public long getTime()
//返回從1970,1/1 00:00:00GMT到這個日期的毫秒
2.8 setTime方法
public void setTime(long time)
//設置此日期爲從1970,1/1 00:00:00GMT之後time毫秒
2.9 toInstant方法
public Instant toInstant()
//返回一個Instant對象代表Date對象同樣的時間節點
輸入:
public class Run {
public static void main(String[] args) {
Date d1 = new Date();
Instant i = d1.toInstant();
Date d2 = Date.from(i);
System.out.println(d1);
System.out.println(d2);
}
}
輸出:
Tue Nov 14 20:05:52 CST 2017
Tue Nov 14 20:05:52 CST 2017
2.Calendar類
java.util.Calendar類是一個抽象類,提供了日曆字段和一些時間處理的方法
構造器私有,利用Calendar.getInstant() 方法獲得Calendar對象
3.SimpleDateFormat(日期核心類)
java.text.SimpleDateFormat是一個用於轉換日期格式的類,是一個具體類。
3.1 構造器
3.1.1
//
public SimpleDateFormat()
public SimpleDateFormat(String pattern)
public SimpleDateFormat(String pattern,Locale locale)
public SimpleDateFormat(String pattern,DateFormatSymbols formatSymbols)
3.2 方法
一般就是設置日期格式,然後用format方法將日期轉化爲String,或者用parse將String轉化爲日期
3.2.1 applyPattern方法
public void applyPattern(String pattern)
應用日期的格式,關於日期的格式參考jdk java.text.SimpleDateFormat文檔
3.2.2 format方法
該類有該方法,但是一般用的是他的父類DateFormat的format方法,詳情查看文檔
public final String format(Date date)
//將日期轉換爲字符串
例如:
輸入:
public class Run {
public static void main(String[] args) {
Date d1 = new Date();
SimpleDateFormat s = new SimpleDateFormat();
//s.applyPattern("yyyy,MM,dd,HH,mm,ss,SS");
String d2 =s.format(d1);
System.out.println(d2);
}
}
輸出:
17-11-14 下午8:54
輸入:
public class Run {
public static void main(String[] args) {
Date d1 = new Date();
SimpleDateFormat s = new SimpleDateFormat();
s.applyPattern("yyyy,MM,dd,HH,mm,ss,SS");
String d2 =s.format(d1);
System.out.println(d2);
}
}
輸出:
2017,11,14,20,56,52,624 //這裏用的就是上面設置的格式
3.2.3 parse方法
public Date parse(String source) throws ParseException
將字符串轉化爲日期
輸入:
public class Run {
public static void main(String[] args) throws ParseException {
String str ="2017-11-14";
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyy-MM-dd");
Date d =sdf.parse(str);
System.out.println(d);
}
}
輸出:
Tue Nov 14 00:00:00 CST 2017 //因爲上面只設置了年月日