Java学习_Day 04(学习内容:尚硅谷常用类JAVA零基础P479-P485)

P479 常用类-Idea Debug调试

package com.commonClass;

import org.junit.Test;

public class IdeaDebug {
    @Test
    public void testStringBuffer(){
        String str = null;
        StringBuffer sb = new StringBuffer();
        sb.append(str);
        System.out.println(sb.length()); //4
        System.out.println(sb); //"null"
        StringBuffer sb1 = new StringBuffer(str); //空指针异常
        System.out.println(sb1);
    }
}

P480 常用类-SimpleDateFormat的使用

package com.commonClass;

import org.junit.Test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateTimeTest2 {
    /*
    JDK8之前的: SimpleDateFormat对日期Date类的格式化和解析
    1. 两个操作
    1.1 格式化:日期 --> 字符串
    1.2 解析  字符 --> 日期

    2. 实例化
    */
    @Test
    public void testSimpleDataFormat() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat();
        // 格式化日期  默认构造器
        Date date = new Date();
//        System.out.println(date);
        String format = sdf.format(date);
        System.out.println(format);
        // 解析
        String str = "22-4-24 上午10:53";
        Date date1 = sdf.parse(str);
        System.out.println(date1);

        System.out.println("***********************");
//        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyy.MMMMM.dd GGG hh:mm aaa");
        // 指定方式格式化和解析,调用带参构造器
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String format1 = sdf1.format(date);
        System.out.println(format1); //2022-04-24 11:05:04
        // 解析
        Date date2 = sdf1.parse("2022-04-24 11:05:04");
        System.out.println(date2);
    }
}

P481 常用类-SimpleDateFormat的课后练习1

    // 练习
    @Test
    public void testExer() throws ParseException {`在这里插入代码片`
        String birth = "2020-09-08";
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf1.parse(birth);
        System.out.println(date);
        java.sql.Date birthDate = new java.sql.Date(date.getTime());
        System.out.println(birthDate);
    }

P482 常用类-SimpleDateFormat的课后练习2

    // 练习 2 ”三天打鱼两天晒网“

P483 常用类-Calendar日历类的使用

    /*
    Calendar日历类/抽象类
     */
    @Test
    public void testCalendar(){
        // 1. 实例化
        // 方式一:创建子类对象
        // 方式二:调用静态方法
        Calendar calendar = Calendar.getInstance();
        // 2. 常用方法
        // get
        int days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
        // set
        calendar.set(Calendar.DAY_OF_MONTH,22);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        // add
        calendar.add(Calendar.DAY_OF_MONTH,3);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
        // getTime  日历类 --> date
        Date date = calendar.getTime();
        System.out.println(date);
        // setTime date --> 日历类
        Date date1 = new Date();
        calendar.setTime(date1);
        days = calendar.get(Calendar.DAY_OF_MONTH);
        System.out.println(days);
    }

P484 常用类-JDK8中日期时间API的介绍

克服了之前Date和Calendar类的一些缺点

package com.commonClass;

import org.junit.Test;

import java.util.Date;

public class JDK8DateTimeTest {
    @Test
    public void testDate(){
        // Date偏移性
        Date date1 = new Date(2020 - 1900,9 - 1,8);
        System.out.println(date1);
    }
}

P485 常用类-LocalDate、LocalTime、LocalDateTime的使用

    /*
    Java8时间API
     */
    @Test
    public void test1(){
        // 创建对象
        // now获取当前的日期、时间和日期+时间
        LocalDate localDate = LocalDate.now();
        LocalTime localTime = LocalTime.now();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDate);
        System.out.println(localTime);
        System.out.println(localDateTime);
        // 无需偏移量 设置指定年月日时分秒
        LocalDateTime localDateTime1 = LocalDateTime.of(2020, 10, 6, 12, 0, 11);
        System.out.println(localDateTime1);

        // get
        System.out.println(localDateTime.getDayOfMonth());
        System.out.println(localDateTime.getDayOfWeek());
        System.out.println(localDateTime.getMonth());
        System.out.println(localDateTime.getMonthValue());
        System.out.println(localDateTime.getMinute());

        // with
        // 不可变性
        LocalDate localDate1 = localDate.withDayOfMonth(22);
        System.out.println(localDate1);
        System.out.println(localDate);

        LocalDateTime localDateTime2 = localDateTime.withHour(4);
        System.out.println(localDateTime2);

        LocalDateTime localDateTime3 = localDateTime.plusMonths(3);
        System.out.println(localDateTime3);
        LocalDateTime localDateTime4 = localDateTime.minusDays(3);
        System.out.println(localDateTime4);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值