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