JavaSE常用类2

常用类

String

 String s1 = "多喝热水";
 String s2 = "多喝热水";
 System.out.println(s1 == s2);//true
 System.out.println(s1.equals(s2));//true
 ​
 String s3 = new String("多喝热水");
 System.out.println(s1 == s3);//false
 System.out.println(s1.equals(s3));//true
 ​
 String s4 = "多喝" + "热水";//编译阶段就能拼接完成
 System.out.println(s1 == s4);//true
 System.out.println(s1.equals(s4));//true
 ​
 String s5 = "多喝";
 String s6 = "热水";
 String s7 = s5 + s6;//有一个变量都不能在编译阶段得到结果
 System.out.println(s1 == s7);//false
 System.out.println(s1.equals(s7));//true
 ​
 String s8 = s5 + "热水";
 System.out.println(s1 == s8);//false
 System.out.println(s1.equals(s8));//true

数学相关类

Math
 //0~9
 System.out.println((int)(Math.random() * 10));
 ​
 //1~10
 System.out.println((int)(Math.random() * 10 + 1));
 ​
 //1~5
 System.out.println((int)(Math.random() * 5 + 1));

 for(int i = 0; i < 10; i++){
     sout(Math.random());
 }

BigDecimal

作用:精确计算浮点整数

 BigDecimal n1 = new BigDecimal("100");
 BigDecimal n2 = new BigDecimal("3");
 ​
 BigDecimal result = n1.add(n2);//103
 ​
 result = n1.subtract(n2);//97
 ​
 result = n1.multiply(n2);//300
 ​
 result = n1.divide(n2);//ArithmeticException
 ​
 result = n1.divide(n2,5,BigDecimal.ROUND_HALF_UP);//33.33333
 //(除数,保留几位小数,进位的方式) //ROUND_HALF_UP是四舍五入

日期相关类

Date

实现了字符串和data的相互转化

 Date date = new Date();
 System.out.println(date);//现在的时间
 ​
 long time = date.getTime();
 System.out.println(time);//现在的时间戳
 ​
 Date date1 = new Date(time);
 System.out.println(date1);//传入谁的时间戳,就是哪个时间

SimpleDateFormat
  1. 要求:以特定格式打印时间日期 - 格式化Date对象

    如何做:

    • 创建SimpleDateFormat对象并设置格式

    • 使用format方法将Date对象转换成特定格式的字符串

 Date date = new Date();
 //创建SimpleDateFormat对象并设置格式
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS a");
 //年月日 时分秒毫秒 上下午
 //使用format方法将Date对象转换成特定格式的字符串
 String dateStr = sdf.format(date);
 System.out.println(dateStr);
  1. 将特定格式的日期时间字符串转换成Date对象:使用SimpleDateFromat

    如何做:

    • 创建SimpleDateFormat对象并设置格式

    • 使用parse方法将特定格式的字符串转换成Date对象

 Date date = new Date();
 SimpleDataFormat sft = new SimpleDataFormat("yyyy-MM-dd HH:mm:ss");
 String dateStr = sft.format(date);
 ​
 Date date1 = sft.parse(dateStr);
 //parse要catch-throw

Calender

 //获取当前时间对象
 LocalDateTime d = LocalDateTime.now();
 System.out.println(d);
 //获取特定日期时间对象
 LocalDateTime d1 = LocalDateTime.of(2024,1,1,19,20,10);
 System.out.println(d1);
 //获取年
 System.out.println(d.getYear());
 //获取月
 System.out.println(d.getMonth().getValue());
 //获取日
 System.out.println(d.getDayOfMonth());
 //获取这一年的哪一天
 System.out.println(d.getDayOfYear());
 //获取周几
 System.out.println(d.getDayOfWeek().getValue());
 //获取时
 System.out.println(d.getHour());
 //获取分
 System.out.println(d.getMinute());
 //获取秒
 System.out.println(d.getSecond());
 //加上几天的时间
 LocalDateTime d2 = d.plusDays(1);
 System.out.println(d2);

格式化与解析日期及时间

//LocalDateTime -->  特定格式的字符串
LocalDateTime d1 = LocalDateTime.now();
System.out.println( d1);//2024-07-24T15:10:53.562

String s1 = d1.format(DateTimeFormatter.BASIC_ISO_DATE);
//20240724
String s2 = d1.format(DateTimeFormatter.ISO_LOCAL_DATE);
//2024-07-24
String s3 = d1.format(DateTimeFormatter.ISO_DATE_TIME);
//2024-07-24T15:10:53.562

//LocalDateTime -->  特定格式的字符串   yyyy-MM-dd HH:mm:ss
DateTimeFormatter f1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String s4 = d1.format(f1);
System.out.println(s4);//2024-07-24 15:13:50

//特定格式字符串 -->  LocalDateTime
LocalDateTime d2 = LocalDateTime.parse("2024-07-24T15:14:33.616");
System.out.println(d2);//2024-07-24T15:14:33.616
LocalDateTime d3 = LocalDateTime.parse("2024-07-24 15:14:33",f1);
System.out.println(d3);//2024-07-24T15:14:33

//LocalDateTime --> 特定格式的日期时间字符串
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime l = LocalDateTime.of(1999, 10, 10, 10, 10, 10);//创建特定时间对象,现在时间:now()
String s1 = df1.format(l);
System.out.println(s1);
//1999-10-10 10:10:10


//特定格式的日期时间字符串转换成LocalDateTime对象
TemporalAccessor t1 = df1.parse(s1);
LocalDateTime l1 = LocalDateTime.from(t1);
System.out.println(l1);
//1999-10-10T10:10:10
  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值