栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习

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 {
    
    @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日历类的使用
    
    @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的使用
    
    @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);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/839580.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号