Java----常用类:日期相关类、数学运算相关类

常用类

日期相关类

Date类     日期函数

//2012-12-12 00:00:00- 1970-01-01 00:00:00 = 毫秒差:

Date date = new pate();   //得到系统的当前时间
System.out.println(date);

//默认情况下,日期的输出格式,采用是:格林尼治时间EEE MMM dd HH:mm:ss zzz yyyy 
//toGMTString(),日期的输出格式,使用格林威治时间格式: dd MMM yyyy HH:mm:ss zzz
//我们中国人,更多的是熟悉这个格式的时间: yyyy-MM-dd hh:mm:ss EEE
//2018-09-18 13:57:23星期二+lnldate).

Date date02 = new Date();//当前时间
System.out.println(date02.before(date));

 

Date类未过时的方法表

  

  

 boolean after(Date when)

测试此日期是否在指定日期之后

 boolean before(Date when)

测试此日期是否在指定日期之前

long getTime()

返回自 1970 年 1 月 1 日 00:00:00 GMT 以来此 Date 对象表示的毫秒数

 void setTime(long time)

设置此 Date 对象,以表示 1970 年 1 月 1 日 00:00:00 GMT 以后 time 毫秒的时间点

//我们中国人,更多的是熟悉这个格式的时间: vyyy-MM-dd hh:mm:ss SSS DDD EEE
 //2018-09-18 13:57:23星期二
Date date = new Date();
System.out.println(date);


SimpleDateFormat df = new SimpleDateFormat ("yyyy-M-dd HH:mm:ss E");
df. applyPattern ("yyyy-MM-dd HH:mm:ss E");//应用模板格式


//将日期对象,转换为符合格式的字符串数据
System.out.println(df. format (date));

模式字母及含义

日期或时间元素

示例

G

公元

公元

y

1996、 96

M

年中的月份

一月、二月

w

年中的周数

27

W

月份中的周数

2

D

年中的天数

189

d

月份中的天数

10

F

月份中的星期

2

E

星期中的天数

星期五

a

上午/下午 标记

AM/PM

H

一天中的小时数(0-23)

0

k

一天中的小时数(1-24)

24

K

am/pm 中的小时数(0-11)

0

h

am/pm 中的小时数(1-12)

12

m

小时中的分钟数

30

s

分钟中的秒数

55

S

毫秒数

978

z

时区

GMT-08:00

Z

时区

-0800

 

数学运算相关类

Math类

 System.out.println(Math.abs(-1));//取得一个数的绝对值!

System.out.println(Math.cbrt(63));
System.out.println(Math.ceil(Math.cbrt(63));
System.out.println(Math.floor(Math.cbrt(63)));
Systen.out.println(Math.pow(2, 8));//计算某一个值得N次方

Systen.out.println((int)(Math.random()*8)+2);//取值2—10之间的随机值.

System. out. println(Math.rint(-3.5)); //返回最接近参数并等于某一整数的double值。如果两个同为整数的double值都同样接近,那么结果取偶数。
System.out.println(Math.round(-3.5));//(long)Math.floor(a+ 0.5d)   四舍五入

System.out.println(Math.sqrt(4));//计算某一个值的正平方根

 

随机数

在Java中,产生随机数的方法有三种:

通过System.currentTimeMillis()来获取一个当前时间毫秒数的long型数字,我们可以把这个数字当作随机数。

通过Math类的静态方法random()返回一个0到1之间的double值。我们可以将这个值乘以一定的数,比如100,那么它就是一个100以内的随机数。

通过Random类来产生一个随机数。


//相同的种子,得到的随机序列也相同,一般用于模拟上次的随机值
Random random02 = new Random(100);
System.out.println(random02.nextInt());
System. 
out.println(random02.nextInt ());
System.out.println(random02.nextInt());

 

Random random03 = new Random(100);
System.out.println(random03.nextInt());
System. 
out.println(random03.nextInt ());
System.out.println(random03.nextInt());

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值