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 list = new ArrayList<>();
        list.add("aaaa");
        list.add("bbbb");
        list.add("cccc");
        list.add("dddd");
        Iterator 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);
        }
    }

你可能感兴趣的:(2021-04-05)