Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例
本文實(shí)例講述了Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法。分享給大家供大家參考,具體如下:
BigInteger類
發(fā)
package cn.itcast_01; import java.math.BigInteger; /* * BigInteger:可以讓超過Integer范圍內(nèi)的數(shù)據(jù)進(jìn)行運(yùn)算 * * 構(gòu)造方法: * BigInteger(String val) */ public class BigIntegerDemo { public static void main(String[] args) { // 這幾個(gè)測(cè)試,是為了簡(jiǎn)單超過int范圍內(nèi),Integer就不能再表示,所以就更談不上計(jì)算了。 // Integer i = new Integer(100); // System.out.println(i); // // System.out.println(Integer.MAX_VALUE); // Integer ii = new Integer("2147483647"); // System.out.println(ii); // // NumberFormatException // Integer iii = new Integer("2147483648"); // System.out.println(iii); // 通過大整數(shù)來創(chuàng)建對(duì)象 BigInteger bi = new BigInteger("2147483648"); System.out.println("bi:" + bi); } }
運(yùn)行結(jié)果:
bi:2147483648
BigInteger的運(yùn)算方法
package cn.itcast_02; import java.math.BigInteger; /* * public BigInteger add(BigInteger val):加 * public BigInteger subtract(BigInteger val):減 * public BigInteger multiply(BigInteger val):乘 * public BigInteger divide(BigInteger val):除 * public BigInteger[] divideAndRemainder(BigInteger val):返回商和余數(shù)的數(shù)組 */ public class BigIntegerDemo { public static void main(String[] args) { BigInteger bi1 = new BigInteger("100"); BigInteger bi2 = new BigInteger("50"); // public BigInteger add(BigInteger val):加 System.out.println("add:" + bi1.add(bi2)); // public BigInteger subtract(BigInteger val):減 System.out.println("subtract:" + bi1.subtract(bi2)); // public BigInteger multiply(BigInteger val):乘 System.out.println("multiply:" + bi1.multiply(bi2)); // public BigInteger divide(BigInteger val):除 System.out.println("divide:" + bi1.divide(bi2)); // public BigInteger[] divideAndRemainder(BigInteger val):返回商和余數(shù)的數(shù)組 BigInteger[] bis = bi1.divideAndRemainder(bi2); System.out.println("商:" + bis[0]); System.out.println("余數(shù):" + bis[1]); } }
運(yùn)行結(jié)果:
add:150
subtract:50
multiply:5000
divide:2
商:2
余數(shù):0
BigDecimal類
不可變的、任意精度的有符號(hào)十進(jìn)制數(shù)
package cn.itcast_01; /* * 看程序?qū)懡Y(jié)果:結(jié)果和我們想的有一點(diǎn)點(diǎn)不一樣,這是因?yàn)閒loat類型的數(shù)據(jù)存儲(chǔ)和整數(shù)不一樣導(dǎo)致的。它們大部分的時(shí)候,都是帶有有效數(shù)字位。 * * 由于在運(yùn)算的時(shí)候,float類型和double很容易丟失精度,演示案例。所以,為了能精確的表示、計(jì)算浮點(diǎn)數(shù),Java提供了BigDecimal * * BigDecimal類:不可變的、任意精度的有符號(hào)十進(jìn)制數(shù),可以解決數(shù)據(jù)丟失問題。 */ public class BigDecimalDemo { public static void main(String[] args) { System.out.println(0.09 + 0.01); System.out.println(1.0 - 0.32); System.out.println(1.015 * 100); System.out.println(1.301 / 100); System.out.println(1.0 - 0.12); } }
運(yùn)行結(jié)果:
0.09999999999999999
0.6799999999999999
101.49999999999999
0.013009999999999999
0.88
BigDecimal的運(yùn)算
package cn.itcast_02; import java.math.BigDecimal; /* * 構(gòu)造方法: * public BigDecimal(String val) * * public BigDecimal add(BigDecimal augend)加 * public BigDecimal subtract(BigDecimal subtrahend)減 * public BigDecimal multiply(BigDecimal multiplicand)乘 * public BigDecimal divide(BigDecimal divisor)除 * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,幾位小數(shù),如何舍取 */ public class BigDecimalDemo { public static void main(String[] args) { // System.out.println(0.09 + 0.01); // System.out.println(1.0 - 0.32); // System.out.println(1.015 * 100); // System.out.println(1.301 / 100); BigDecimal bd1 = new BigDecimal("0.09"); BigDecimal bd2 = new BigDecimal("0.01"); System.out.println("add:" + bd1.add(bd2)); System.out.println("-------------------"); BigDecimal bd3 = new BigDecimal("1.0"); BigDecimal bd4 = new BigDecimal("0.32"); System.out.println("subtract:" + bd3.subtract(bd4)); System.out.println("-------------------"); BigDecimal bd5 = new BigDecimal("1.015"); BigDecimal bd6 = new BigDecimal("100"); System.out.println("multiply:" + bd5.multiply(bd6)); System.out.println("-------------------"); BigDecimal bd7 = new BigDecimal("1.301"); BigDecimal bd8 = new BigDecimal("100"); System.out.println("divide:" + bd7.divide(bd8)); System.out.println("divide:" + bd7.divide(bd8, 3, BigDecimal.ROUND_HALF_UP)); System.out.println("divide:" + bd7.divide(bd8, 8, BigDecimal.ROUND_HALF_UP)); } }
運(yùn)行結(jié)果:
add:0.10
-------------------
subtract:0.68
-------------------
multiply:101.500
-------------------
divide:0.01301
divide:0.013
divide:0.01301000
Date類
Date概述
package cn.itcast_01; import java.util.Date; /* * Date:表示特定的瞬間,精確到毫秒。 * * 構(gòu)造方法: * Date():根據(jù)當(dāng)前的默認(rèn)毫秒值創(chuàng)建日期對(duì)象 * Date(long date):根據(jù)給定的毫秒值創(chuàng)建日期對(duì)象 */ public class DateDemo { public static void main(String[] args) { // 創(chuàng)建對(duì)象 Date d = new Date(); System.out.println("d:" + d); // 創(chuàng)建對(duì)象 // long time = System.currentTimeMillis(); long time = 1000 * 60 * 60; // 1小時(shí) Date d2 = new Date(time); System.out.println("d2:" + d2); } }
運(yùn)行結(jié)果:
d:Fri Mar 22 14:09:43 CST 2019
d2:Thu Jan 01 09:00:00 CST 1970
日期和毫秒值的相互轉(zhuǎn)換
package cn.itcast_02; import java.util.Date; /* * public long getTime():獲取時(shí)間,以毫秒為單位 * public void setTime(long time):設(shè)置時(shí)間 * * 從Date得到一個(gè)毫秒值 * getTime() * 把一個(gè)毫秒值轉(zhuǎn)換為Date * 構(gòu)造方法 * setTime(long time) */ public class DateDemo { public static void main(String[] args) { // 創(chuàng)建對(duì)象 Date d = new Date(); // 獲取時(shí)間 long time = d.getTime(); System.out.println(time); // System.out.println(System.currentTimeMillis()); System.out.println("d:" + d); // 設(shè)置時(shí)間 d.setTime(1000); System.out.println("d:" + d); } }
運(yùn)行結(jié)果:
1553235006473
d:Fri Mar 22 14:10:06 CST 2019
d:Thu Jan 01 08:00:01 CST 1970
DateFormat
是日期/時(shí)間格式化子類的抽象類,它以與語(yǔ)言無關(guān)的方式格式化并解析日期或時(shí)間。
Date -- String(格式化)
String -- Date(解析)
DateFormat是抽象類,所以使用其子類SimpleDateFormat
package cn.itcast_03; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /* * Date -- String(格式化) * public final String format(Date date) * * String -- Date(解析) * public Date parse(String source) * * DateForamt:可以進(jìn)行日期和字符串的格式化和解析,但是由于是抽象類,所以使用具體子類SimpleDateFormat。 * * SimpleDateFormat的構(gòu)造方法: * SimpleDateFormat():默認(rèn)模式 * SimpleDateFormat(String pattern):給定的模式 * 這個(gè)模式字符串該如何寫呢? * 通過查看API,我們就找到了對(duì)應(yīng)的模式 * 年 y * 月 M * 日 d * 時(shí) H * 分 m * 秒 s * * 2014年12月12日 12:12:12 */ public class DateFormatDemo { public static void main(String[] args) throws ParseException { // Date -- String // 創(chuàng)建日期對(duì)象 Date d = new Date(); // 創(chuàng)建格式化對(duì)象 // SimpleDateFormat sdf = new SimpleDateFormat(); // 給定模式 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss"); // public final String format(Date date) String s = sdf.format(d); System.out.println(s); //String -- Date String str = "2008-08-08 12:12:12"; //在把一個(gè)字符串解析為日期的時(shí)候,請(qǐng)注意格式必須和給定的字符串格式匹配 SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date dd = sdf2.parse(str); System.out.println(dd); } }
運(yùn)行結(jié)果:
2019年03月22日 14:11:01
Fri Aug 08 12:12:12 CST 2008
日期工具類
package cn.itcast_04; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; /** * 這是日期和字符串相互轉(zhuǎn)換的工具類 * * @author 風(fēng)清揚(yáng) */ public class DateUtil { private DateUtil() { } /** * 這個(gè)方法的作用就是把日期轉(zhuǎn)成一個(gè)字符串 * * @param d * 被轉(zhuǎn)換的日期對(duì)象 * @param format * 傳遞過來的要被轉(zhuǎn)換的格式 * @return 格式化后的字符串 */ public static String dateToString(Date d, String format) { // SimpleDateFormat sdf = new SimpleDateFormat(format); // return sdf.format(d); return new SimpleDateFormat(format).format(d); } /** * 這個(gè)方法的作用就是把一個(gè)字符串解析成一個(gè)日期對(duì)象 * * @param s * 被解析的字符串 * @param format * 傳遞過來的要被轉(zhuǎn)換的格式 * @return 解析后的日期對(duì)象 * @throws ParseException */ public static Date stringToDate(String s, String format) throws ParseException { return new SimpleDateFormat(format).parse(s); } }
運(yùn)行結(jié)果:
2019年03月22日 14:11:42
Fri Aug 08 12:12:12 CST 2008
package cn.itcast_04; import java.text.ParseException; import java.util.Date; /* * 工具類的測(cè)試 */ public class DateUtilDemo { public static void main(String[] args) throws ParseException { Date d = new Date(); // yyyy-MM-dd HH:mm:ss String s = DateUtil.dateToString(d, "yyyy年MM月dd日 HH:mm:ss"); System.out.println(s); String s2 = DateUtil.dateToString(d, "yyyy年MM月dd日"); System.out.println(s2); String s3 = DateUtil.dateToString(d, "HH:mm:ss"); System.out.println(s3); String str = "2014-10-14"; Date dd = DateUtil.stringToDate(str, "yyyy-MM-dd"); System.out.println(dd); } }
運(yùn)行結(jié)果:
2019年03月22日 14:12:18
2019年03月22日
14:12:18
Tue Oct 14 00:00:00 CST 2014
測(cè)試來到世上多少天
package cn.itcast_05; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner; /* * 算一下你來到這個(gè)世界多少天? * * 分析: * A:鍵盤錄入你的出生的年月日 * B:把該字符串轉(zhuǎn)換為一個(gè)日期 * C:通過該日期得到一個(gè)毫秒值 * D:獲取當(dāng)前時(shí)間的毫秒值 * E:用D-C得到一個(gè)毫秒值 * F:把E的毫秒值轉(zhuǎn)換為年 * /1000/60/60/24 */ public class MyYearOldDemo { public static void main(String[] args) throws ParseException { // 鍵盤錄入你的出生的年月日 Scanner sc = new Scanner(System.in); System.out.println("請(qǐng)輸入你的出生年月日:"); String line = sc.nextLine(); // 把該字符串轉(zhuǎn)換為一個(gè)日期 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date d = sdf.parse(line); // 通過該日期得到一個(gè)毫秒值 long myTime = d.getTime(); // 獲取當(dāng)前時(shí)間的毫秒值 long nowTime = System.currentTimeMillis(); // 用D-C得到一個(gè)毫秒值 long time = nowTime - myTime; // 把E的毫秒值轉(zhuǎn)換為年 long day = time / 1000 / 60 / 60 / 24; System.out.println("你來到這個(gè)世界:" + day + "天"); } }
Calendar類
(1)日歷類,封裝了所有的日歷字段值,通過統(tǒng)一的方法根據(jù)傳入不同的日歷字段可以獲取值。
(2)如何得到一個(gè)日歷對(duì)象呢?
Calendar rightNow = Calendar.getInstance();
本質(zhì)返回的是子類對(duì)象
(3)成員方法
A:根據(jù)日歷字段得到對(duì)應(yīng)的值
B:根據(jù)日歷字段和一個(gè)正負(fù)數(shù)確定是添加還是減去對(duì)應(yīng)日歷字段的值
C:設(shè)置日歷對(duì)象的年月日
(4)案例:
計(jì)算任意一年的2月份有多少天?
package cn.itcast_01; import java.util.Calendar; /* * Calendar:它為特定瞬間與一組諸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等 日歷字段之間的轉(zhuǎn)換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。 * * public int get(int field):返回給定日歷字段的值。日歷類中的每個(gè)日歷字段都是靜態(tài)的成員變量,并且是int類型。 */ public class CalendarDemo { public static void main(String[] args) { // 其日歷字段已由當(dāng)前日期和時(shí)間初始化: Calendar rightNow = Calendar.getInstance(); // 子類對(duì)象 // 獲取年 int year = rightNow.get(Calendar.YEAR); // 獲取月 int month = rightNow.get(Calendar.MONTH); // 獲取日 int date = rightNow.get(Calendar.DATE); System.out.println(year + "年" + (month + 1) + "月" + date + "日"); } }
運(yùn)行結(jié)果:
2019年3月22日
Clander的add和set方法
package cn.itcast_02; import java.util.Calendar; /* * public void add(int field,int amount):根據(jù)給定的日歷字段和對(duì)應(yīng)的時(shí)間,來對(duì)當(dāng)前的日歷進(jìn)行操作。 * public final void set(int year,int month,int date):設(shè)置當(dāng)前日歷的年月日 */ public class CalendarDemo { public static void main(String[] args) { // 獲取當(dāng)前的日歷時(shí)間 Calendar c = Calendar.getInstance(); // 獲取年 int year = c.get(Calendar.YEAR); // 獲取月 int month = c.get(Calendar.MONTH); // 獲取日 int date = c.get(Calendar.DATE); System.out.println(year + "年" + (month + 1) + "月" + date + "日"); // // 三年前的今天 // c.add(Calendar.YEAR, -3); // // 獲取年 // year = c.get(Calendar.YEAR); // // 獲取月 // month = c.get(Calendar.MONTH); // // 獲取日 // date = c.get(Calendar.DATE); // System.out.println(year + "年" + (month + 1) + "月" + date + "日"); // 5年后的10天前 c.add(Calendar.YEAR, 5); c.add(Calendar.DATE, -10); // 獲取年 year = c.get(Calendar.YEAR); // 獲取月 month = c.get(Calendar.MONTH); // 獲取日 date = c.get(Calendar.DATE); System.out.println(year + "年" + (month + 1) + "月" + date + "日"); System.out.println("--------------"); c.set(2011, 11, 11); // 獲取年 year = c.get(Calendar.YEAR); // 獲取月 month = c.get(Calendar.MONTH); // 獲取日 date = c.get(Calendar.DATE); System.out.println(year + "年" + (month + 1) + "月" + date + "日"); } }
運(yùn)行結(jié)果:
2019年3月22日
2024年3月12日
--------------
2011年12月11日
獲取任意一年的二月有多少天
package cn.itcast_03; import java.util.Calendar; import java.util.Scanner; /* * 獲取任意一年的二月有多少天 * * 分析: * A:鍵盤錄入任意的年份 * B:設(shè)置日歷對(duì)象的年月日 * 年就是A輸入的數(shù)據(jù) * 月是2 * 日是1 * C:把時(shí)間往前推一天,就是2月的最后一天 * D:獲取這一天輸出即可 */ public class CalendarTest { public static void main(String[] args) { // 鍵盤錄入任意的年份 Scanner sc = new Scanner(System.in); System.out.println("請(qǐng)輸入年份:"); int year = sc.nextInt(); // 設(shè)置日歷對(duì)象的年月日 Calendar c = Calendar.getInstance(); c.set(year, 2, 1); // 其實(shí)是這一年的3月1日 // 把時(shí)間往前推一天,就是2月的最后一天 c.add(Calendar.DATE, -1); // 獲取這一天輸出即可 System.out.println(c.get(Calendar.DATE)); } }
運(yùn)行結(jié)果:
請(qǐng)輸入年份:
2019
28
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- JAVA基本類型包裝類 BigDecimal BigInteger 的使用
- Java你不了解的大數(shù)型BigInteger與BigDecimal類
- Java Big Number操作BigInteger及BigDecimal類詳解
- JAVA?biginteger類bigdecimal類的使用示例學(xué)習(xí)
- Java中BigInteger與BigDecimal類用法總結(jié)
- java數(shù)學(xué)類Math?BigInteger?BigDecimal使用介紹
- Java中的System類、BigInteger類和BigDecimal類詳解
- JavaAPI中BigInteger、BigDecimal的使用方法及應(yīng)用
相關(guān)文章
MyEclipse開發(fā)一個(gè)webservice接口
這篇文章主要為大家詳細(xì)介紹了MyEclipse開發(fā)一個(gè)webservice接口,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Java?多個(gè)文件生成zip包、下載zip包的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java?多個(gè)文件生成zip包、下載zip包,包括文件上傳,文件下載,多個(gè)文件打成zip包的操作代碼,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01java中Class類的基礎(chǔ)知識(shí)點(diǎn)及實(shí)例
在本篇文章里小編給大家分享了關(guān)于java中Class類的基礎(chǔ)知識(shí)點(diǎn)及實(shí)例內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-05-05Spring MVC配置雙數(shù)據(jù)源實(shí)現(xiàn)一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)的方法
這篇文章主要給大家介紹了關(guān)于Spring MVC如何配置雙數(shù)據(jù)源實(shí)現(xiàn)一個(gè)java項(xiàng)目同時(shí)連接兩個(gè)數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05SpringBoot詳解實(shí)現(xiàn)自定義異常處理頁(yè)面方法
SpringBoot是Spring全家桶的成員之一,是一種整合Spring技術(shù)棧的方式(或者說是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開發(fā)的腳手架2022-06-06SpringBoot讀取自定義配置文件方式(properties,yaml)
這篇文章主要介紹了SpringBoot讀取自定義配置文件方式(properties,yaml),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07