一.日期与时间类
JDK1.8之前的日期类
1.获取系统当前时间:long time = System.currentTimeMillis()
返回当前的时间距1970年1月1日0时0分0秒的差值以毫秒为单位,称为时间戳。
2…Java.util.Date类
Java.util包下,提供了一个Date类用于表示日期和时间,这是通常说的日期类。
在JDK1.8中,Date类只有两个构造方法可以使用:
① Date():创建当前日期的Date对象。
Date date = new Date();
System.out.println(date.toString()); //Fri Nov 19 20:49:52 CST 2021
② Date(long time):创建指定日期的Date对象,即时间戳。
Date date1 = new Date(1637246832369L);
System.out.println(date1); //Thu Nov 18 22:47:12 CST 2021
Date常用的方法:
toString() :格式的日期格式为YYYY-MM-DD
getTime():获取当前Date对象的毫秒数
3…java.sql.Date类
java.sql包下的Date类,对应着数据库中的日期类型的变量。
① 创建java.sql.Date对象
long date = 1637246832369L;
java.sql.Date date2 = new java.sql.Date(date);
System.out.println(date2); //2021-11-18
② sql.Date–>util.Date
Date date3 = new java.sql.Date(1637246832369L); //多态
System.out.println(date3); //2021-11-18
③ java.util.Date—>java.sql.Date
Date date = new Date();
java.sql.Date date4 = new java.sql.Date(date.getTime());
4…SimpleDateformat类
① 格式化:日期—>字符串,调用构造器SimpleDateFormat
//1.默认格式化
Date date = new Date(); //Fri Nov 19 22:13:29 CST 2021
SimpleDateFormat sdf = new SimpleDateFormat();
String s = sdf.format(date); //21-11-19 下午10:12
//2.指定格式化
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:s");
String s2 = sdf1.format(date);
System.out.println(s2); //2021-11-19 10:41:30
② 解析:字符串—>日期,调用parse()方法
//解析,根据格式化解析,要求字符串必须符合SimpleDateFormat识别的格式(构造器体现),否则异常:ParseException
String s1 = "21-11-19 下午10:13";
Date date1 = sdf.parse(s1); //parse()解析字符串以产生 Date
System.out.println(date1); //Fri Nov 19 22:13:00 CST 2021
//2.指定的格式化
Date date2 = sdf1.parse(s2);
System.out.println(date2); //Fri Nov 19 10:44:20 CST 2021
5…Calendar日历类
Calendar是一个抽象类,自身无法实例化,实例化有两种方式:
① 创建其子类GregorianCalendar的对象(不常用)
② 调用其静态方法Calendar.getInstance()
@Test
//Calendar日期类的创建以及常用方法使用说明
public void test01(){
Calendar calendar = Calendar.getInstance();//调用其静态方法getInstance()
System.out.println(calendar.getClass()); //class java.util.GregorianCalendar
//1.get(int field):返回给定日历字段的值。
int day1 = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day1); //20
int days = calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(days); //324
//2.set(int field, int value) 将给定的日历字段设置为给定的值
calendar.set(Calendar.DAY_OF_MONTH, 10); //set返回值类型为void,在原有的基础上修改
int day2 = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day2); //10
//3.add(int field, int amount) 添加或减去指定的时间给定日历领域,基于日历的规则。
calendar.add(Calendar.DAY_OF_MONTH,10);
int day3 = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day3); //20
//4.getTime() 返回表示这 Calendar时间价值的 Date对象
Date date = calendar.getTime();
System.out.println(date); //Sat Nov 20 10:08:11 CST 2021
//5.setTime(Date date) 设置日历的时间与给定的 Date。
Date date1 = new Date();
calendar.setTime(date1);
int day4 = calendar.get(Calendar.DAY_OF_MONTH);
System.out.println(day4); //20
}
注意:
获取月份时:一月是0,二月是1,以此类推,12月是11
获取星期时:周日是1,周二是2,。。。。周六是7
JDK 1.8以后的日期类
1.LocalDate、LocalTime、LocalDateTime类
优点:①不可变性 ②不偏移 ③线程安全
以上三个类实例化的两种方式:
① xxx.now():当前日期时间
② xxx.of():指定的日期时间
public class JDK8DateTest {
public static void main(String[] args) {
//实例化
//now:获取当前的时间
LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now(); //比较常用
System.out.println(localDate); //2021-11-20
System.out.println(localTime); //10:41:41.533
System.out.println(localDateTime); //2021-11-20T10:41:41.533
//of():设置指定的年月日,没有偏移
LocalDateTime localDateTime1 = LocalDateTime.of(2020, 12, 12, 12, 12);
System.out.println(localDateTime1); //2020-12-12T12:12
//getXxx():获取某个时间字段
System.out.println(localDateTime.getDayOfYear()); //324
System.out.println(localDate.getDayOfMonth()); //20
System.out.println(localDateTime.getDayOfWeek()); //SATURDAY
System.out.println(localDateTime.getHour()); //10
System.out.println(localDateTime.getMinute()); //48
//不可变性
//设置时间段
//1.withDayOfYear(int dayOfYear) 返回此日期的副本与一年的日期更改
LocalDateTime localDateTime2 = localDateTime.withDayOfYear(2);
System.out.println(localDateTime); //2021-11-20T10:54:38.648
System.out.println(localDateTime2); //2021-01-02T10:54:38.648
//修改时间段
//1.plusDays(long daysToAdd) 返回一份当前LocalDateTime加上指定的天数
LocalDateTime localDateTime3 = localDateTime.plusDays(8);
System.out.println(localDateTime); //2021-11-20T11:00:40.939
System.out.println(localDateTime3); //2021-11-28T11:00:40.939
//2.minusDays(long daysToSubtract)返回一份当前LocalDateTime减去指定的天数
LocalDateTime localDateTime4 = localDateTime.minusDays(10);
System.out.println(localDateTime); //2021-11-20T11:03:51.939
System.out.println(localDateTime4); //2021-11-10T11:03:51.939
}
}
2.Instant类:
功能类似于java.util.Date类
public class InstantTest {
public static void main(String[] args) {
Instant instant = Instant.now(); //now() 获取本初子午线对应的日期时间
System.out.println(instant); //2021-11-20T03:19:44.206Z
//instant.atOffset(ZoneOffset.ofHours(8))添加时间的偏移量
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));//此时获取的就是当前的东八区,北京时间
System.out.println(offsetDateTime); //2021-11-20T11:22:05.043+08:00
//toEpochMilli():获取1970-01-01-00:00:00至今对应的毫秒数
long milli = instant.toEpochMilli();
System.out.println(milli);
//ofEpochMilli(long mill):给定毫秒数,获取instant,类似于---> Date(long time)
Instant instant1 = instant.ofEpochMilli(1637378778297L);
System.out.println(instant1); //2021-11-20T03:26:18.297Z
}
}
3.DateTimeFormatter类
DateTimeFormatter继承与Object类,用于格式化或解析日期、时间,功能类似于SimpleDateFormat
三种格式化的方法:
① 预定义的标准格式
② 本地化相关的方式
③ 自定义的方式(重点)
public class DateTimeFormatterTest {
public static void main(String[] args) {
DateTimeFormatter isoDateTime = DateTimeFormatter.ISO_DATE_TIME;
//格式化1:默认格式化
LocalDateTime now = LocalDateTime.now();
String format = isoDateTime.format(now);
System.out.println(format); //2021-11-20T15:40:04.193
//解析
TemporalAccessor parse = isoDateTime.parse("2021-11-20T12:52:20.853");
System.out.println(parse); //{},ISO resolved to 2021-11-20T12:52:20.853
//格式化2:本地化相关的格式 如:ofLocalizedDate
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
String format1 = dateTimeFormatter.format(LocalDate.now());
System.out.println(format1); //2021年11月20日
String format2 = dateTimeFormatter.format(LocalDateTime.now());
System.out.println(format2); //2021年11月20日
//格式化2:本地化相关的格式化 如 :ofLocallizedDateTime
DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG);
String format3 = dateTimeFormatter1.format(LocalDateTime.now());
System.out.println(format3); //2021年11月20日 下午03时43分07秒
//格式化3:自定义格式化
DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");
String format4 = dateTimeFormatter2.format(LocalDateTime.now());
System.out.println(format4); //2021-11-20 03:43:07
//解析:
TemporalAccessor parse1 = dateTimeFormatter2.parse("2021-11-20 03:40:04");
System.out.println(parse1); //{HourOfAmPm=3, SecondOfMinute=4, NanoOfSecond=0, MinuteOfHour=40, MicroOfSecond=0, MilliOfSecond=0},ISO resolved to 2021-11-20
}
}
二.Java常用类之比较器
1.Comparable接口
实现Comparable接口,重写了compareTo()方法的排序称为自然排序,默认按照从小到到大排序。像String、包装类实现了Comparable接口,sort()方法就是自然排序。
对于自定义类,如果想实现自然排序,该类需要实现Comparable接口,重写compareTo()方法,在该方法内部指定排序规则
重写compareTo()方法规则:
① 若当前对象this大于形参对象obj,返回正数
② 若当前对象this小于形参对象obj,返回负数
③ 若当前对象this等于形参对象obj,返回0
2.Comparator接口
定制排序,一次性使用。
public class CompareTest {
public static void main(String[] args) {
Student[] stu = new Student[5];
stu[0] = new Student(21,"Tom");
stu[1] = new Student(19,"Jhon");
stu[2] = new Student(24,"Charry");
stu[3] = new Student(20,"Petter");
stu[4] = new Student(19,"Smith");
Arrays.sort(stu); //调用自然排序
System.out.println(Arrays.toString(stu));
Student[] stu1 = new Student[5];
stu1[0] = new Student(21,"Tom");
stu1[1] = new Student(19,"Jhon");
stu1[2] = new Student(24,"Charry");
stu1[3] = new Student(20,"Petter");
stu1[4] = new Student(19,"Smith");
//定制排序:按照学生姓名从低到高排序,相同时按年龄从低到高
Arrays.sort(stu1, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Student && o2 instanceof Student){
Student s1 = (Student)o1;
Student s2 = (Student)o2;
if(s1.getName().equals(s2.getName())){
return compare(s1.getAge(),s2.getAge());
}else{
return s1.getName().compareTo(s2.getName());
}
}
throw new RuntimeException("传入的数据类型不一致。");
}
});
System.out.println(Arrays.toString(stu1));
}
}
//自然排序时必须实现Comparable接口,可以多次调用重写的compare()方法
class Student implements Comparable{
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
= name;
}
public Student(){}
public Student(int age, String name) {
this.age = age;
= name;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
//自然排序:重写compareTo()方法
@Override
public int compareTo(Object o) {
if(o instanceof Student){
Student s = (Student) o;
if(this.age > s.age){
return 1;
}else if (this.age < s.age){
return -1;
}else{
return .compareTo(); //按照name从低到高排序
// return -.compareTo();//按照name从高到低排序
}
}
throw new RuntimeException("传入的数据类型不一致。");
}
}
三.System类
public final class Systemextends Object{}
System类包含一些有用的类的字段和方法。它不能被实例化。
包括标准输入、标准输出和错误输出流;访问外部定义的属性和环境变量;加载文件和库的方法;和一种快速复制数组的一部分的实用方法。
常用方法:
四.Math类
Math类包含用于执行基本的数字运算等基本指数、对数、平方根法、三角函数。 返回值类型一般为double类型。
五.Java高新技术——大数操作(BigInteger、BigDecimal)
Java高新技术——大数操作 可以使用BigInteger操作大整数
可以使用BigDecimal指定小数的保留位数
public class BigIntegerTest {
public static void main(String[] args) {
BigInteger bi1 = new BigInteger("1234567256585289") ; // 声明BigInteger对象
BigInteger bi2 = new BigInteger("9876542688948321") ; // 声明BigInteger对象
System.out.println("加法操作:" + bi2.add(bi1)) ; // 加法操作
System.out.println("减法操作:" + bi2.subtract(bi1)) ; // 减法操作
System.out.println("乘法操作:" + bi2.multiply(bi1)) ; // 乘法操作
System.out.println("除法操作:" + bi2.divide(bi1)) ; // 除法操作
System.out.println("最大数:" + bi2.max(bi1)) ; // 求出最大数
System.out.println("最小数:" + bi2.min(bi1)) ; // 求出最小数
BigInteger result[] = bi2.divideAndRemainder(bi1) ; // 求出余数的除法操作
System.out.println("商是:" + result[0] +
";余数是:" + result[1]) ;
}
}