JAVA打怪之路 - Java常用类 -日期和时间、Java比较器、数学公式

Java常用类 - 日期和时间、Java比较器、数学公式

一、日期时间API

在这里插入图片描述
① java.lang.System类 JDK8之前
在这里插入图片描述
② java.util.Date类 JDK8之前
在这里插入图片描述
③ java.text.SimpleDateFormat类 ( 格式化和解析日期的具体类 )

在这里插入图片描述

Date date = new Date(); // 产生一个Date实例
// 产生一个formater格式化的实例
SimpleDateFormat formater = new SimpleDateFormat();
System.out.println(formater.format(date));// 打印输出默认的格式
SimpleDateFormat formater2 = new SimpleDateFormat("yyyy年MM月dd日 EEE HH:mm:ss");
System.out.println(formater2.format(date));
try {
    // 实例化一个指定的格式对象
    Date date2 = formater2.parse("2008年08月08日 星期一 08:08:08");
    // 将指定的日期解析后格式化按指定的格式输出
    System.out.println(date2.toString());
} catch (ParseException e) {
    e.printStackTrace();
}

④ java.util.Calendar(日历)类 JDK8之前
在这里插入图片描述
⑤ JDK8新日期时间API

JDK8以前日期时间API的局限性:
在这里插入图片描述
5.1 LocalDate、LocalTime、LocalDateTime

说明:LocalDate、LocalTime、LocalDateTime 类是其中较重要的几个类,它们的实例是不可变的对象,分别表示使用 ISO-8601日历系统的日期、时间、日期和时间。它们提供了简单的本地日期或时间,并不包含当前的时间信息,也不包含与时区相关的信息。

在这里插入图片描述

5.2 Instant (瞬时)
在这里插入图片描述

5.3 java.time.format.DateTimeFormatter 类
在这里插入图片描述
5.4 其他类
在这里插入图片描述
5.5 与传统日期处理的转换
在这里插入图片描述

二、Java比较器

① 自然排序:java.lang.Comparable
在这里插入图片描述
在这里插入图片描述

class Goods implements Comparable {
    private String name;
    private double price;
    //按照价格,比较商品的大小
    @Override
    public int compareTo(Object o) {
        if(o instanceof Goods) {
            Goods other = (Goods) o;
            if (this.price > other.price) {
                return 1;
            } else if (this.price < other.price) {
                return -1;
            }
            return 0;
        }
        throw new RuntimeException("输入的数据类型不一致");
    }
    //构造器、getter、setter、toString()方法略
}
public class ComparableTest{
    public static void main(String[] args) {
        Goods[] all = new Goods[4];
        all[0] = new Goods("《红楼梦》", 100);
        all[1] = new Goods("《西游记》", 80);
        all[2] = new Goods("《三国演义》", 140);
        all[3] = new Goods("《水浒传》", 120);
        Arrays.sort(all);
        System.out.println(Arrays.toString(all));
    }
}

② 定制排序:java.util.Compartor
在这里插入图片描述

Goods[] all = new Goods[4];
all[0] = new Goods("War and Peace", 100);
all[1] = new Goods("Childhood", 80);
all[2] = new Goods("Scarlet and Black", 140);
all[3] = new Goods("Notre Dame de Paris", 120);
Arrays.sort(all, new Comparator() {
    @Override
    public int compare(Object o1, Object o2) {
        Goods g1 = (Goods) o1;
        Goods g2 = (Goods) o2;
        return g1.getName().compareTo(g2.getName());
    }
});
System.out.println(Arrays.toString(all));

三、System 类
在这里插入图片描述
在这里插入图片描述

四、Math 类
在这里插入图片描述

六、BigInteger与BigDecimal 类

BigInteger

在这里插入图片描述
在这里插入图片描述

BigDecimal 类
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值