学习韩顺平老师java基础笔记(自用)3.29_韩顺平笔记是否为闰年-程序员宅基地

技术标签: java  学习记录  

学习内容:

例如:

  1. System类
  2. BigInteger 和 BigDecimal 类
  3. 日期类

学习产出:

1. System类

1.1 System 类常见方法和案例

1) exit退出当前程序
2) arraycopy :复制数组元素,比较适合底层调用,一般使用
Arrays.copyOf完成复制数组.
int[] src={
    1,2,3};
int[] dest = new int[3];
System.arraycopy(src, 0, dest, 0, 3);
3) currentTimeMillens:返回当前时间距离1970-1-1的毫秒数
4) gc:运行垃圾回收机制System.gc0;

案例:

public static void main(String[] args) {
    
//exit 退出当前程序
// System.out.println("ok1");
// //1. exit(0) 表示程序退出
// //2. 0 表示一个状态 , 正常的状态
// System.exit(0);//
// System.out.println("ok2");
//arraycopy :复制数组元素,比较适合底层调用,
// 一般使用 Arrays.copyOf 完成复制数组
int[] src={
    1,2,3};
int[] dest = new int[3];// dest 当前是 {0,0,0}
//1. 主要是搞清楚这五个参数的含义
//2. // 源数组
// * @param src the source array. // srcPos: 从源数组的哪个索引位置开始拷贝
// * @param srcPos starting position in the source array. // dest : 目标数组,即把源数组的数据拷贝到哪个数组
// * @param dest the destination array. // destPos: 把源数组的数据拷贝到 目标数组的哪个索引
// * @param destPos starting position in the destination data. // length: 从源数组拷贝多少个数据到目标数组
// * @param length the number of array elements to be copied. System.arraycopy(src, 0, dest, 0, src.length);
// int[] src={1,2,3};
System.out.println("dest=" + Arrays.toString(dest));//[1, 2, 3]
//currentTimeMillens:返回当前时间距离 1970-1-1 的毫秒数
System.out.println(System.currentTimeMillis());
}

2.BigInteger 和 BigDecimal 类

2.1 BigInteger 和 BigDecimal 介绍

应用场景:

  1. BigInteger适合保存比较大的整型
  2. BigDecimal适合保存精度更高的浮点型(小数)

2.2 BigInteger 和 BigDecimal 常见方法
BigDecimal代码实现:

public class BigDecimal_ {
    
    public static void main(String[] args) {
    
        //当我们需要保存一个精度很高的数时,double 不够用
        //可以是 Bigdecimal
        //double d = 1999.1111111111111111d;
        BigDecimal bigDecimal = new BigDecimal("1999.1111111119898989898989898989898989898989898");
        BigDecimal bigDecimal1 = new BigDecimal("1.212");
        System.out.println(bigDecimal);

        //1.如果对BigDecimal进行运算,比如加减乘除,需要使用对应的方法
        //2.创建一个需要操作的 BigDecimal,然后调用相应的方法即可
        System.out.println(bigDecimal.add(bigDecimal1));
        System.out.println(bigDecimal.subtract(bigDecimal1));
        System.out.println(bigDecimal.multiply(bigDecimal1));
        //System.out.println(bigDecimal.divide(bigDecimal1));//可能会抛出异常ArithmeticException
        //在调用divide方法时,指定精度即可 BigDecimal.ROUND_CEILING
        //如果有无限循环小数,就会保留分子的精度
        System.out.println(bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING));
    }
}

BigInteger代码实现:

public class BigInteger_ {
    
    public static void main(String[] args) {
    
        //当我们在编程中,需要处理很大的整数,long不够用
        //可以使用BigInteger的类来搞定
       //long l =2378888885555555555555l;
        BigInteger bigInteger = new BigInteger("2378888885555555555555");
        BigInteger bigInteger1 = new BigInteger("100");
        System.out.println(bigInteger);
        //1.在对 BigInteger进行加减乘除的时候,需要使用对应的方法,不能直接进行 + - * /
        //2.可以创建一个要操作的BigInteger 然后进行相应操作
        BigInteger add = bigInteger.add(bigInteger1);//加
        System.out.println(add);
        BigInteger subtract = bigInteger.subtract(bigInteger1);//减
        System.out.println(subtract);
        BigInteger multiply = bigInteger.multiply(bigInteger1);//乘
        System.out.println(multiply);
        BigInteger divide = bigInteger.divide(bigInteger1);//除
        System.out.println(divide);
    }
}

3. 日期类

3.1 第一代日期类

  1. Date:精确到毫秒,代表特定的瞬间
  2. SimpleDateFormat: 格式和解析日期的类SimpleDateFormat格式化和解析日期的具体类。
    它允许进行格式化(日期->文本)、 解析(文本->日期)和规范化.

案例:

public class Date_ {
    
    public static void main(String[] args) throws ParseException {
    
        //1.//获取当前系统时间
        //2.这里的Date 类是在java.itil包下
        //3.默认输出的日期格式是国外的方式,因此通常需要对格式进行转换
        Date date = new Date();//获取当前系统时间
        System.out.println("当前日期="+date);
        Date date1 = new Date(9235456);//通过指定毫秒数得到时间
        System.out.println("date1="+date1);


        //1.创建SimpleDateFormat 对象,可以指定相应的格式
        //2.这里的格式使用的字母是规定好的,不能乱写
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss E");
        String format = simpleDateFormat.format(date);
        System.out.println(format);

        //1.可以把一个格式化的String 转成对应的Date
        //2.得到Date 仍然在输出时,还是按照国外的形式,如果希望指定格式输出,需要转换
        //3.在把一个String->>Date 使用的是simpleDateFormat格式需要和给的String的格式一样,否则会抛出转换异常
         String s ="1996年01月01日 10:20:30 星期二";
         Date parse=simpleDateFormat.parse(s);
        System.out.println("parse="+parse);
    }
}

3.2 第二代日期类

1)第二代日期类,主要就是Calendar类(日历)。 public abstract class Calendar extends
Obiect implements Seralizable, Cloneable, Comparable < Calendar
2)Calendar类是一个抽象类,它为特定瞬间与组诸如YEAR、MONTH、 DAY OFMONTH、HOUR等日历字段之间的转换提供了一些方法,并为操 作日历字段(例如获得下星期的日期)提供了一些方法。

案例:

public class Calendar_ {
    
    public static void main(String[] args) {
    
        //1.calendar时一个抽象类,并且构造器时private的
        //2.可以通过getInstance()来获取实例
        //3.提供大量的方法和字段提供给程序员
        //4.Calendar没有提供对应的格式化的类,因此需要程序员自己组合显示
        //5.如果我们需要按照 24小时进制来获取时间,Calendar.HOUR 改成HOUR_OF_DAY
        Calendar instance = Calendar.getInstance();
        System.out.println(instance);
        //获取日历对象的某个日历字段
        System.out.println("年"+instance.get(Calendar.YEAR));
        //这里为什么要+1 因为Calendar返回月的时候,是按照0开始编号
        System.out.println("年"+instance.get(Calendar.MONTH)+1);
    }
}

3.3 第三代日期类
➢前面两代日期类的不足分析

JDK 1.0中包含了一个java.util.Date类, 但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了。
而Calendar也存在问题是:
1)可变性:像日期和时间这样的类应该是不可变的。
2)偏移性: Date中的年份是从1900开始的,而月份都从开始。
3)格式化:格式化只对Date有用,Calendar则不行。
4)此外,它们也不是线程安全的;不能处理闰秒等(每隔2天,多出1s)。

第三代日期类的方法
案例:

public class LocalDate_ {
    
    public static void main(String[] args) {
    
        //第三代日期
        //1.使用now()返回表示当前日期时间的对象
        LocalDateTime now = LocalDateTime.now();
        System.out.println(now);

        //2.使用DateTimeFormatter 类进行格式化
        //创建 DateTimeFormatter 对象
        DateTimeFormatter dtm = DateTimeFormatter.ofPattern("yyyy年MM月dd日 hh:mm:ss E");
        String format = dtm.format(now);
        System.out.println(format);
        System.out.println("年="+now.getYear());
        System.out.println("月="+now.getMonth());
        System.out.println("月="+now.getMonthValue());
        System.out.println("日="+now.getDayOfMonth());
        System.out.println("时="+now.getHour());
        //提供 plus 和minus方法可以对当前时间进行加或者减
        //看看898天后,是什么时候 把 年月日时分秒
        LocalDateTime localDateTime = now.plusDays(898);
        System.out.println("898天后="+dtm.format(localDateTime));
        //看看345分钟前是什么时候,把 年月日-时分秒输出
        LocalDateTime localDateTime1 = now.minusMinutes(345);
        System.out.println("345分钟前="+dtm.format(localDateTime1));
    }
}

3.4 DateTimeFormatter 格式日期类

类似于SimpleDateFormat
DateTimeFormat dtf = Date
TimeFormatter.ofPattern(格式); String str = dtf.format(日期对象);

3.5 Instant 时间戳

类似于Date 提供了一系列和Date类转换的方式
Instant-- > Date:
Date date =Date.from(instant);
Date-- > Instant:
Instant instant =
date.tolnstant();

案例:

public class Instant_ {
    
    public static void main(String[] args) {
    
        //1.通过静态方法 now() 获取表示当前时间戳的对象
        Instant now = Instant.now();
        System.out.println(now);
        //2.通过from方法可以把Instant转成Date
        Date date = Date.from(now);
        //3.通过 date的toInstant(),可以把date转成Instant方法
        Instant instant = date.toInstant();
    }
}

3.6 第三代日期类更多方法

1.LocalDateTime类
2.MonthDay类:检查重复事件
3.是否是闰年
4.增加日期的某个部分
5.使用plus方法测试增加时间的某个部分
6.使用minus方法测试查看一年前和一年后的日期
7.其他的方法,查看API使用即可

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_47828100/article/details/123857628

智能推荐

while循环&CPU占用率高问题深入分析与解决方案_main函数使用while(1)循环cpu占用99-程序员宅基地

文章浏览阅读3.8k次,点赞9次,收藏28次。直接上一个工作中碰到的问题,另外一个系统开启多线程调用我这边的接口,然后我这边会开启多线程批量查询第三方接口并且返回给调用方。使用的是两三年前别人遗留下来的方法,放到线上后发现确实是可以正常取到结果,但是一旦调用,CPU占用就直接100%(部署环境是win server服务器)。因此查看了下相关的老代码并使用JProfiler查看发现是在某个while循环的时候有问题。具体项目代码就不贴了,类似于下面这段代码。​​​​​​while(flag) {//your code;}这里的flag._main函数使用while(1)循环cpu占用99

【无标题】jetbrains idea shift f6不生效_idea shift +f6快捷键不生效-程序员宅基地

文章浏览阅读347次。idea shift f6 快捷键无效_idea shift +f6快捷键不生效

node.js学习笔记之Node中的核心模块_node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是-程序员宅基地

文章浏览阅读135次。Ecmacript 中没有DOM 和 BOM核心模块Node为JavaScript提供了很多服务器级别,这些API绝大多数都被包装到了一个具名和核心模块中了,例如文件操作的 fs 核心模块 ,http服务构建的http 模块 path 路径操作模块 os 操作系统信息模块// 用来获取机器信息的var os = require('os')// 用来操作路径的var path = require('path')// 获取当前机器的 CPU 信息console.log(os.cpus._node模块中有很多核心模块,以下不属于核心模块,使用时需下载的是

数学建模【SPSS 下载-安装、方差分析与回归分析的SPSS实现(软件概述、方差分析、回归分析)】_化工数学模型数据回归软件-程序员宅基地

文章浏览阅读10w+次,点赞435次,收藏3.4k次。SPSS 22 下载安装过程7.6 方差分析与回归分析的SPSS实现7.6.1 SPSS软件概述1 SPSS版本与安装2 SPSS界面3 SPSS特点4 SPSS数据7.6.2 SPSS与方差分析1 单因素方差分析2 双因素方差分析7.6.3 SPSS与回归分析SPSS回归分析过程牙膏价格问题的回归分析_化工数学模型数据回归软件

利用hutool实现邮件发送功能_hutool发送邮件-程序员宅基地

文章浏览阅读7.5k次。如何利用hutool工具包实现邮件发送功能呢?1、首先引入hutool依赖<dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.19</version></dependency>2、编写邮件发送工具类package com.pc.c..._hutool发送邮件

docker安装elasticsearch,elasticsearch-head,kibana,ik分词器_docker安装kibana连接elasticsearch并且elasticsearch有密码-程序员宅基地

文章浏览阅读867次,点赞2次,收藏2次。docker安装elasticsearch,elasticsearch-head,kibana,ik分词器安装方式基本有两种,一种是pull的方式,一种是Dockerfile的方式,由于pull的方式pull下来后还需配置许多东西且不便于复用,个人比较喜欢使用Dockerfile的方式所有docker支持的镜像基本都在https://hub.docker.com/docker的官网上能找到合..._docker安装kibana连接elasticsearch并且elasticsearch有密码

随便推点

Python 攻克移动开发失败!_beeware-程序员宅基地

文章浏览阅读1.3w次,点赞57次,收藏92次。整理 | 郑丽媛出品 | CSDN(ID:CSDNnews)近年来,随着机器学习的兴起,有一门编程语言逐渐变得火热——Python。得益于其针对机器学习提供了大量开源框架和第三方模块,内置..._beeware

Swift4.0_Timer 的基本使用_swift timer 暂停-程序员宅基地

文章浏览阅读7.9k次。//// ViewController.swift// Day_10_Timer//// Created by dongqiangfei on 2018/10/15.// Copyright 2018年 飞飞. All rights reserved.//import UIKitclass ViewController: UIViewController { ..._swift timer 暂停

元素三大等待-程序员宅基地

文章浏览阅读986次,点赞2次,收藏2次。1.硬性等待让当前线程暂停执行,应用场景:代码执行速度太快了,但是UI元素没有立马加载出来,造成两者不同步,这时候就可以让代码等待一下,再去执行找元素的动作线程休眠,强制等待 Thread.sleep(long mills)package com.example.demo;import org.junit.jupiter.api.Test;import org.openqa.selenium.By;import org.openqa.selenium.firefox.Firefox.._元素三大等待

Java软件工程师职位分析_java岗位分析-程序员宅基地

文章浏览阅读3k次,点赞4次,收藏14次。Java软件工程师职位分析_java岗位分析

Java:Unreachable code的解决方法_java unreachable code-程序员宅基地

文章浏览阅读2k次。Java:Unreachable code的解决方法_java unreachable code

标签data-*自定义属性值和根据data属性值查找对应标签_如何根据data-*属性获取对应的标签对象-程序员宅基地

文章浏览阅读1w次。1、html中设置标签data-*的值 标题 11111 222222、点击获取当前标签的data-url的值$('dd').on('click', function() { var urlVal = $(this).data('ur_如何根据data-*属性获取对应的标签对象

推荐文章

热门文章

相关标签