Java学习

Java学习,第1张

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);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/735516.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-04-27
下一篇2022-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存