一、Math、System、Runtime
Math代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法。
System代表程序所在的系统,也是一个工具类。
- 终止当前运行的Java虑拟机。status该参数用作状态代码;按照惯例,非零状态代码表示异常终止。
- 返回的是long类型的时间毫秒值:指的是从1970-1-1 0:0:0开始走到此刻的总的毫秒低:1s=1000ms。
- 了解:为啥选择“1970年1月1日00:00:00”作为时间的起点?
1969年8月,贝尔实验室的程序员肯汤普逊利用妻儿离开一个月的机会,开始着手创造一个全新的革命性的操作系统,他使用B编译语言在老旧的PDP-7机器上开发出了Unix的一个版本。随后,汤普逊和同事丹尼斯里奇改进了B语言,开发出了C语言,重写了UNIX。1970年1月1日 算C语言的生日
Runtime代表程序所在的运行环境,是一个单例类。
- 如果使用exec方法可以启动计算机内的程序,同时如果该程序配置过环境变量,可以直接使用环境变量名赋值启动的。使用destroy方法可以销毁对象。
二、BigDecimal
用于解决浮点型运算时,出现结果失真的问题
除法过程中可能会出现除不尽无法精确的效果,使用ROUNDMODE中的HALF.UP四舍五入。
三、JDK8之前传统的日期、时间的
1. Date
- 代表的是日期和时间。
long time= 1709092796232L;
Date d=new Date(time);
System.out.println(d);
//当前时间
Date d1=new Date();
System.out.println(d1);
//当前时间的毫秒数+时间差
long l = d1.getTime() + 1800 * 1000;
//使用获得的时间毫秒数创建日期对象的
Date d2=new Date(l);//d1.setTime(l);
System.out.println(d2);
2.SimpleDateFormat
代表简单日期格式化,可以用来把日期对象、时间毫秒值格式化成我们想要的形式
Date d1=new Date();
System.out.println(d1);
long l = d1.getTime() + 1800 * 1000;
Date d2=new Date(l);
System.out.println(d2);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");
System.out.println(simpleDateFormat.format(l));
System.out.println(simpleDateFormat.format(d2));
解析字符串时间成为日期对象
使用该方法会抛出异常
String dateStr="2022-12-12 12:12:11";
SimpleDateFormat simpleDateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date parse = simpleDateFormat1.parse(dateStr);
System.out.println(parse);
案例
String start="2023年11月11日 0:0:0";
String end="2023年11月11日 0:10:0";
String xj="2023年11月11日 0:01:18";
String xp="2023年11月11日 0:10:57";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date parse = simpleDateFormat.parse(start);
Date parse1 = simpleDateFormat.parse(end);
Date parse2 = simpleDateFormat.parse(xj);
Date parse3 = simpleDateFormat.parse(xp);
if(parse2.getTime()-parse.getTime()>=0 && parse2.getTime()-parse1.getTime()<=0)
System.out.println("xj成功购买");
else
System.out.println("xj没能秒杀");
if(parse3.getTime()-parse.getTime()>=0 && parse3.getTime()-parse1.getTime()<=0)
System.out.println("xp成功购买");
else
System.out.println("xp没能秒杀");
3.Calendar
日历对象可以简单对月操作,使用之前的方法很妈蛋的。
- 代表的是系统此刻时间对应的日历。
- 通过它可以单独获取、修改时间中的年、月、日、时、分、秒等。
注意:calendar是可变对象,一旦修改后其对象本身表示的时间将产生变化。
import java.util.Calendar;
import java.util.Date;
public class CalendarTest {
public static void main(String[] args) {
Calendar instance = Calendar.getInstance();
System.out.println(instance);
//打印日历对象,然后取值即可,month要+1,从0开始计数的哦。
System.out.println(instance.get(Calendar.YEAR));
System.out.println(instance.getWeekYear());
//拿到日历中记录的日期对象
Date time = instance.getTime();
System.out.println(time);
//拿到时间毫秒值
long timeInMillis = instance.getTimeInMillis();
System.out.println(timeInMillis);
//修改日历中的某个信息
instance.set(Calendar.YEAR, 2025);
System.out.println(instance);
//为某个信息增加或减少多少
instance.add(Calendar.DAY_OF_YEAR,100);
instance.add(Calendar.DAY_OF_YEAR,-10);
instance.add(Calendar.DAY_OF_MONTH,6);
instance.add(Calendar.HOUR, 12);
System.out.println(instance);
}
}
四、JDK8开始新增的日期、时间的
1.LocalDate LocalTime LocalDateTime
- Date对象很多方法都过时了,不合理,而且只能操作毫秒值和日期对象,Calendar还需要记住参数才能加减。
- 同时还是可变对象,一改原始时间就没了。丢失最开始的时间。
- 线程不安全,很多用户一起用时间对象,会出现bug
- 只能精确到毫秒,无法精确到纳秒
- 1秒=1000毫秒
- 1毫秒=1000微秒
- 1微秒=1000纳秒
- 在以后的项目中不推荐使用JDK8之前的API了,老项目里可能会用到,所以介绍一下。
- LocalDate:代表本地日期(年、月、日、星期)
- LocalTime:代表本地时间(时、分、秒、纳秒)
- LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)
LocalDate和LocalTime和LocalDateTime的API都差不多,只不过操作的成员不一样,最后一个获取的东西最多,修改用with 加用plus 剑用minus 判断equals isAfter isBefore的,使用of方法直接得到对象的。只不过可以拆分为前两个,前两个也可以合为一体。
//1. 获取本地日期对象
LocalDate now = LocalDate.now();
int year = now.getYear();
Month month = now.getMonth();//得到英文
int monthValue = now.getMonthValue();//得到中文
int dayOfMonth = now.getDayOfMonth();
int dayOfYear = now.getDayOfYear();
int dayOfWeek = now.getDayOfWeek().getValue();
System.out.println(year+"\t"+month+"\t"+monthValue+"\t"+dayOfMonth+"\t"+dayOfYear+"\t"+dayOfWeek);
//2. 不可变对象 直接修改某个信息withYear、withMonth、withDayofMonth、withDayofYear
LocalDate localDate = now.withYear(2099);
System.out.println(now+" "+localDate);
//3. 把某个信息加多少plusYears,plusMonths,plusDays,plusWeeks
LocalDate localDate1 = now.plusYears(2);
System.out.println(localDate1);
LocalDate localDate2 = now.plusMonths(2);
System.out.println(localDate2);
//4. 把某个信息减去多少minusYears,minusMonths,minusDays,minusWeeks
LocalDate localDate3 = now.minusYears(1);
System.out.println(localDate3);
//5. 获取指定日期的LocalDate对象,public static LocalDate of(int year,int month,dayOfMonth)
LocalDate localDate4 = LocalDate.of(2025, 12, 5);
LocalDate localDate5 = LocalDate.of(2025, 12, 6);
//6. 判断两个日期对象是否相等,在前还是在后,equals isBefore isAfter
System.out.println(localDate4.equals(localDate5));
System.out.println(localDate4.isAfter(localDate5));
System.out.println(localDate4.isBefore(localDate5));
//7. LocalDateTime可以转换成前面两种
//LocalDate ld = ldt.toLocalDate();
//LocalTIME lt = ldt.toLocalTime();
//LocalDateTime ldt10 =LocalDateTine.of(ld,lt);
2.ZoneID ZoneDateTime
由于世界各个国家与地区的经度不同,各地区的时间也有所不同,因此会划分为不同的时区。
Zoneld:代表时区ld
package com.API;
import java.time.Clock;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class ZoneIdTest {
public static void main(String[] args) {
//1.常见方法
//public static ZoneId systemDefault():获取系统默认的时区
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
System.out.println(zoneId.getId());
//public static Set<String>getAvailableZoneIds(): 获取Java支持的全部时区Id
System.out.println(ZoneId.getAvailableZoneIds());
//public static oneId of(String zoneId):把某个时区id封装成ZoneId对象。
ZoneId zoneId1 =ZoneId.of("America/New_York");
//2. ZonedDateTime:带时区的时间。
//public static zonedDateTime now(ZoneId zone): 获取菜个时区的ZonedDateTime对象。
ZonedDateTime now = ZonedDateTime.now(zoneId1);
System.out.println(now);
//世界标准时间
ZonedDateTime now1 = ZonedDateTime.now(Clock.systemUTC());
System.out.println(now1);
//public static ZonedDateTime now():获取系统默认时区的ZonedDateTime对象
ZonedDateTime now2 = ZonedDateTime.now();
System.out.println(now2);
//System.out.println(Calendar.getInstance(TimeZone.getTimeZone(zoneId1)));
}
}
3.Instant
Instant 时间线上的某个时刻/时间戳
- 通过获取Instant的对象可以拿到此刻的时间,该时间由两部分组成:
- 从1970-01-01 00:00:00 开始走到此刻的总秒数
- +不够1秒的纳秒数
- 作用:可以用来记录代码的执行时间,或用于记录用户操作某个事件的时间点
- 传统的Date类,只能精确到毫秒,并且是可变对象;
- 新增的Instant类,可以精确到纳秒,并且是不可变对象,推荐用Instant代替Date。
- LocalDateTime不能拿到总秒数
package com.API;
import java.time.Instant;
public class InstanceTest {
public static void main(String[] args) {
//1、创建Instant的对象,获取此刻时间信息
Instant now = Instant.now();
// 2、获取总秒数
long epochSecond = now.getEpochSecond();
System.out.println(epochSecond);
// 3、不够1秒的纳秒数
int nano = now.getNano();
System.out.println(nano);
System.out.println(now);
Instant plus = now.plusNanos(111);
// Instant对象的作用:做代码的性能分析,或者记录用户的操作时间点。
Instant now1 = Instant.now();
//代码执行
Instant now2 = Instant.now();
}
}
4.DateTimeFormatter
package com.API;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class TimeFormatterTest {
public static void main(String[] args) {
//1.创建一个日期时间格式化器对象出来
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:dd");
//2.对时间进行格式化
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//3.格式化操作
String format = dateTimeFormatter.format(now);//正向格式化,用格式化器格式化时间
System.out.println(format);
//4.格式化时间,其实还有一种方案
String format1 = now.format(dateTimeFormatter);//反向格式化,用时间格式化格式化器
System.out.println(format1);
//5.解析时间:解析时间一般使用LocalDateTime提供的解析方案来解决
String s = "2024-02-28 13:46:28";
LocalDateTime parse = LocalDateTime.parse(s,dateTimeFormatter);
System.out.println(parse);
}
}
5.Period Duration
Period:可以用于计算两个 LocalDate对象 相差的年数、月数、天数。
package com.API;
import java.time.LocalDate;
import java.time.Period;
public class PeriodTest {
public static void main(String[] args) {
LocalDate now = LocalDate.now();
System.out.println(now);
LocalDate end = LocalDate.of(2024, 5, 4);
System.out.println(end);
Period between = Period.between(now, end);
int years = between.getYears();
int days = between.getDays();
int months = between.getMonths();
System.out.println(years+"\t"+months+"\t"+days);
}
}
Duration:可以用于计算两个 LocalDate对象 相差的年数、月数、天数可以用于计算两个时间对象相差的天数、小时数、分数、秒数、纳秒数;支持LocalTime、LocalDateTime、lInstant等时间。
LocalDateTime localDateTime = LocalDateTime.of(2025, 11, 11, 11, 10, 10);
LocalDateTime localDateTime1 = LocalDateTime.of(2025, 11, 11, 11, 11, 11);
Duration between1 = Duration.between(localDateTime, localDateTime1);
System.out.println(between1.toDays());
System.out.println(between1.toHours());
System.out.println(between1.toMinutes());
System.out.println(between1.toSeconds());
System.out.println(between1.toMillis());
System.out.println(between1.toNanos());
6.Arrays
用来操作数组的一个工具类。
package com.API;
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;
public class ArraysTest {
public static void main(String[] args) {
//1、public staticString toString(类型[]arr):返回数组的内容
int[] arr={10,20,30,40,50};
System.out.println(Arrays.toString(arr));
//2、public static int[]copyOfRange(类型[]arr,起始索引,结束案引):拷贝数组(指定范围,包前不包后)
int[] arr1 = Arrays.copyOfRange(arr, 1, 4);
System.out.println(Arrays.toString(arr1));
//3、public static copy0f(类型[]arr,int newLength):拷贝数组,可以指定新数组的长度。
int[] arr2 = Arrays.copyOf(arr, 10);
int[] arr3 = Arrays.copyOf(arr, 3);//截断
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
//4、public static setAll(double[]array,IntToDoubleFunction generator):把数组中的原数据改为新数据又存进去
double[] d= {99.8, 128, 100};
Arrays.setAll(d, new IntToDoubleFunction() {
@Override
public double applyAsDouble(int value) {
//value=0,1,2
return d[value]*0.8;
}
});
System.out.println(Arrays.toString(d));
//5、public static void sort(类型[]arr):对数组进行排序(默认是升序排序)
Arrays.sort(d);
System.out.println(Arrays.toString(d));
}
}
- 方式一:让该对象的类实现Comparable(比较规则)接口,然后重写compareTo方法,自己来制定比较规则。
- 约定1:认为左边对象大于右边对象请您返回正整数
- 约定2:认为左边对象小于右边对象请您返回整数
- 约定3:认为左边对象等于右边对象请您一定返回0
return this.age-o.age;
- 方式二:使用下面这个sort方法,创建Comparator比较器接口的匿名内部类对象,然后自己制定比较规则。
- public static void sort(T arr,Comparator<?super T>c)对数组进行排序(支持自定义排序规则)
package com.API;
import java.util.Arrays;
import java.util.Comparator;
public class ArrayTest1 {
public static void main(String[] args) {
//2、public static <T> void sort(T[] arr,Comparator<? super T>)
// 参数一:需要排序的数组
//参数二:Comparator比较器对象(用来制定对象的比较规购)
Student[] students =new Student[4];
students[0]=new Student("蜘姝精",23);
students[1]=new Student( "紫假", 26);
students[2]=new Student( "紫霞",26);
students[3]=new Student( "至蓉宝",24);
//1、public static void sort(类型[]arr):对数组进行排序。
Arrays.sort(students);
System.out.println(Arrays.toString(students));
Arrays.sort(students, new Comparator<Student>() {
@Override
public int compare(Student o1,Student o2){
// return o1.getAge()-o2.getAge();
return Integer.compare(o1.getAge(),o2.getAge());
//return Double.compare(o1.getHeight(),o2.getHeight());
}
});
System.out.println(Arrays.toString(students));
}
}
class Student implements Comparable<Student>{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public int compareTo(Student o) {
return this.age-o.age;
}
}