Math 类
直接通过类名调用,数学类中还有两个静态常量PI和E
利用帮助文档,查阅相关方法说明
random 类
jdk的Java.util包中,指定的取值范围内随机产生数字
使用帮助文档
使用random类的方法
包装类
通过包装类将基本数据类型的值包装为引用数据类型的对象
自动装箱、拆箱是包装类进行数据类型转换的方式
日期与时间类
calendar类:一个抽象类,无法实例化,需要调用静态方法getInstance()得到一个Calender对象
Calender calender=Calender.getInstance();
int get(int field)//返回指定日历字段的值
field参数接收calender类定义的常量值(时间日期等等)
calender类获取计算机日期和时间
calender日历容错模式和非容错模式
演示案例:
说明:
常用类中,要学会使用帮助文档:
1、注意类所在的包的导入
2、类是不是抽象类 ,如果是抽象类,存在一个静态方法可以获取该类的对象。不是抽象类 ,则new 一个对象,通过对象调用方法
格式化类
1、DateFormat类
jdk8版本使用的:——————————————
LocalDateTime date = LocalDateTime.now();
// 1、使用常量创建DateTimeFormatter
System.out.print(“使用常量创建DateTimeFormatter:”);
System.out.println(DateTimeFormatter
.ISO_LOCAL_DATE.format(date));
// 2、使用Long类型风格的DateTimeFormatter
System.out.print(“使用Long类型风格的DateTimeFormatter:”);
DateTimeFormatter dtf = DateTimeFormatter
.ofLocalizedDateTime(FormatStyle.LONG);
System.out.println(dtf.format(date));
// 3、根据模式字符串来创建DateTimeFormatter格式器
System.out.print(“根据模式字符串来创建DateTimeFormatter:”);
DateTimeFormatter formatter = DateTimeFormatter
.ofPattern(“yyyy MM dd HH:mm:ss”);
// 使用LocalDateTime的format()方法格式化
String text = date.format(formatter);
// 使用格式化程序解析文本,返回日期时间
LocalDateTime parsedDate = LocalDateTime.parse(text, formatter);
System.out.println(parsedDate);
运行结果: