好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java BigInteger类,BigDecimal类,Date类,DateFormat类及Cal

本文实例讲述了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

是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或时间。

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 0 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://HdhCmsTestcnblogs测试数据/baiyangyuanzi/p/6868281.html

查看更多关于Java BigInteger类,BigDecimal类,Date类,DateFormat类及Cal的详细内容...

  阅读:17次