BigDecimal类
BigDecimal类 : 关于小数精准运算的类 (不可变的、任意精度的有符号十进制数)
构造方法 :
BigDecimal(String val) -> 推荐的构造方法;
BigDecimal(double val) -> 不推荐的
四则运算方法 :
加 : BigDecimal add(BigDecimal augend) -> BigDecimal result = bd1.add(bd2);
bd1 + bd2 把结果返回给result变量
减 : BigDecimal subtract(BigDecimal subtrahend)
乘 : BigDecimal multiply(BigDecimal multiplicand)
除 : BigDecimal divide(BigDecimal divisor)
带有舍入模式的除法:
BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
BigDecimal divisor : 除数
int scale : 保留小数点后几位
RoundingMode roundingMode : 舍入模式
RoundingMode.CEILING : 向上取整
RoundingMode.FLOOR : 向下取整
RoundingMode.HALF_UP : 四舍五入
包装类
包装类 : 四类八种基本数据类型所对应的引用数据类型
为什么学习包装类 :
集合中只能存储引用数据类型数据(集合中只能存对象)
四类八种基本数据类型所对应的引用数据类型(包装类) :
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
以Integer为例去讲解包装类 :
构造方法 :
Integer(int value)
Integer(String s)
包装类中的构造方法一般不用!!原因是因为所有的包装类型都具备 自动装箱 和 自动拆箱的特性
自动装箱 : 把基本数据类型 --> 对应包装类型 叫装箱
例如 : Integer i = 100; //隐藏代码 : Integer i = Integer.valueOf(100);
自动拆箱 : 把包装类型 --> 对应基本数据类型 叫拆箱
例如 : int num = i; //隐藏代码 : int num = i.intValue();
常见需求 : 把一个字符串类型的基本值转换成真正的基本数据类型值;
"100" + "200" --> "100200"
在包装类中都有一个 static 对应基本数据类型 parseXxx(字符串类型的基本值) //字符串基本值必须是纯的
例如 :
Integer : static int parseInt(字符串类型的整数)
Double : static double parseDouble(字符串类型的小数
注意事项 : Character 中没有parseXxx方法 , String --> char : charAt(索引)
时间操作的类
1. 时间原点 : 1970年01月01日 00:00:00 ; 零时区
2. 时间换算 : 1 秒 = 1000 毫秒 1 毫秒 = 1000 微秒 1 微秒 = 1000 纳秒
3. 时间单词/单词缩写 :
4. 时间文化差异 :
a. 一周的第一天 : 代码中是礼拜日
b. 月份的编号 : 从0开始
5. 时间类型
long 时间 (时间毫秒值) -> 可以运算的时间单位
Date 时间 -> 中间值
String 时间 ("1990年01月01日") -> 最方便表示的时间
Calendar 时间 最好操作的时间类型
Date类
Date类 : Date对象指向一个时间瞬间,单位精确到 毫秒
Date类的包 : java.util.Date;
创建Date对象 :
Date(): 创建Date对象,并让此Date对象指向创建对象的那个瞬间;
Date(long millis):创建Date对象,先把时间"归零",再加上传入的毫秒值定格的瞬间就是此Date对象指向的瞬间
成员方法 :
获取时间 : long getTime() -> 获取调用方法的Date对象指向的时间所对应的毫秒值
设置时间 : void setTime(long millis) -> 设置调用此方法的Date对象指向的时间瞬间
先把时间"归零",再加上传入的毫秒值定格的瞬间就是此Date对象被设置的时间瞬间
看本质 : 时间类型之间的转换
long 时间 --> Date 时间
1. Date(long millis)
2. void setTime(long millis)
Date 时间 --> long 时间
long getTime()
SimpleDateFormat类
SimpleDateFormat类 : 格式化和解析Date对象(时间对象)
格式化时间 : 把时间格式变好看 Date --> String
解析时间 : 把一个字符串时间解析成为一个真正的Date时间对象 String --> Date
构造方法 :
SimpleDateFormat(String pattern) : 设置解析和格式化的日期格式创建SimpleDateFormat对象
String pattern : 日期格式 -> 日期格式代码
y : 年
M : 月
d : 日
H : 小时
m : 分钟
s : 秒
实例 : yyyy年MM月dd日 HH:mm:ss
格式化日期的方法 :
SimpleDateFormat类 : String format(Date date) -> 按照创建SimpleDateFormat对象时给定的日期格式对Date对象进行日期格式化操作 (变好看)
解析日期的方法 : String --> Date 对象
SimpleDateFormat类 : Date parse(String time) -> 按照传入的字符串时间解析到Date对象上
//解析时一定要保证字符串的日期格式和创建SimpleDateFormat对象时给的时期格式一致!
//如何解决 : alt + enter 把异常抛出,交给调用者处理

案例:
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Demo1 {
public static void main(String[] args) throws ParseException {
//计算你活了多久
//获取当前时间毫秒值
long now = System.currentTimeMillis();
//获取出生时间毫秒值
Scanner sc = new Scanner(System.in);
System.out.println("输入你的出生年月日:");
String birthStr = sc.nextLine();
System.out.println("请您输入您的出生日期 : (请按照 yyyy年MM月dd日 HH:mm:ss 格式给予日期)");
//解析字符串出生时间日期 sdf.parse(birthStr)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date birthDate = sdf.parse(birthStr);
//Date 对象 --> long 时间毫秒值
long birthTime = birthDate.getTime();//出生时间毫秒值
long days = (now - birthTime) / (1000 * 60 * 60 * 24);
System.out.println("您已经活了 " + days + "天 . 人的一生一共能活30000多天,请珍爱生命" );
}
}
Calendar类
Calendar类 : 日历类
创建对象的方法:
static Calendar getInstance() : 时间指向现在
获取日历信息和设置日历信息的方法:
int get(int field) : 根据传入的日历字段来获取该日历字段的信息
int field : 日历字段 --> 使用Calendar类中的自定义常量值 例如: Calendar.YEAR
返回值 int : 该字段的具体值
void set(int field,int value) : 设置调用方法的日历对象时间,不会生成新的对象而是在老对象上直接修改
int field : 日历字段
int value :需要设置的值
void set(年,月,日,时,分,秒)
Calendar类的转换方法 :
Calendar类对象 --> Date对象
Date getTime()
Date对象 --> Calendar类对象
void setTime(Date date)
Calendar类对象 --> long 毫秒值
long getTimeInMillis()
long 毫秒值 --> Calendar类对象
void setTimeInMillis(long millis)
案例:
import java.util.Calendar;
import java.util.Scanner;
public class Demo2 {
public static void main(String[] args){
//获取当前时间毫秒值
long now = System.currentTimeMillis();
//获取出生时间毫秒值
Scanner sc = new Scanner(System.in);
System.out.println("请您输入您的出生年份");
int year = sc.nextInt();
System.out.println("请您输入您的出生月份");
int month = sc.nextInt();
System.out.println("请您输入您的出生日期");
int day = sc.nextInt();
//把年月日设置到Calendar对象上
Calendar calendar = Calendar.getInstance();
calendar.set(year,month - 1,day,0,0,0);
//Calendar --> long
long birthTime = calendar.getTimeInMillis();
long days = (now - birthTime) / (1000 * 60 * 60 * 24);
System.out.println("您已经活了 " + days + "天 . 人的一生一共能活30000多天,请珍爱生命");
}
}
相互转换图

LocalDateTime/LocalDate/LocalTime类
LocalDateTime/LocalDate/LocalTime类 -> JDK8的时间类
好处 :
1. 类似于日历对象操作 : 单独设置日期字段
2. 解决时间文化差异问题
3. 时间精度 : 纳秒级别
LocalDateTime/LocalDate/LocalTime : 本地日期时间/本地日期/本地时间
创建LocalDateTime/LocalDate/LocalTime类的对象
LocalDateTime是一个不可变的日期时间对象,代表日期时间 , 精度到纳秒
创建对象的方法 :
static LocalDateTime now() : 创建LocalDateTime对象指向现在
static LocalDateTime of(int year, int month, int dayOfMonth, int hour, int minute, int second) : 创建LocalDateTime对象指向指定时间
static LocalDateTime of(int year, Month month, int dayOfMonth, int hour, int minute, int second) Month month : 枚举类型
DateTimeFormatter类
DateTimeFormatter类 : 只提供解析和格式化的日期格式
创建DateTimeFormatter类对象的方法 : static DateTimeFormatter ofPattern("日期格式")
LocalDateTime的时间解析操作:
格式化操作 : LocalDateTim对象 --> String 对象
LocalDateTime类中 : String format(DateTimeFormatter formatter)
解析操作 : String 对象 --> LocalDateTim对象
LocalDateTime类中 : static LocalDateTime parse(CharSequence text, DateTimeFormatter formatter)
案例:
public class Demo3 {
public static void main(String[] args) throws ParseException {
//获取当前时间毫秒值
long now = System.currentTimeMillis();
//获取出生时间毫秒值
Scanner sc = new Scanner(System.in);
System.out.println("请您输入您的出生年份");
int year = sc.nextInt();
System.out.println("请您输入您的出生月份");
int month = sc.nextInt();
System.out.println("请您输入您的出生日期");
int day = sc.nextInt();
//创建LocalDateTime对象
LocalDateTime birth = LocalDateTime.of(year, month, day, 0, 0, 0);
//LocalDateTime -> String 格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日 HH:mm:ss");
String format = birth.format(formatter);
//String -> Date 解析
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date parse = sdf.parse(format);
//Date -> long
long birthTime = parse.getTime();
long days = (now - birthTime) / (1000 * 60 * 60 * 24);
System.out.println("您已经活了 " + days + "天 . 人的一生一共能活30000多天,请珍爱生命");
}
}
LocalDateTime类中的until方法
long until(Temporal endExclusive, TemporalUnit unit)
Temporal 接口 : 是 LocalDateTime/LocaDate/LocalTime的父接口
TemporalUnit 接口 : 时间单位的父接口 实现类- > ChronoUnit
编写格式:
long 结果 = 起始时间对象.until(结束时间对象,时间单位);
案例:
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Scanner;
public class Demo4 {
public static void main(String[] args) throws ParseException {
//获取出生时间毫秒值
Scanner sc = new Scanner(System.in);
System.out.println("请您输入您的出生年份");
int year = sc.nextInt();
System.out.println("请您输入您的出生月份");
int month = sc.nextInt();
System.out.println("请您输入您的出生日期");
int day = sc.nextInt();
//创建LocalDateTime对象
LocalDateTime birth = LocalDateTime.of(year, month, day, 0, 0, 0);
//创建现在的时间单位
LocalDateTime now = LocalDateTime.now();
// long 结果 = 起始时间对象.until(结束时间对象,时间单位);
long days = birth.until(now, ChronoUnit.DAYS);
System.out.println("您已经活了 " + days + "天 . 人的一生一共能活30000多天,请珍爱生命");
}
}
LocalDateTime的get系列方法(获取值)
int getYear() :获取年信息
int getMonthValue() :获取月份信息(1-12)
int getDayOfMonth(): 获取天信息
int getHour() :获取小时信息
int getMinute():获取分钟信息
int getSecond(): 获取秒钟信息
-------------------------------------------
DayOfWeek getDayOfWeek():获取星期信息
int getDayOfYear():获取年中天信息
Month getMonth(): 获取月份信息(枚举对象返回)
int getNano():获取纳秒信息
LocalDateTime的系列方法
LocalDateTime的plus系列方法(加)
plus系列方法是对日期字段进行做加法的操作!
LocalDateTime plusYears(long years)
LocalDateTime plusMonths(long months)
LocalDateTime plusDays(long days)
LocalDateTime plusHours(long hours)
LocalDateTime plusMinutes(long minutes)
LocalDateTime plusSeconds(long seconds)
-------------------------------------------------
LocalDateTime plusNanos(long nanos)
LocalDateTime plusWeeks(long weeks)
注意:
1. 记得接收, 因为时间修改是生成新的LocalDateTime对象,老的LocalDateTime对象不会改变
2. 如果你传入的是正数就是做加法,如果你传入的是负数就是做减法
LocalDateTime的minus系列方法(减)
minus系列方法是对日期字段进行做减法的操作!
LocalDateTime minusYears(long years)
LocalDateTime minusMonths(long months)
LocalDateTime minusDays(long days)
LocalDateTime minusHours(long hours)
LocalDateTime minusMinutes(long minutes)
LocalDateTime minusSeconds(long seconds)
-------------------------------------------------
LocalDateTime minusNanos(long nanos)
LocalDateTime minusWeeks(long weeks)
注意:
1. 记得接收, 因为时间修改是生成新的LocalDateTime对象,老的LocalDateTime对象不会改变
2. 如果你传入的是正数就是做减法,如果你传入的是负数就是做加法
LocalDateTime的with系列方法(设置值)
with系列方法是对日期字段进行设置值的操作!
LocalDateTime withYears(long years)
LocalDateTime withMonths(long months)
LocalDateTime withDays(long days)
LocalDateTime withHours(long hours)
LocalDateTime withMinutes(long minutes)
LocalDateTime withSeconds(long seconds)
-------------------------------------------------
LocalDateTime withNanos(long nanos)
LocalDateTime withWeeks(long weeks)
注意:
1. 记得接收, 因为时间修改是生成新的LocalDateTime对象,老的LocalDateTime对象不会改变
时间间隔类
Period类
Period类 : 年月日的时间间隔
创建对象 :
static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
成员方法 : int getDays() -> 获取间隔的天数(废物方法)
Duration类
Duration类 : 时分秒的时间间隔
创建对象:
static Duration between(Temporal startInclusive, Temporal endExclusive)
Temporal 接口 : 是 LocalDateTime/LocaDate/LocalTime的父接口
实际使用上 : between 方法不能接收 LocaDate 对象
案例:
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
//获取出生时间毫秒值
Scanner sc = new Scanner(System.in);
System.out.println("请您输入您的出生年份");
int year = sc.nextInt();
System.out.println("请您输入您的出生月份");
int month = sc.nextInt();
System.out.println("请您输入您的出生日期");
int day = sc.nextInt();
//创建LocalDateTime对象
LocalDateTime birth = LocalDateTime.of(year, month, day, 0, 0, 0);
//创建现在的时间单位
LocalDateTime now = LocalDateTime.now();
//时间间隔对象
Duration duration = Duration.between(birth, now);
//一键获取间隔的天数
long days = duration.toDays(); //获取持续时间的天数。
System.out.println("您已经活了 " + days + "天 . 人的一生一共能活30000多天,请珍爱生命");
}
}
本文详细介绍了Java中的BigDecimal类用于高精度小数运算,包括构造方法和四则运算。此外,还讨论了包装类的作用,如Integer,并解释了自动装箱和拆箱的概念。时间操作部分涉及Date、SimpleDateFormat和Calendar类,以及时间格式化和解析。最后,提到了Java 8中的LocalDateTime及其优势,包括时间间隔的处理和日期时间的加减设置。

2522

被折叠的 条评论
为什么被折叠?



