Java常用类

目录

1.Math

2.Date

3.SimpleDateFormat

4.Calendar

5.LocalDate

6.localTime

7.localDateTime


1.Math

大家好,今天分享常用类的相关知识,开始之前先搞点文艺

                   浪淘沙·最怕问初衷

                             近现代·墨色(网名)

最怕问初衷,大梦成空。眉间鬓上老英雄,剑甲鞮鍪封厚土,说甚擒龙。

壮志付西风,逝去无踪。少年早作一闲翁,诗酒琴棋终日里,岁月匆匆​​​。

方法描述
Math.sqrt()求平方根
Math.pow(x,a)幂运算,x的a次方
Math.floorMod(x,y)求余,x%y
Math.sin()求正弦
Math.cos()求余弦
Math.tan()求正切
Math.abs()求绝对值
Math.max()两个数的最大值
Math.min()两个数最小值
Math.random()0.0到1.0的随机数

2.Date

方法描述
Date()生成代表当前时间的date对象
Date(long date)根据long类型参数获得date对象
after(Date when)判断是否在某个日期后
before(Date when)判断是否在某个日期后
getTime()获取时间毫秒数
setTime(long date)s
compareTo(Date date)比价两个日期大小

3.SimpleDateFormat

方法描述
SimpleDateFormat(String pattern)创建格式化对象
format(Date date)日期转字符串
parse(String date)字符串转日期

3.1示例

public class DateTest {
	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		System.out.println(date);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//时间转字符串
		String current = format.format(date);
		System.out.println(current);
		//字符串转时间
		String d = "2018-11-23 23:00:00";
		Date parse = format.parse(d);
		System.out.println(parse);
	}
}

 

4.Calendar

方法描述
getInstance()创建日期对象
get(int field)获取指定字段的值
add(int field,int amount)为指定的字段添加或者减去指定的时间量,会进位
roll(int field,int amount)为指定的字段添加或者减去指定的时间量,不会进位

5.LocalDate

方法描述
now()获取当前日期
of()设置日期
plusYears()/minusYears()增加/减少年份
plusMonths()/minusMonths()增加/减少月份
plusDays()/minusDays()增加/减少日
format()格式化日期
parse()字符串转LocalDate

6.localTime

方法描述
now()获取当前时间
of()设置时间
plusHours()/minusHours()增加/减少小时
plusMinutes()/minusMinutes()增加/减少分钟
plusSeconds()/minusSeconds()增加/减少秒
format()格式化时间
parse()字符串转LocalTime

 

7.localDateTime

方法描述
getYear()获取年份
getMonthValue()获取月份
getDayOfMonth()获取月份的第几天
parse()字符串转LocalDateTime对象
format()格式化日期
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值