生活随笔
收集整理的這篇文章主要介紹了
Java核心类库篇3——util
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java核心類庫篇3——util
1、Date
用于描述特定的瞬間,也就是年月日時分秒,可以精確到毫秒
1.1、構造方法
方法聲明功能介紹
| public Date() | 獲取當前時間表示的date對象 |
| public Date(long date) | 根據給定的毫秒值創建date對象 |
public class Test {public static void main(String[] args
) {Date date
=new Date();System.out
.println(date
);Date date1
= new Date(10000);System.out
.println(date1
);}
}
Sun Jul 11 16:17:00 CST 2021
Thu Jan 01 08:00:10 CST 1970
1.2、方法
方法聲明功能介紹
| public boolean after(Date when) | 測試指定日期是否在指定日期之后 |
| public boolean before(Date when) | 測試指定日期是否在指定日期之前 |
| public Objec clone() | 新建此對象的副本 |
| public int compareTo(Date anotherDate) | 比較兩個日期字典大小 |
| public long getTime() | 獲取調用對象距離1970年1月1日0時0分0秒的毫秒數 |
| public void setTime(long time) | 設置調用對象為距離基準時間time毫秒的時間點 |
public class Test {public static void main(String[] args
) {Date date
=new Date();System.out
.println(date
);Date date1
= new Date(10000);System.out
.println(date1
);System.out
.println(date
.after(date1
));System.out
.println(date
.compareTo(date1
));System.out
.println(date1
.getTime());}
}
Sun Jul 11 16:20:21 CST 2021
Thu Jan 01 08:00:10 CST 1970
true
1
10000
2、 SimpleDateFormat
用于實現日期和文本之間的轉換
2.1、構造方法
方法聲明功能介紹
| public SimpleDateFormat() | 用默認的格式和默認的語言環境構造 SimpleDateFormat |
| public SimpleDateFormat(String pattern) | 用指定的格式和默認的語言環境構造 SimpleDateFormat |
| public SimpleDateFormat(String pattern,Locale locale) | 用指定的格式和指定的語言環境構造 SimpleDateFormat |
public class Test {public static void main(String[] args
) {SimpleDateFormat simpleDateFormat
=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");Date date
= new Date();System.out
.println(simpleDateFormat
.format(date
));}
}
2021-07-11 04:23:26
2.2、方法
方法聲明功能介紹
| public Date parse(String source) | 用于將文本類型轉換為日期類型 |
public class Test {public static void main(String[] args
) throws ParseException {SimpleDateFormat simpleDateFormat
=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");String str
= "2021-07-11 04:23:26";Date date
= simpleDateFormat
.parse(str
);System.out
.println(date
);}
}
Sun Jul 11 04:23:26 CST 2021
3、Calendar
Calendar 不能直接創建對象,但可以使用靜態方法 getInstance() 獲得代表當前日期的日歷對象
3.1、構造方法
方法聲明功能介紹
| public static Calendar getInstance() | 用于獲取Calendar類型的引用 |
public class Test {public static void main(String[] args
) throws ParseException {Calendar calendar
= Calendar.getInstance();System.out
.println(calendar
);}
}
3.2、方法
方法聲明功能介紹
| public void set(int year, int month, int date, int hourOfDay, int minute, int second) | 用于設置年月日時分秒信息 |
| public Date getTime() | 用于將Calendar類型轉換為 Date類型 |
| public void set(int field, int value) | 設置指定字段的數值 void add(int field, int amount) 向指定字段增加數值 |
public class Test {public static void main(String[] args
) throws ParseException {Calendar calendar
= Calendar.getInstance();calendar
.set(2021,7,11,16,27,23);calendar
.set(Calendar.MONTH
,6);Date time
= calendar
.getTime();System.out
.println(time
);}
}
Sun Jul 11 16:27:23 CST 2021
4、LocalDate
用于描述年-月-日格式的日期信息,該類不表示時間和時區信息
4.1、構造方法
方法聲明功能介紹
| public static LocalDate now() | 在默認時區中從系統時鐘獲取當前日期 |
5、LocalTime
用于描述時間信息,可以描述時分秒以及納秒
方法聲明功能介紹
| public static LocalTime now() | 從默認時區的系統時間中獲取當前時間 |
| public static LocalTime now(ZoneId zone) | 獲取指定時區的當前時間 |
6、LocalDateTime
用于描述ISO-8601日歷系統中沒有時區的日期時間,如2007-12- 03T10:15:30
方法聲明功能介紹
| public static LocalDateTime now() | 從默認時區的系統時間中獲取 當前日期時間 |
| public static LocalTime now(ZoneId zone) | 獲取指定時區的當前時間 |
| public static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) | 根據參數指定的年月日時分秒 信息來設置日期時間 |
| public int getYear() | 獲取年份字段的數值 |
| public int getMonthValue() | 獲取1到12之間的月份字段 |
| public int getDayOfMonth() | 獲取日期字段 |
| public int getHour() | 獲取小時數 |
| public int getMinute() | 獲取分鐘數 |
| public int getSecond() | 獲取秒數 |
| public LocalDateTime withYear(int year) | 設置為參數指定的年 |
| public LocalDateTime withMonth(int month) | 設置為參數指定的月 |
| public LocalDateTime withDayOfMonth(int dayOfMonth) | 設置為參數指定的日 |
| public LocalDateTime withHour(int hour) | 設置為參數指定的時 |
| public LocalDateTime withMinute(int minute) | 設置為參數指定的分 |
| public LocalDateTime withSecond(int second) | 設置為參數指定的秒 |
| public LocalDateTime plusYears(long years) | 加上參數指定的年 |
| public LocalDateTime plusMonths(long months) | 加上參數指定的月 |
| public LocalDateTime plusDays(long days) | 加上參數指定的日 |
| public LocalDateTime plusHours(long hours) | 加上參數指定的時 |
| public LocalDateTime plusMinutes(long minutes) | 加上參數指定的分 |
| public LocalDateTime plusSeconds(long seconds) | 加上參數指定的秒 |
| public LocalDateTime minusYears(long years) | 減去參數指定的年 |
| public LocalDateTime minusMonths(long months) | 減去參數指定的月 |
| public LocalDateTime minusDays(long days) | 減去參數指定的日 |
| public LocalDateTime minusHours(long hours) | 減去參數指定的時 |
| public LocalDateTime minusMinutes(long minutes) | 減去參數指定的分 |
| public LocalDateTime minusSeconds(long seconds) | 減去參數指定的秒 |
7、Instant
用于描述瞬間的時間點信息
方法聲明功能介紹
| public static Instant now() | 從系統時鐘上獲取當前時間 |
| public OffsetDateTime atOffset(ZoneOffset offset) | 將此瞬間與偏移量組合以創建偏移日期時間 |
| public static Instant ofEpochMilli(long epochMilli) | 根據參數指定的毫秒數來構造對象,參數為距離1970年1月1 日0時0分0秒的毫秒數 |
| public long toEpochMilli() | 獲取距離1970年1月1日0時0分0秒的毫秒數 |
8、DateTimeFormatter
用于格式化和解析日期
方法聲明功能介紹
| public static DateTimeFormatter ofPattern(String pattern) | 根據參數指定的模式來獲取對象 |
| public String format(TemporalAccessor temporal) | 將參數指定日期時間轉換為字符串 |
| public TemporalAccessor parse(CharSequence text) | 將參數指定字符串轉換為日期時間 |
總結
以上是生活随笔為你收集整理的Java核心类库篇3——util的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。