1.日期工具类
Date日期:java.util;此包下的类在jdk中代表工具类
import java.util.Date;导入类
该类官方定义如下:
public class Date extends object implements Serializable,cloneable,comparable{}
对于该类的构造函数,只有两个
Date();分配对象并对其进行初始化,以便它表示分配对象的时间,以最接近的毫秒为单位。
Date(long date);分配一个对象并对其进行初始化,以表示自称为“纪元”的标准时间;
Date d=new Date(); Date d1=new Date(long time);
//实例化 var d=new Date(); //使用Date类实例获取当前的系统使劲按戳 long System.out.println(d.getTime()); System.out.println(System.currentTimeMillis()); System.out.println(d.getYear()+1900); System.out.println(d.getMonth()+1); d.setTime(0); System.out.printf("%tF %小案例:计算一个人活了多少天
var c1=Calendar.getInstance();//now的时间 c1.set(1788,1-1,24);//出生的时间,月份要减一, long days=System.currentTimeMillis()-c1.getTimeInMillis();//当前时间减去出生的时间 long ddd=days/(1000*60*60*24);// System.out.println(ddd);LocalDate
LocalDate表示了一个确切的日期,该对象值是不可变的,用起来和LocalTime基本一致。另外要注意的是这些对象是不可变的,操作返回的总是一个新实例。
//java.time.format.DateTimeFormatter DateTimeFormatter fmt=DateTimeFormatter.ofpattern("yyyy-MM-dd"); //将字符串根据格式转换为LocalDate对象 LocalDate dd=LocalDate.parse(s,fmt);LocalDateTime
1、LocalDateTime,是LocalDate和LocalTime的合体。它同时表示了日期和时间,但不带有时区 信息,可以直接创建,也可以通过合并日期和时间对象构造。
2、LocalDateTime和LocalTime还有LocalDate一样,都是不可变的。LocalDateTime提供了一些 能访问具体字段的方法
LocalDateTime n1=LocalDateTime.now(ZoneId.of("+08:00")); System.out.println(n1); LocalDateTime n2=LocalDateTime.now(); System.out.println(n2); DateTimeFormatter ff=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); System.out.println(n2.format(ff)); //获取秒数 long second=LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")); //获取毫秒数 long milliSecond=LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); System.out.println(milliSecond); System.out.println(System.currentTimeMillis());LocalTime
LocalTime time= LocalTime.of(16,4,5); //格式化时 hh 12小时制 HH24小时制 DateTimeFormatter tt= DateTimeFormatter.ofPattern("HH:mm:ss"); System.out.println(time.format(tt)); System.out.println(time.plusHours(2).format(tt)); System.out.println(time.minusMinutes(15));案例:
//获得当月第一天 LocalDate now=LocalDate.now(); now=LocalDate.of(now.getYear(),now.getMonth().getValue(),1); System.out.printf("%tF%n",now); LocalDate nn=LocalDate.of(2022,3,20); System.out.printf("%tF%n",nn); LocalDate nn2=nn.minusDays(nn.getDayOfMonth()-1); System.out.printf("%tF%n",nn2); //获得当月最后一天 LocalDate now=LocalDate.now(); now=LocalDate.of(now.getYear(),now.getMonth().getValue(),1); System.out.printf("%tF%n",now); LocalDate nn=LocalDate.of(2022,3,20); System.out.printf("%tF%n",nn); LocalDate nn2=nn.minusDays(nn.getDayOfMonth()-1); System.out.printf("%tF%n",nn2); var two=LocalDate.now().with(TemporalAdjusters.lastDayOfMonth()); System.out.println(two); var two2=LocalDate.now().with(t -> t.with(DAY_OF_MONTH, t.range(DAY_OF_MONTH).getMaximum())); System.out.println(two2);2.随机工具类
java.util.Random类 java.util.uuID
//Math.random()随机小数大于0小于1 double System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()); System.out.println(Math.random()>.5?"yes":"no"); //产生5-10随机整数 for (int i = 0; i <20 ; i++) { int a=(int)Math.round(Math.random()*6); System.out.println(a); } //随机字符串 String vid= UUID.randomUUID().toString(); System.out.println(vid);3.Math工具类
System.out.println(Math.pow(2,3));//8.0 System.out.println(Math.round(1.4));//1 System.out.println(Math.round(1.5));//2 System.out.println(Math.floor(1.4));//1.0舍掉小数 System.out.println(Math.floor(1.5));//1.0 System.out.println(Math.ceil(1.4));//2.0小数必须入整 System.out.println(Math.ceil(1.5));//2.0 System.out.println(Math.max(10,20));//20最大值 System.out.println(Math.min(10,20));//10最小值