【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() | 多少天、多少小时、多少分、多少秒、多少毫秒、多少纳秒 |