day13

day13

自动装箱与自动拆箱

自动装箱:基本数据类型可以自动转换为包装类型。

自动拆箱:包装类型可以自动的转换为基本数据类型

//自动装箱
Integer i1 = 10;//Integer i1 = Integer.vauleOf(10);
//自动拆箱
int i2 = i1;//int i2 = i1.int Value();

常见API

StringBuilder类

主要方法:

 public StringBuilder append(任意类型);// 添加数据并返回StringBuilder对象

public StringBuilder reverse();//将对象内容翻转

public intlength();//返回对象内容长度

public String();//将对象转换成字符串类型

对于字符串相关的操作,如频繁的拼接,修改等,建议用StringBuilder,效率更高

注意:如果字符串较少,或者不需要操作,以及定义字符串变量,还是建议用String

StringJoiner类

而StringJoiner号称是拼接神器,不仅效率高,而且代码简洁

public StringJoiner (间隔符号) 创建一个StringJoiner对象,指定拼接时的间隔符号
public StringJoiner (间隔符号,开始符号,结束符号) 创建一个StringJoiner对象,指定拼接时的间隔符号、开始符号、结束符号
public StringJoiner add (添加的内容) 添加数据,并返回对象本身
public int length() 返回长度 ( 字符出现的个数)
public String toString() 返回一个字符串(该字符串就是拼接之后的结果)

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) 获取两个int值中的较大值
public static double pow(double a,double b) 返回a的b次幂的值
public static double random() 返回值为double的随机值,范围[0.0,1.0)

System类

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

Runtinme类

方法名 说明
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) 启动某个程序,并返回代表该程序的对象

Bigdecimal类

构造器 说明
public BigDecimal(double val) 注意:不推荐使用这个,double可能会有精度丢失问题 将 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

Date类

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

SimpleDateFormat

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

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) 为某个信息增加/减少指定的值

LocalDate

方法名 示例
public static Xxxx now(): 获取系统当前时间对应的该对象 LocaDate ld = LocalDate.now(); LocalTime lt = LocalTime.now(); LocalDateTime ldt = LocalDateTime.now();
public static Xxxx of(…):获取指定时间的对象 LocalDate localDate1 = LocalDate.of(2099 , 11,11); LocalTime localTime1 = LocalTime.of(9, 8, 59); LocalDateTime localDateTime1 = LocalDateTime.of(2025, 11, 16, 14, 30, 01);
public int geYear() 获取年
public int getMonthValue() 获取月份(1-12)
public int getDayOfMonth() 获取日
public int getDayOfYear() 获取当前是一年中的第几天
Public DayOfWeek getDayOfWeek() 获取星期几:ld.getDayOfWeek().getValue()
方法名 说明
withYear、withMonth、withDayOfMonth、withDayOfYear 直接修改某个信息,返回新日期对象
plusYears、plusMonths、plusDays、plusWeeks 把某个信息加多少,返回新日期对象
minusYears、minusMonths、minusDays,minusWeeks 把某个信息减多少,返回新日期对象
equals isBefore isAfter 判断两个日期对象,是否相等,在前还是在后

LocalTime

方法名 说明
public int getHour() 获取小时
public int getMinute() 获取分
public int getSecond() 获取秒
public int getNano() 获取纳秒
方法名 说明
withHour、withMinute、withSecond、withNano 修改时间,返回新时间对象
plusHours、plusMinutes、plusSeconds、plusNanos 把某个信息加多少,返回新时间对象
minusHours、minusMinutes、minusSeconds、minusNanos 把某个信息减多少,返回新时间对象
equals isBefore isAfter 判断2个时间对象,是否相等,在前还是在后

LocalDateTime

方法名 说明
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个时间对象,是否相等,在前还是在后

LocalDateTime转换成LocalDate、LocalTime

方法名 说明
public LocalDate toLocalDate() 转换成一个LocalDate对象
public LocalTime toLocalTime() 转换成一个LocalTime对象
posted @   萌新小夏  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
点击右上角即可分享
微信分享提示