一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例

Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例

2021-07-25 15:45白楊-M Java教程

這篇文章主要介紹了Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法,結合實例形式詳細分析了Java使用BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類進行數值運算與日期運算相關操作技巧,需要的朋友可以參考下

本文實例講述了java biginteger類,bigdecimal類,date類,dateformat類及calendar類用法。分享給大家供大家參考,具體如下:

biginteger類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package cn.itcast_01;
import java.math.biginteger;
/*
 * biginteger:可以讓超過integer范圍內的數據進行運算
 *
 * 構造方法:
 * biginteger(string val)
 */
public class bigintegerdemo {
  public static void main(string[] args) {
    // 這幾個測試,是為了簡單超過int范圍內,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);
    // 通過大整數來創建對象
    biginteger bi = new biginteger("2147483648");
    system.out.println("bi:" + bi);
  }
}

運行結果:

bi:2147483648

biginteger的運算方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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):返回商和余數的數組
 */
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):返回商和余數的數組
    biginteger[] bis = bi1.divideandremainder(bi2);
    system.out.println("商:" + bis[0]);
    system.out.println("余數:" + bis[1]);
  }
}

運行結果:

add:150
subtract:50
multiply:5000
divide:2
商:2
余數:0

bigdecimal類

不可變的、任意精度的有符號十進制數

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.itcast_01;
/*
 * 看程序寫結果:結果和我們想的有一點點不一樣,這是因為float類型的數據存儲和整數不一樣導致的。它們大部分的時候,都是帶有有效數字位。
 *
 * 由于在運算的時候,float類型和double很容易丟失精度,演示案例。所以,為了能精確的表示、計算浮點數,java提供了bigdecimal
 *
 * bigdecimal類:不可變的、任意精度的有符號十進制數,可以解決數據丟失問題。
 */
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的運算

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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):商,幾位小數,如何舍取
 */
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概述

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.itcast_01;
import java.util.date;
/*
 * date:表示特定的瞬間,精確到毫秒。
 *
 * 構造方法:
 *     date():根據當前的默認毫秒值創建日期對象
 *     date(long date):根據給定的毫秒值創建日期對象
 */
public class datedemo {
  public static void main(string[] args) {
    // 創建對象
    date d = new date();
    system.out.println("d:" + d);
    // 創建對象
    // 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

日期和毫秒值的相互轉換

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.itcast_02;
import java.util.date;
/*
 * public long gettime():獲取時間,以毫秒為單位
 * public void settime(long time):設置時間
 *
 * 從date得到一個毫秒值
 *     gettime()
 * 把一個毫秒值轉換為date
 *     構造方法
 *     settime(long time)
 */
public class datedemo {
  public static void main(string[] args) {
    // 創建對象
    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

是日期/時間格式化子類的抽象類,它以與語言無關的方式格式化并解析日期或時間。

Java BigInteger類,BigDecimal類,Date類,DateFormat類及Calendar類用法示例

date -- string(格式化)

string -- date(解析)

dateformat是抽象類,所以使用其子類simpledateformat

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
    // 創建日期對象
    date d = new date();
    // 創建格式化對象
    // 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

日期工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package cn.itcast_04;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;
/**
 * 這是日期和字符串相互轉換的工具類
 *
 * @author 風清揚
 */
public class dateutil {
  private dateutil() {
  }
  /**
   * 這個方法的作用就是把日期轉成一個字符串
   *
   * @param d
   *      被轉換的日期對象
   * @param format
   *      傳遞過來的要被轉換的格式
   * @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
   *      傳遞過來的要被轉換的格式
   * @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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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

測試來到世上多少天

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package cn.itcast_05;
import java.text.parseexception;
import java.text.simpledateformat;
import java.util.date;
import java.util.scanner;
/*
 * 算一下你來到這個世界多少天?
 *
 * 分析:
 *     a:鍵盤錄入你的出生的年月日
 *     b:把該字符串轉換為一個日期
 *     c:通過該日期得到一個毫秒值
 *     d:獲取當前時間的毫秒值
 *     e:用d-c得到一個毫秒值
 *     f:把e的毫秒值轉換為年
 *       /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();
    // 把該字符串轉換為一個日期
    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的毫秒值轉換為年
    long day = time / 1000 / 60 / 60 / 24;
    system.out.println("你來到這個世界:" + day + "天");
  }
}

calendar類

(1)日歷類,封裝了所有的日歷字段值,通過統一的方法根據傳入不同的日歷字段可以獲取值。

(2)如何得到一個日歷對象呢?

?
1
calendar rightnow = calendar.getinstance();

本質返回的是子類對象

(3)成員方法

a:根據日歷字段得到對應的值
b:根據日歷字段和一個正負數確定是添加還是減去對應日歷字段的值
c:設置日歷對象的年月日

(4)案例:

計算任意一年的2月份有多少天?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.itcast_01;
import java.util.calendar;
/*
 * calendar:它為特定瞬間與一組諸如 year、month、day_of_month、hour 等 日歷字段之間的轉換提供了一些方法,并為操作日歷字段(例如獲得下星期的日期)提供了一些方法。
 *
 * public int get(int field):返回給定日歷字段的值。日歷類中的每個日歷字段都是靜態的成員變量,并且是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方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package cn.itcast_02;
import java.util.calendar;
/*
 * public void add(int field,int amount):根據給定的日歷字段和對應的時間,來對當前的日歷進行操作。
 * 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日

獲取任意一年的二月有多少天

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package cn.itcast_03;
import java.util.calendar;
import java.util.scanner;
/*
 * 獲取任意一年的二月有多少天
 *
 * 分析:
 *     a:鍵盤錄入任意的年份
 *     b:設置日歷對象的年月日
 *       年就是a輸入的數據
 *       月是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程序設計有所幫助。

原文鏈接:https://www.cnblogs.com/baiyangyuanzi/p/6868281.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 涩涩国产精品福利在线观看 | 草莓视频旧版本 | 免费国产成人高清视频网站 | 鬼惨笑小说 | 波多野结衣之双方调教在线观看 | 日韩精品福利视频一区二区三区 | 欧美久久影院 | 男人天堂网页 | 2012在线观看免费视频大全 | 亚洲福利电影一区二区? | np高h疯狂黄暴宫口 narutomanga玖辛奈之乳 | 日本一级不卡一二三区免费 | www.av在线视频 | 国产成人久久精品一区二区三区 | 日韩在线视频免费观看 | 日韩经典在线观看 | 日本一卡二卡3卡四卡无卡网址 | 久久久久久久国产精品视频 | 美女做又爽又黄又猛 | 91理论片午午伦夜理片久久 | 亚洲香蕉综合在人在线视看 | 99福利视频导航 | 被夫上司侵犯了中文字幕 | 亚洲国产精品久久精品成人网站 | 女同久久另类99精品国产 | 欧美精品一区二区三区免费 | 99视频在线观看免费视频 | 欧美男同video | 国产福利一区二区三区四区 | 亚洲精品国产精品精 | 日本制服丝袜 | 日韩视频免费观看 | 免费国产网站 | 亚洲精品成人A8198A片漫画 | 婷婷色天使在线视频观看 | 冰雪奇缘1完整版免费观看 变形金刚第一部 | 亚洲+欧美+国产+综合 | 久久精品国产在热亚洲完整版 | 风间由美理论片在线观看 | 男人都懂www深夜免费网站 | 国产精品久久国产三级国电话系列 |