2021-04-05

[TOC]

1.时间日期类

java.util.Date

    //构造方法
    public static void main(String[] args) {
        Date date1 = new Date();
        System.out.println(date1);  //当前时间
        Date date2 = new Date(0L);
        System.out.println(date2);  //Thu Jan 01 08:00:00 CST 1970
        Date date3 = new Date(3600000L);
        System.out.println(date3);  //Thu Jan 01 09:00:00 CST 1970
    }
    //getTime() && setTime()
    public static void main(String[] args) {
        Date date1 = new Date();
        Long time1 = date1.getTime();
        System.out.println(time1);  //获取date对象毫秒值

        date1.setTime(0L);  //设置date对象毫秒值
        System.out.println(date1);  //Thu Jan 01 08:00:00 CST 1970
    }

java.text.SimpleDateFormat

    //format()格式化
    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String result1 = sdf.format(date);   //2021-04-04 11:03:53
        System.out.println(result1);
    }
    //parse()解析
     public static void main(String[] args) throws ParseException {
        String s = "2021-04-04 11:03:53";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(s);   //Sun Apr 04 11:03:53 CST 2021
        System.out.println(date);
    }

java.time.LocalDateTime(1.8新增)

    public static void main(String[] args) throws ParseException {
        //of() && now()
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);    //2021-04-04T21:31:23.521
        LocalDateTime localDateTime = LocalDateTime.of(2020, 11, 11, 11, 11, 11);
        System.out.println(localDateTime);  //2020-11-11T11:11:11
        //getDayOfMonth()
        int dayOfMonth = localDateTime.getDayOfMonth();
        System.out.println(dayOfMonth); //11
        //toLocalDate()转换为年月日
        LocalDate localDate = localDateTime.toLocalDate();
        //toLocalTime()转换为时分秒
        LocalTime localTime = localDateTime.toLocalTime();
        System.out.println(localDate); //2020-11-11
        System.out.println(localTime); //11:11:11
    }
    public static void main(String[] args) {
        //format
        LocalDateTime localDateTime = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String s = localDateTime.format(dateTimeFormatter);
        System.out.println(s);
        //parse
        String s1 = "2021-04-04 21:52:00";
        LocalDateTime parse = LocalDateTime.parse(s1, dateTimeFormatter);
        System.out.println(parse);
    }
     public static void main(String[] args) {
        //plus && minus && with
        LocalDateTime localDateTime = LocalDateTime.now();
        LocalDateTime newlocalDateTime1 = localDateTime.plusYears(1);
        LocalDateTime newlocalDateTime2 = localDateTime.minusYears(1);
        LocalDateTime newlocalDateTime3 = localDateTime.withYear(2099);
        //Period.between 两个日期相差的天数
        Period period = Period.between(newlocalDateTime1.toLocalDate(), newlocalDateTime3.toLocalDate());
        System.out.println(period); //P-79Y
        System.out.println(period.getYears()); //79
        //Duration.between 两个日期相差的秒数
        Duration duration = Duration.between(newlocalDateTime1, newlocalDateTime3);
        System.out.println(duration); //PT674976H
        System.out.println(duration.getSeconds()); //2429913600
    }

2.异常

异常体系

graph TD;
    Throwable-->Error;
    Throwable-->Exception;
    Exception-->RuntimeException及其子类;
    Exception-->除RuntimeException之外的所有异常
    

3.集合

集合类体系

graph TD;   
    集合-->|单列| Collection;
    集合-->|单列| Map;
    Collection-->|可重复| List;
    Collection-->|不可重复| Set;
    Map-->HashMap;
    Map-->TreeMap;
    List-->ArrayList;
    List-->LinkedList;
    Set-->HashSet;
    Set-->TreeSet;
    style ArrayList fill:white;
    style LinkedList fill:white;
    style HashSet fill:white;
    style TreeMap fill:white;
    style HashMap fill:white;
    style TreeSet fill:white;
    subgraph  
        接口
        实现类
        style 实现类 fill:white;
    end

collection常见成员方法

方法名 说明
boolean add(E e) 添加元素
boolean remove(Object o) 从集合中移除指定元素
boolean removeif(Object o) 根据条件进行删除
void clear() 清空集合
boolean contains(Object o) 判断集合是否存在指定元素
boolean isEmpty() 判断集合是否为空
int size 集合长度

迭代器与增强for

    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("cccc");
        list.add("dddd");
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
            String s = it.next();
            if ("bbbb".equals(s)) {
                it.remove();
            }
        }
        System.out.println(list);
        for (String s : list) {
            System.out.println(s);
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 废话不多说,自己进入今天的主题 1、面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:...
    传奇内服号阅读 7,044评论 1 31
  • 转载 1、面向对象的特征有哪些方面? 答:面向对象的特征主要有以下几个方面: - 抽象:抽象是将一类对象的共同特征...
    Nathan_Yang阅读 5,415评论 0 1
  • JAVASE进阶 [toc] 1.内部类 1.成员内部类 如何创建内部类的对象public class Start...
    250deb66c358阅读 1,319评论 0 0
  • String 类的概述  String类: 代表 字符串。Java 程序中的所有字符串字面值(如 "abc" )...
    是小猪童鞋啦阅读 2,885评论 0 0
  • Java 语言支持的类型分为两类:基本类型和引用类型。整型(byte 1, short 2, int 4, lon...
    xiaogmail阅读 5,171评论 0 10