Java中Stringbuild,Date和Calendar類的用法詳解
Stringbuild類
由于String類的對象內(nèi)容不可改變,每次拼接都會構(gòu)建一個新的String對象,既耗時,又浪費內(nèi)存空間
這時需要通過java提供的StringBuild類解決這個問題
StringBuilder又稱為可變字符序列,它是一個類似于 String 的字符串緩沖區(qū),可以看作是一個容器,容器中可以裝很多字符串
可變指的是StringBuilder對象中的內(nèi)容是可變的
構(gòu)造方法
public StringBuilder()
: 創(chuàng)建一個空的緩沖區(qū)
public StringBuilder(String srt)
: 創(chuàng)建一個存儲了str的緩沖區(qū)
//public StringBuilder():創(chuàng)建一個空白可變字符串對象,不含有任何內(nèi)容 StringBuilder sb = new StringBuilder(); System.out.println("sb:" + sb); System.out.println("sb.length():" + sb.length()); //public StringBuilder(String str):根據(jù)字符串的內(nèi)容,來創(chuàng)建可變字符串對象 StringBuilder sb2 = new StringBuilder("hello"); System.out.println("sb2:" + sb2); System.out.println("sb2.length():" + sb2.length());
append
public StringBuilder append(Object obj)
: 向容器中追加任意類型數(shù)據(jù), 轉(zhuǎn)為字符串
// 鏈式編程, 鏈式編程返回結(jié)果 看最后調(diào)用的方法 StringBuilder abc = new StringBuilder(stringBuilder.append(10).append("abc").append(10.1).append(new Object()).toString()); System.out.println("abc = " + abc);
reverse
public StringBuilding reverse()
: 將緩沖區(qū)數(shù)據(jù)反轉(zhuǎn)
String string = new StringBuilder(abc).reverse().toString(); System.out.println(string);
Date類
java.util.Date
表示特定的瞬間,精確到毫秒
構(gòu)造方法
public Date()
: 當前日期對象, 從運行程序的時間到時間原點經(jīng)歷的毫秒值,轉(zhuǎn)換成Date對象,分配Date對象并初始化此對象,以表示分配它的時間(精確到毫秒)。
public Date(long date)
:將指定參數(shù)的毫秒值date,轉(zhuǎn)換成Date對象,分配Date對象并初始化此對象
時間原點: 1970年1月1日 00:00:00
中國處于東8區(qū) 嚴格來說是1970年1月1日 00:08:00
1s = 1000ms
public static void main(String[] args) { // 創(chuàng)建日期對象,把當前的時間 System.out.println(new Date()); // Tue Jan 16 14:37:35 CST 2020 // 創(chuàng)建日期對象,把當前的毫秒值轉(zhuǎn)成日期對象 System.out.println(new Date(0)); // Thu Jan 01 08:00:00 CST 1970 }
getTime
long getTime()
: 獲取日期對象的毫秒值
// 獲取從 時間原點 到 當前日期 的毫秒值 long time = nowTime.getTime(); System.out.println(time);
setTime
void setTime(long time)
: 設(shè)置毫秒值
// 設(shè)置偏移毫秒值為0, 即時間原點 nowTime.setTime(0); System.out.println(nowTime);
DateFormat
java.text.DateFormat
是日期/時間格式化子類的抽象類,我們通過這個類可以幫我們完成日期和文本之間的轉(zhuǎn)換,也就是可以在Date對象與String對象之間進行來回轉(zhuǎn)換。
SimpleDateFormat
由于DateFormat為抽象類,不能直接使用,所以需要常用的子類java.text.SimpleDateFormat
。
這個類需要一個模式(格式)來指定格式化或解析的標準。
構(gòu)造方法
public SimpleDateFormat()
: 用默認的模式和語言環(huán)境的日期格式符號構(gòu)造SimpleDateFormat。
默認格式為: (年)-(月)-(日) (上午/下午)xx:xx
public SimpleDateFormat(String pattern)
:用給定的模式和默認語言環(huán)境的日期格式符號構(gòu)造SimpleDateFormat。
參數(shù)pattern是一個字符串,代表日期時間的自定義格式。
常用的格式規(guī)則為:
標識字母(區(qū)分大小寫) | 含義 |
---|---|
y | 年 |
M | 月 |
d | 日 |
H | 時 |
m | 分 |
s | 秒 |
備注:更詳細的格式規(guī)則,可以參考SimpleDateFormat類的API文檔。
日期對象轉(zhuǎn)換為字符串
public String format(Date date)
: 傳遞日期對象,返回格式化后的字符串。
// 將當前日期 轉(zhuǎn)換為 x年x月x日 xx:xx:xx Date nowTime = new Date(); DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss E"); System.out.println(df.format(nowTime));
字符串轉(zhuǎn)換為日期對象
public Date parse(String source)
傳遞字符串,返回日期對象
// 獲取sDate所代表的時間的毫秒值 String sDate = "1949-10-01"; DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd"); // parse 若無法解析字符串會拋出異常 ParseException Date date = df2.parse(sDate); long time = date.getTime(); System.out.println(time);
Calendar類
java.util.Calendar
Calendar 日歷類, 替換掉了許多Date的方法
它是一個抽象類, 但提供了靜態(tài)方法創(chuàng)建對象, 同時也提供了很多靜態(tài)屬性
月份 0-11 代表 1-12月
國外每周的第一天是星期日
getInstance
public static Calendar getInstance()
:使用默認時區(qū)和語言環(huán)境獲得一個日歷。
Calendar c = Calendar.getInstance(); System.out.println(c);
靜態(tài)屬性及其對應(yīng)字段
使用類名.屬性名
調(diào)用,代表給定的日歷字段:
字段值 | 含義 |
---|---|
YEAR | 年 |
MONTH | 月(從0開始,可以+1使用) |
DAY_OF_MONTH | 月中的天(幾號) |
HOUR | 時(12小時制) |
HOUR_OF_DAY | 時(24小時制) |
MINUTE | 分 |
SECOND | 秒 |
DAY_OF_WEEK | 周中的天(周幾,周日為1,可以-1使用) |
get
int get(int field)
: 返回給定日歷字段的值
int year = c.get(Calendar.YEAR); // 0-11表示月份 需要+1 int month = c.get(Calendar.MONTH) + 1; // DATE 和 DAY_OF_MONTH 的值是一樣的 int day = c.get(Calendar.DAY_OF_MONTH); System.out.println(year+"年"+month+"月"+day+"日");
getTimeZone
TimeZone getTimeZone()
獲取時區(qū)
TimeZone timeZone = c.getTimeZone(); System.out.println(timeZone);
add
void add(int field, int amount)
: 根據(jù)日歷規(guī)則 為給定的字段添加或減去指定的時間量
// 將日歷設(shè)置為2000.5.1, 當前時間為2023.4.5 // add方法設(shè)置偏移量 c.add(Calendar.YEAR, -23); c.add(Calendar.MONTH, 1); c.add(Calendar.DATE, -4); System.out.println(c.get(Calendar.YEAR)+"."+(c.get(Calendar.MONTH) + 1)+"."+c.get(Calendar.DAY_OF_MONTH));
set
void set(int field, int value)
: 將給定的日歷字段設(shè)置為給定值
void set(int year, int month, int date)
直接設(shè)置年月日為指定值
// set(int field, int value)方法 將日歷設(shè)置為2001.4.2 c.set(Calendar.YEAR, 2001); c.set(Calendar.MONTH, 3); c.set(Calendar.DAY_OF_MONTH, 2); System.out.println(c.get(Calendar.YEAR)+"."+(c.get(Calendar.MONTH) + 1)+"."+c.get(Calendar.DAY_OF_MONTH)); // set(int year, int month, int date)方法 將日歷設(shè)置為2003.10.1 c.set(2003, 9, 1); System.out.println(c.get(Calendar.YEAR)+"."+(c.get(Calendar.MONTH) + 1)+"."+c.get(Calendar.DAY_OF_MONTH));
getTime
Date getTime()
: 將日歷對象轉(zhuǎn)為日期對象
Date date = c.getTime(); System.out.println(date);
練習
定義一個方法, 使用StringBuild將數(shù)組轉(zhuǎn)換為 [元素1,元素2...] 的格式
public class Demo { public static void main(String[] args) { int[] arr = {3,765,8234,1,23}; System.out.println(arrayConcatToSting(arr)); } public static String arrayConcatToSting(int[] arr) { StringBuilder stringBuilder1 = new StringBuilder("["); for (int i = 0; i < arr.length; i++) { stringBuilder1.append(arr[i]); if (i < arr.length - 1) { stringBuilder1.append(", "); } else if (i == arr.length - 1){ stringBuilder1.append("]"); } } return stringBuilder1.toString(); } }
計算一個人活了多少天
import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; public class Demo { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); System.out.print("請輸入您的生日(年.月.日): "); System.out.println("您活了"+howLongHaveYouLived(sc.nextLine())+"天"); } public static long howLongHaveYouLived (String str) throws ParseException { DateFormat df = new SimpleDateFormat("yyyy.MM.dd"); Date birthDay = df.parse(str); long birthDayTime = birthDay.getTime(); long nowTime = new Date().getTime(); return (nowTime - birthDayTime) / 1000 / 60 / 60 /24; } }
計算指定年份的2月有多少天
import java.util.Calendar; import java.util.Date; import java.util.Scanner; public class Demo { public static void main(String[] args) { // 計算指定年份的2月有多少天 Scanner sc = new Scanner(System.in); System.out.print("請輸入您要指定的年份: "); int inputYear = sc.nextInt(); System.out.println(inputYear+"年的2月有"+getDay(inputYear)+"天"); } public static int getDay(int year) { Calendar calendar = Calendar.getInstance(); calendar.set(year, 2, 1); calendar.add(Calendar.DATE, -1); return calendar.get(Calendar.DATE); } }
到此這篇關(guān)于Java中Stringbuild,Date和Calendar類的用法詳解的文章就介紹到這了,更多相關(guān)Java Stringbuild Date Calendar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot開發(fā)實戰(zhàn)系列之定時器
定時任務(wù)我想諸位童鞋都不陌生,簡而言之名為“設(shè)定定時鬧鐘做某件事情”,下面這篇文章主要給大家介紹了關(guān)于SpringBoot定時器的相關(guān)資料,需要的朋友可以參考下2021-08-08linux的shell命令檢測某個java程序是否執(zhí)行
ps -ef |grep java|grep2016-04-04