日期时间:Date、simpleDateFormat、Calendar
1.把时间毫秒值转换成日期对象。
long time=System.currentTimeMillis();
Date date=new Date(time);
//或者
long time=System.currentTimeMillis();
Date date=new Date();
date.setTime(time);
2.我们可以把日期对象和时间毫秒值格式化成我们喜欢的时间格式
//EEE表示周几,a表示上下午
//把日期转换成我们喜欢的日期格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss EEE a");
Date d=new Date();
String s=sdf.format(d);
//把时间毫秒值转换成我们喜欢的日期格式
long time = System.currentTimeMillis();
String s2=sdf.format(time);
3.学会使用SimpleDateformat解析字符串对象 计算2023年4月5日11点33分44秒之后2天13小时56分的时间。
public static void main(String[] args) throws ParseException {
String str = "2023年4月5日 11:44:33";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date = sdf.parse(str);
long time = date.getTime() + (2L*24*60*60+13*60*60+56*60)*1000;
String format = sdf.format(time);
System.out.println(format);
}
方法:日期1是否在日期2之前,或者之后
public static void main(String[] args) throws ParseException {
String s1="2020年12月10日 10:22:34";
String s2="2023年03月24日 12:34:35";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
Date date1=sdf.parse(s1);
Date date2 = sdf.parse(s2);
if (date1.after(date2)){
System.out.println("s1在s2时间之后");
}else if (date1.before(date2)){
System.out.println("s1在s2时间之前");
}else{
System.out.println("日期一样");
}
}
4.Calendar是日期抽象类,不能直接创建对象,通过调用方法返回对象。(获取日历的年月日,这一年的第几天,还有好多种方法)
Calendar cal= Calendar.getInstance();//获取日历对象
System.out.println(cal);
System.out.println(cal.get(Calendar.YEAR));
System.out.println(cal.get(Calendar.MONTH));
System.out.println(cal.get(Calendar.DATE));
System.out.println(cal.get(Calendar.DAY_OF_YEAR));
//拿到时间和时间毫秒值
System.out.println(cal.getTime());
System.out.println(cal.getTimeInMillis());
StringBuilder是一个可变字符串类,我们把他看成对象容器。 StirngBuilder只是手段,最终要调用toString方法返回String对象。 reverse() 方法是反转
StringBuilder sb=new StringBuilder();
StringBuilder a = sb.append("a").append(12).append("23").append(false);
String s = a.toString();
System.out.println(s);
String s2 = a.reverse().append("伽罗").toString();
System.out.println(s2);
Math类不需要创建对象,里面的方法都是静态的,直接通过类名.方法 来调用。 Math类也没有公开的构造器 介绍几个常用方法
System.out.println(Math.abs(-10)); //绝对值
System.out.println(Math.ceil(4.1)); //向上取整
System.out.println(Math.floor(4.9)); //向下取整
System.out.println(Math.pow(2, 3)); //2的3次方
System.out.println(Math.round(4.2));//四舍五入
System.out.println(Math.round(4.8));
System.out.println(Math.random()); //0-1之间的随机数,包含0,不包含1
System类中的方法也都是静态方法。 System.exit(0) 退出虚拟机。 System.currentTimeMills() 打印当前时间到1970年-1-1 的毫秒值时间。 System.arraycopy(做数组的拷贝)
int[] arr={2,3,6,1,0};
int[] arr2=new int[5];
/**
* Object src, int srcPos,
* Object dest, int destPos,
* int length);
* 参数一:被拷贝的数组
* 参数二:从哪个索引位置拷贝
* 参数三:复制的目标数组
* 参数四:粘贴到哪个位置(目标数组的位置)
* 参数五:拷贝元素个数
*/
System.arraycopy(arr,0,arr2,2,2);
System.out.println(Arrays.toString(arr2));
BigDecimal类 解决浮点型运算失真问题。BigDecimal必须要精度运算,如果不精度运算就会报错。BigDecimal对象如何获取,直接调用他的valueOf()方法.
double a=0.1;
double b=0.2;
double c=a+b;
System.out.println(c);
BigDecimal a1=BigDecimal.valueOf(a);
BigDecimal b1=BigDecimal.valueOf(b);
BigDecimal c1=a1.add(b1); //加法
BigDecimal c2 = a1.subtract(b1); //减法
BigDecimal c3 = a1.multiply(b1); //乘法
BigDecimal c4 = a1.divide(b1); //除法
System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
double c5=c1.doubleValue(); //BigDecimal只是手段,最后转回double类型
必须精度运算,否则报错。 (向上保留两位小数)
BigDecimal a1=new BigDecimal(10.0);
BigDecimal a2=new BigDecimal(3.0);
BigDecimal a3 = a1.divide(a2, 2, RoundingMode.HALF_UP);
System.out.println(a3);
jdk8的一些时间api
获取本地日期对象
LocalDate date = LocalDate.now();
date.getYear()//获取年份
date.getMonthValue()//获取月份
date.getDayOfMonth()//一个月得第几天
date.getDayOfYear//一年的第几天
date.getDayOfWeek()//输出英文,星期几
date.getDayOfWeek().getValue()//输出数字礼拜几
date.getMonth() //输出英文月份
date.getMonth().getValue()//输出数字月份
LocalDate ld=LocalDate.of(2000,12,04)//输出指定年月份
//获取本地时间对象
LocalTime nowTime = LocalTime.now(); //获取今天时间
nowTime.getHour();//获取时
nowTime.getMinute();//获取分
nowTime.getSecond();//获取秒
nowTime.getNano();//获取纳秒
LocalTime.of(8,20);//打印 08:20 按着输入打印出时间 后面还可以加秒和纳秒
LocalDateTime(2001,2,23,8,56) //打印出日期和时间
LocalDateTime nowDateTime=new LocalDateTime();
nowDateTime.getYear();//获取年
nowDateTime.getMonthValue();//获取月
nowDateTime.getDayofMonth();//获取日
这个兼容时间和日期的所有api
LocalDate ld=nowDateTime.toLocalDate(); //转成日期对象
LocalTime lt=nowDateTime.toLocalTime(); //转成时间对象
比较日期是否相同
LocalDate birthDay = LocalDate.of(2006,6,14); //封装日期
LocalDate nowDate = LocalDate.now(); //今天的日期
MonthDay of = MonthDay.of(birthDay.getMonthValue(), birthDay.getDayOfMonth());
//封装月和日
MonthDay nowMd = MonthDay.from(nowDate);
System.out.println(nowMd.equals(of));
判断一个日期在另一个日期之前或之后
LocalDate localDate=LocalDate.of(2006,6,6);
LocalDate nowDate = LocalDate.now();
System.out.println(localDate.isAfter(nowDate));
System.out.println(localDate.isBefore(nowDate));
Instance时间戳对象,通过时间戳打印的时间,不是本地时间,需要通过设置本地时区打印本地时间。
Instant instant=Instant.now();
System.out.println(instant);
//设置系统默认时区
ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
System.out.println(zonedDateTime);
DateTimeFormatter可以格式化日期(DateTimeFormatter.ofPattern() ),LocalDateTime.parse()可以解析字符串
LocalDateTime ldf=LocalDateTime.now();
DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println(ldf.format(dtf)); //反向格式化
String format = dtf.format(ldf);//正向格式化
System.out.println("-------------------");
LocalDateTime ldt=LocalDateTime.parse("2000-09-09 16:45:45",dtf); //解析字符串
System.out.println(ldt);
Perion.between()计算日期间隔
LocalDate ld = LocalDate.now();
LocalDate ld2=LocalDate.of(2006,04,9);
Period period = Period.between(ld, ld2);
System.out.println(period);
System.out.println(period.getYears());
System.out.println(period.getMonths());
System.out.println(period.getDays());
Duration.between()计算日期和时间间隔
LocalDateTime today = LocalDateTime.now();
LocalDateTime birthDate = LocalDateTime.of(2021,03,04,23,00,00);
Duration duration = Duration.between(birthDate,today);//第二个参数减第一个参数
System.out.println(duration.toDays());
System.out.println(duration.toHours());
System.out.println(duration.toMinutes());
System.out.println(duration.toMillis());
System.out.println(duration.toNanos());