【JAVA】第五天

devtools/2024/10/11 13:26:09/

【JAVA】第五天

  • 一、Math类
  • 二、System类
  • 三、Runtime类
  • 四、BigDecimal类
  • 五、JDK8之前传统的日期、时间(不推荐
    • 1.Date类
    • 2.SimpleDateFormat类
    • 3.Calendar类
  • 六、JDK8之后新增的日期、时间
    • 1.LocalDateTime类
    • 2.ZoneId类
    • 3.ZonedDateTime类
    • 4.Instant类
    • 5.DateTimeFormatter类
    • 6.Period类
    • 7.Duration类

一、Math类

表格:

方法名作用
public static int abs​(int a)获取参数绝对值
public static double ceil​(double a)向上取整
public static double floor​(double a)向下取整
public static int round​(float a)四舍五入
public static int max​(int a,int b)获取最大值
public static int min(int a,int b)获取最小值
public static double pow​(double a,double b)返回a的b次幂的值
public static double random​()返回值为double的随机值,范围[0.0,1.0)

用法:

java">    public static void main(String[] args) {// 绝对值System.out.println(Math.abs(-1));    // 1// 向上取整System.out.println(Math.ceil(1.1));  // 2.0// 向下取整System.out.println(Math.floor(1.5)); // 1.0// 四舍五入System.out.println(Math.round(1.4)); // 1System.out.println(Math.round(1.5)); // 2// 获取最大值System.out.println(Math.max(2,1));   // 2// 获取最小值System.out.println(Math.min(2,1));   // 1// a的b次幂System.out.println(Math.pow(2, 3));  // 8.0// 获取随机值System.out.println(Math.random());   // [0.0,1.0)}

二、System类

表格:

方法名作用
public static void exit​(int status)终止当前运行的Java虚拟机
public static long currentTimeMillis​()返回当前系统的时间毫秒值形式

用法:

java">    public static void main(String[] args) {// 从1970年1月1日 00:00:00走到此刻的总的毫秒数// 方法一System.out.println(System.currentTimeMillis());// 方法二System.out.println(new Date().getTime());// 终止Java虚拟机的运行System.exit(0);}

三、Runtime类

表格:

方法名作用
public static Runtime getRuntime()返回与当前Java应用程序关联的运行时对象
public void exit​(int status)终止当前运行的虚拟机
public int availableProcessors()返回Java虚拟机可用的处理器数
public long totalMemory()返回Java虚拟机中的内存总量
public long freeMemory()返回Java虚拟机中的可用内存
public Process exec​(String command)启动某个程序,并返回代表该程序的对象

用法:

java">    public static void main(String[] args) throws IOException {Runtime r = Runtime.getRuntime();System.out.println(r.availableProcessors()); // 获取可用处理器数System.out.println(r.totalMemory()); // 获取内存总量System.out.println(r.freeMemory()); // 获取可用内存量r.exec("QQ"); // 启动QQr.exit(0); // 终止虚拟机的运行}

四、BigDecimal类

表格:

构造器作用
public BigDecimal(double val)不推荐)将 double转换为 BigDecimal
public BigDecimal(String val)把String转成BigDecimal
方法名作用
public static BigDecimal valueOf(double val)转换一个 double成 BigDecimal
public BigDecimal add(BigDecimal b)加法
public BigDecimal subtract(BigDecimal b)减法
public BigDecimal multiply(BigDecimal b)乘法
public BigDecimal divide(BigDecimal b)除法
public BigDecimal divide (另一个BigDecimal对象,精确几位,舍入模式)除法、可以控制精确到小数几位
public double doubleValue()将BigDecimal转换为double

用法:

java">    public static void main(String[] args) {// String 转 BigDecimalBigDecimal bd1 = new BigDecimal("0.1");System.out.println(bd1); // 0.1// double 转 BigDecimalBigDecimal bd2 = BigDecimal.valueOf(0.2);System.out.println(bd2); // 0.2// 相加System.out.println(bd2.add(bd1)); // 0.3// 相减System.out.println(bd2.subtract(bd1)); // 0.1// 相乘System.out.println(bd2.multiply(bd1)); // 0.02// 除法一System.out.println(bd2.divide(bd1)); // 2// 除法二System.out.println(bd2.divide(bd1,1, RoundingMode.CEILING)); // 2.0// BigDecimal 转 doubleSystem.out.println(bd1.doubleValue()); // 0.1}

BigDecimal 解决浮点型运算时,出现结果失真的问题

五、JDK8之前传统的日期、时间(不推荐

1.Date类

表格:

构造器作用
public Date()创建一个Date对象,为当前系统时间
public Date(long time)把时间毫秒值转换成Date日期对象
方法名作用
public long getTime()返回从1970年1月1日 00:00:00走到此刻的总的毫秒数
public void setTime(long time)设置日期对象的时间为当前时间毫秒值对应的时间

用法:

java">    public static void main(String[] args) {Date d1 = new Date();System.out.println(d1); // 当前系统时间Date d2 = new Date(1000);System.out.println(d2); // 1970 加上 1000毫秒的时间System.out.println(d1.getTime()); // 1970 到当前时间的毫秒值d1.setTime(1000); // 重新设置d1的时间System.out.println(d1);}

2.SimpleDateFormat类

表格:

构造器作用
public SimpleDateFormat​(String pattern)创建简单日期格式化对象,并封装时间的格式
方法名作用
public final String format(Date date)将日期格式化成日期/时间字符串
public final String format(Object time)将时间毫秒值式化成日期/时间字符串
public Date parse​(String source)把字符串时间解析成日期对象

用法:

java">    public static void main(String[] args) throws ParseException {// 创建格式化对象SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss EEE a");// 获取时间Date d = new Date();// 时间格式化成字符串String format = sdf.format(d);System.out.println(format); // 2024-09-10 19:20:59 周二 下午// 获取从1970年到现在系统时间的毫秒long time = d.getTime();// 时间毫秒格式化成字符串String format1 = sdf.format(time);System.out.println(format1); // 2024-09-10 19:20:59 周二 下午// 字符串转成日期对象Date parse = sdf.parse(format);System.out.println(parse); // Tue Sep 10 19:20:59 CST 2024}

y —— 年
M —— 月
d —— 日
H —— 时
m —— 分
s —— 秒
EEE —— 周几
a —— 上午/下午

3.Calendar类

表格:

方法名作用
public static Calendar getInstance()获取当前日历对象
public int get(int field)获取日历中的某个信息
public final Date getTime()获取日期对象
public long getTimeInMillis()获取时间毫秒值
public void set(int field,int value)修改日历的某个信息
public void add(int field,int amount)为某个信息增加/减少指定的值

用法:

java">    public static void main(String[] args) {// 创建获取日历对象Calendar c = Calendar.getInstance();System.out.println(c); // ...YEAR=2024,MONTH=8...System.out.println(c.get(Calendar.YEAR)); // 2024System.out.println(c.getTime()); // Tue Sep 10 19:33:11 CST 2024System.out.println(c.getTimeInMillis()); // 1725968029529// 修改日历中指定的值c.set(Calendar.MONTH,10);System.out.println(c); // ...YEAR=2024,MONTH=10...// 为指定的值加减c.add(Calendar.YEAR,1);System.out.println(c); // ...YEAR=2025,MONTH=10...}

六、JDK8之后新增的日期、时间

1.LocalDateTime类

用法:

java">    public static void main(String[] args) {// 获取当前系统时间LocalDateTime ldt1 = LocalDateTime.now();System.out.println(ldt1); // 2024-09-10T19:57:32.190467400// 指定时间对象LocalDateTime ldt2 = LocalDateTime.of(2024,9,18,12,10,5);System.out.println(ldt2); // 2024-09-18T12:10:05// 转换成LocalDateLocalDate ld = ldt1.toLocalDate();System.out.println(ld); // 2024-09-10// 转换成LocalTimeLocalTime lt = ldt1.toLocalTime();System.out.println(lt); // 20:08:26.372192300}

LocalDate、LocalTime
表格:

方法名作用
getYear、getMonthValue、getDayOfMonth、getDayOfYear、getDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等
withYear、withMonth、withDayOfMonth、withDayOfYear、withHour、withMinute、withSecond、withNano修改某个信息,返回新日期时间对象
plusYears、plusMonths、plusDays、plusWeeks、plusHours、plusMinutes、plusSeconds、plusNanos把某个信息加多少,返回新日期时间对象
minusYears、minusMonths、minusDays、minusWeeks、minusHours、minusMinutes、minusSeconds、minusNanos把某个信息减多少,返回新日期时间对象
equals isBefore isAfter判断2个时间对象,是否相等,在前还是在后

2.ZoneId类

表格:

方法名作用
public static Set<String> getAvailableZoneIds​()获取Java中支持的所有时区
public static ZoneId systemDefault​()获取系统默认时区
public static ZoneId of​(String zoneId)获取一个指定时区

3.ZonedDateTime类

表格:

方法名作用
public static ZonedDateTime now​()获取当前时区的ZonedDateTime对
public static ZonedDateTime now​(ZoneId zone)获取指定时区的ZonedDateTime对象
getYear、getMonthValue、getDayOfMonth、getDayOfYeargetDayOfWeek、getHour、getMinute、getSecond、getNano获取年月日、时分秒、纳秒等
public ZonedDateTime withXxx(时间)修改时间系列的方法
public ZonedDateTime minusXxx​(时间)减少时间系列的方法
public ZonedDateTime plusXxx​(时间)增加时间系列的方法

4.Instant类

表格:

方法名作用
public static Instant now​()获取当前时间的Instant对象(标准时间)
public long getEpochSecond()获取从1970-01-01 00:00:00开始记录的秒数
public int getNano()从时间线开始,获取从第二个开始的纳秒数
plusMillis plusSeconds plusNanos判断系列的方法
minusMillis minusSeconds minusNanos减少时间系列的方法
equals、isBefore、isAfter增加时间系列的方法

5.DateTimeFormatter类

表格:

方法名作用
public static DateTimeFormatter ofPattern(时间格式)获取格式化器对象
public String format(时间对象)格式化时间
public static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)解析时间

6.Period类

表格:

方法名作用
public static Period between(LocalDate start, LocalDate end)传入2个日期对象,得到Period对象
public int getYears()隔几年
public int getMonths()隔几个月
public int getDays()隔多少天

7.Duration类

表格:

方法名作用
public static Duration between(开始时间对象1,截止时间对象2)传入2个时间对象,得到Duration对象
public long toDays()、toHours()、toMinutes()、toSeconds()、toMillis()、toNanos()多少天、多少小时、多少分、多少秒、多少毫秒、多少纳秒

http://www.ppmy.cn/devtools/110064.html

相关文章

Leetcode面试经典150题-55.跳跃游戏

解法都在代码里&#xff0c;不懂就留言或者私信 class Solution {public boolean canJump(int[] nums) {/**如果就一个位置&#xff0c;你本来就在这&#xff0c;肯定可以跳到*/if(nums.length 1) {return true;}/**这个题的解题思路是遍历数组&#xff0c;如果当前位置不在之…

【SRC】某次众测绕过限制注册用户+敏感信息泄露漏洞

吉祥知识星球http://mp.weixin.qq.com/s?__bizMzkwNjY1Mzc0Nw&mid2247485367&idx1&sn837891059c360ad60db7e9ac980a3321&chksmc0e47eebf793f7fdb8fcd7eed8ce29160cf79ba303b59858ba3a6660c6dac536774afb2a6330&scene21#wechat_redirect 《网安面试指南》…

再见Java 8,请掌握最新LTS

简介 在Java开发中&#xff0c;Java 8曾经是无可争议的主流&#xff0c;凭借其稳定性和广泛的社区支持&#xff0c;陪伴了无数开发者走过多年辉煌时刻。然而&#xff0c;随着时间的推移&#xff0c;技术不断革新&#xff0c;企业和开发者们逐渐把目光投向了更新的LTS&#xff0…

磁盘映射(C语言)

目录 一、背景介绍 二、磁盘映射技术概述 1.磁盘映射原理 2.磁盘映射的优势 三、C语言实现磁盘映射 磁盘映射技术在C语言中的应用能够极大地提高文件操作的效率。本文将详细介绍磁盘映射的概念、如何在C语言中实现磁盘映射&#xff0c;并通过实际案例展示其在文件读写、数据…

Leetcode面试经典150题-76.最小覆盖子串

解法都在代码里&#xff0c;不懂就留言或者私信 理论上提交这个就是最优解 class Solution {public String minWindow(String s, String t) {if(s.length() < t.length()) {return "";}/**转成字符数组 */char[] sArr s.toCharArray();char[] tArr t.toCharAr…

声音克隆工具CosyVoice

阿里的免费声音克隆工具CosyVoice CosyVoice 是阿里通义实验室在七月初开源的一款专注于自然语音合成的语音大模型&#xff0c;它具备多语言、多音色和细腻的情感控制能力。这个系统支持中文、英文、日文、粤语和韩语五种语言的语音生成&#xff0c;并且在语音合成的效果上远超…

【Go - vendor, 本地, 独立依赖包】

go vendor 是 Go 语言中的一个机制&#xff0c;用于管理项目的依赖包。它允许你将项目所依赖的第三方包复制到项目的 vendor 目录中&#xff0c;从而确保项目的依赖包版本一致&#xff0c;并且在构建和运行时不会受到外部变化的影响。 &#xff08;划重点&#xff1a;所依赖的…

QT 对话框 仿文本编辑器

对话框通常是一个顶层窗口&#xff0c;出现在程序最上层&#xff0c;用于实现短期任务或者简洁的用户交互 一、消息对话框&#xff08;QMessageBox&#xff09; 1、QMessageBox类成员函数实现 1&#xff09;实例化 QMessageBox类 对象 2&#xff09;设置对象属性 3&#x…