Day04常用API&旧时代日期时间与新时代日期时间&Arrays

常用API

Math 

代表数学,是一个工具类,里面提供的都是对数据进行操作的一些静态方法 

Math类常用方法
    int abs(int a) 返回参数的绝对值
    double ceil(double a) 向上取整(不是四舍五入)
    double floor(double a) 向下取整(不是四舍五入)
    long round(double a) 四舍五入
    int max(int a,int b) 返回两个参数中的较大值
    int min(int a,int b) 返回两个参数中的较小值
    double pow(double a,double b) 返回a的b次幂
    double random() 返回[0.0,1.0)之间的随机正数
*/
public class Demo1 {
    public static void main(String[] args) {

        System.out.println(Math.abs(-5));  //5
        System.out.println(Math.ceil(5.5)); //6.0
        System.out.println(Math.floor(5.9)); //5.0
        System.out.println(Math.round(4.5)); //5
        System.out.println(Math.max(5,50)); //50
        System.out.println(Math.min(5,7)); //5
        System.out.println(Math.pow(2,3)); //8.0
        System.out.println(Math.random()); //0.23477365116743942


    }
}

Runtime 

代表程序所在的运行环境 

public class Demo3 {
    public static void main(String[] args) throws IOException {

        Runtime runtime = Runtime.getRuntime();
        
        Process exec = runtime.exec("D:\\Application\\wechat\\WeChat.exe");
        
        exec.destroy();//销毁对象
    }
}

BigDecimal 

用于解决浮点型运算时,出现结果失真的问题。 

 

 BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)  除法、可以控制精确到小数几位
        舍入模式
           RoundingMode.UP         进一
           RoundingMode.FLOOR      去尾
           RoundingMode.HALF_UP    四舍五入
*/
public class Demo4 {
    public static void main(String[] args) {

        BigDecimal a = BigDecimal.valueOf(10);
        BigDecimal b = BigDecimal.valueOf(3);

        System.out.println(a.add(b));
        System.out.println(a.subtract(b));
        System.out.println(a.multiply(b));
        //除不尽报错
        //System.out.println(a.divide(b));

        //
        System.out.println(a.divide(b,2, RoundingMode.UP));
    }
}

JDK8之前传统的日期、时间 

Date

 

SimpleDateFormat 

代表简单日期格式化,可以用来把日期对象、时间毫秒值格式化成我们想要的形式

使用步骤:
    1. 创建格式化器对象(指定格式) sdf
    2. 时间->字符串    字符串 sdf.format(时间)
    3. 字符串->时间    时间 sdf.format(字符串) 

 

public class Demo2 {
    public static void main(String[] args) throws ParseException {

      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      Date date = new Date();
      String format = sdf.format(date);
      System.out.println(format);

      Date parse = sdf.parse(format);
      System.out.println(parse);
      
    }
}

Calendar 

1. 代表的是系统此刻时间对应的日历。

2.通过它可以单独获取、修改时间中的年、月、日、时、分、秒等。

 

public class Demo3 {
    public static void main(String[] args) {
        //1. 创建日历对象   static Calendar getInstance()

        Calendar instance = Calendar.getInstance();
        //2. 获取: 年 月 日  时 分 秒   int get(int field) 获职日历中指定信息

        System.out.println(instance.get(Calendar.YEAR));
        System.out.println(instance.get(Calendar.MONTH)+1);
        System.out.println(instance.get(Calendar.DATE));
        //3. 获取日期对象 final Date getTime()

        Date time = instance.getTime();
        System.out.println(time);
        //4. 获取时间毫秒值 Long getTimeInMillis()

        long timeInMillis = instance.getTimeInMillis();
        System.out.println(timeInMillis);
        //5. 修改日期中的某个项 void set(int field,int value)

        instance.set(Calendar.YEAR,2033);
        System.out.println(instance.getTime());
        //6. 为某个信息增加/减少指定的值 void add(int field,int amount)
        instance.add(Calendar.MONTH,1);
        System.out.println(instance.getTime());
        instance.add(Calendar.HOUR,-3);
        System.out.println(instance.getTime());
    }
}

JDK8开始新增的日期、时间 

 

 

/*
三个类
    LocalDate:代表本地日期(年、月、日、星期)
    LocalTime:代表本地时间(时、分、秒、纳秒)
    LocalDateTime:代表本地日期、时间(年、月、日、星期、时、分、秒、纳秒)

创建方式
    Xxx.now() 获取时间信息
    Xxx.of(2025, 11, 16, 14, 30, 01)  设置时间信息

LocalDateTime转换为LocalDate和LocalTime
    public LocalDate toLocalDate()	转换成一个LocalDate对象
    public LocalTime toLocalTime()	转换成一个LocalTime对象

以LocalDate为例子演示常用方法
    获取细节: getYear、getMonthValue、getDayOfMonth、getDayOfYear、getDayOfWeek
    直接修改某个信息,返回新日期对象: withYear、withMonth、withDayOfMonth、withDayOfYear
    把某个信息加多少,返回新日期对象: plusYears、plusMonths、plusDays、plusWeeks
    把某个信息减多少,返回新日期对象: minusYears、minusMonths、minusDays,minusWeeks
    判断两个日期对象,是否相等,在前还是在后:equals isBefore isAfter
*/
public class Demo1 {
    public static void main(String[] args) {
        //1. 两种方式创建LocalDateTime

        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        //设置时间
        LocalDateTime of = LocalDateTime.of(2025, 9, 6, 5, 6, 7);
        System.out.println(of);
        //2. 使用LocalDateTime获取LocalDate和LocalTime

        System.out.println(now.toLocalDate());
        System.out.println(now.toLocalTime());
        //3. 获取细节: getYear、getMonthValue、getDayOfMonth、getDayOfYear、getDayOfWeek
        System.out.println("===============");

        System.out.println(now.getYear());
        System.out.println(now.getMonthValue());
        System.out.println(now.getDayOfMonth());
        System.out.println(now.getDayOfYear());
        System.out.println(now.getDayOfWeek().getValue());
        //4. 直接修改某个信息,返回新日期对象: withYear、withMonth、withDayOfMonth、withDayOfYear
        System.out.println("===============");
        System.out.println(now.withYear(2025));
        System.out.println(now.withMonth(7));
        System.out.println(now.withDayOfMonth(25));
        //5. 把某个信息加多少,返回新日期对象: plusYears、plusMonths、plusDays、plusWeeks

        System.out.println("===============");
        System.out.println(now.plusYears(10));
        System.out.println(now.plusMonths(10));
        System.out.println(now.plusWeeks(1));
        //6. 把某个信息减多少,返回新日期对象: minusYears、minusMonths、minusDays,minusWeeks

        System.out.println("===============");
        System.out.println(now.minusYears(10));
        System.out.println(now.minusMonths(10));
        System.out.println(now.minusDays(1));
        //7. 判断两个日期对象,是否相等,在前还是在后:equals isBefore isAfter
        System.out.println("===============");
        LocalDate of1 = LocalDate.of(2025, 10, 16);
        LocalDate now1 = LocalDate.now();
        System.out.println(of1.equals(now1));
        System.out.println(of1.isBefore(now1));
        System.out.println(of1.isAfter(now1));
    }
}

DateTimeFormatter 

/*
DateTimeFormatter:定义格式化时间的格式
    public static DateTimeFormatter ofPattern(时间格式) 	获取格式化器对象

LocalDateTime的方法
    public String format(DateTimeFormatter formatter)	时间转字符串
    public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter) 	字符串转时间
*/
public class Demo3 {
    public static void main(String[] args) {

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime now = LocalDateTime.now();
        String format = dateTimeFormatter.format(now);
        System.out.println(format);

        LocalDateTime parse = LocalDateTime.parse("2023-10-16 15:43:27", dateTimeFormatter);
        System.out.println(parse);
    }
}

Arrays 

  用来操作数组的一个工具类。

 

如果数组中存储的是对象该如何排序 

 1.自然排序:让该对象的类实现Comparable(比较规则)接口,重写compareTo方法,制定比较规则

/*
使用Arrays对对象进行排序
    方式1: 自然排序(本案例)
        1、实现Comparable接口,指定泛型
        2、重写compareTo方法
        3、指定排序规则
    排序规则
        返回正数,表示当前元素较大
        返回负数,表示当前元素较小
        返回0,表示相同
 */
public class Demo2 {
    public static void main(String[] args) {
        //1. 定义学生数组对象,保存四个学生进行测试
        Student[] students = new Student[4];
        students[0] = new Student("蜘蛛精", 169.5, 23);
        students[1] = new Student("紫霞", 163.8, 26);
        students[2] = new Student("紫霞", 163.8, 26);
        students[3] = new Student("至尊宝", 167.5, 24);

        //2. 指定排序的规则

        Arrays.sort(students);
        //3. 打印结果
        System.out.println(Arrays.toString(students));
    }
}

class Student implements Comparable<Student>{
    private String name;
    private double height;
    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 double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public Student(String name, double height, int age) {
        this.name = name;
        this.height = height;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }



    @Override
    public int compareTo(Student o) {
        return this.age - o.age;
    }
}

 比较器排序:使用下面这个sort方法,创建Comparator比较器接口的匿名内部类对象,制定比较规则

/*
使用Arrays对对象进行排序
    方式2: 比较器排序
        1、调用sort排序方法,参数二传递Comparator按口的实现类对象(匿名内部类实现)
        2、重写compare方法
        3、指定排序规则
    排序规则
        返回正数,表示当前元素较大
        返回负数,表示当前元素较小
        返回0,表示相同
 */
public class Demo3 {
    public static void main(String[] args) {
        //1. 定义学生数组对象,保存四个学生进行测试
        Teacher[] teachers = new Teacher[4];
        teachers[0] = new Teacher("蜘蛛精", 169.5, 23);
        teachers[1] = new Teacher("紫霞", 163.8, 26);
        teachers[2] = new Teacher("紫霞", 163.8, 26);
        teachers[3] = new Teacher("至尊宝", 167.5, 24);

        //2. 指定排序的规则

        Arrays.sort(teachers, new Comparator<Teacher>() {
            @Override
            public int compare(Teacher o1, Teacher o2) {
                return o1.getAge() - o2.getAge();
            }
        });
        //3. 打印结果

        System.out.println(Arrays.toString(teachers));
    }
}

class Teacher{
    private String name;
    private double height;
    private int age;

    public Teacher(String name, double height, int age) {
        this.name = name;
        this.height = height;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值