苏州营销网站建设,网络怎么推广,贵阳的网站建设公司,蓝色 宽屏 网站 模板下载工作中经常会遇到对时间日期进行处理的业务#xff0c;像日期类的API个人觉得不需要背#xff0c;需要的时候去查资料就行。我整理了Java8之前及之后日期类常用的时间日期处理方法#xff0c;方便工作需要时查找#xff0c;觉得有用的朋友可以收藏。 一、日期格式化和解析 …工作中经常会遇到对时间日期进行处理的业务像日期类的API个人觉得不需要背需要的时候去查资料就行。我整理了Java8之前及之后日期类常用的时间日期处理方法方便工作需要时查找觉得有用的朋友可以收藏。 一、日期格式化和解析
java.util.Date类
格式化操作
public void oldFormat(){Date now new Date();//format yyyy-MM-ddSimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);String date sdf.format(now);System.out.println(String.format(date format : %s, date));//format HH:mm:ssSimpleDateFormat sdft new SimpleDateFormat(HH:mm:ss);String time sdft.format(now);System.out.println(String.format(time format : %s, time));//format yyyy-MM-dd HH:mm:ssSimpleDateFormat sdfdt new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);String datetime sdfdt.format(now);System.out.println(String.format(dateTime format : %s, datetime));
}解析字符串转日期格式
//已弃用
Date date new Date(2021-01-26);
//替换为
SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);
Date date1 sdf.parse(2021-01-26);LocalDate、LocalTime、LocalDateTIme类
格式化
public void newFormat(){//format yyyy-MM-ddLocalDate date LocalDate.now();System.out.println(String.format(date format : %s, date));//format HH:mm:ssLocalTime time LocalTime.now().withNano(0);System.out.println(String.format(time format : %s, time));//format yyyy-MM-dd HH:mm:ssLocalDateTime dateTime LocalDateTime.now();DateTimeFormatter dateTimeFormatter DateTimeFormatter.ofPattern(yyyy-MM-dd HH:mm:ss);String dateTimeStr dateTime.format(dateTimeFormatter);System.out.println(String.format(dateTime format : %s, dateTimeStr));
}解析字符串转日期格式
LocalDate date LocalDate.of(2021, 1, 26);
// 或
LocalDate date1 LocalDate.parse(2021-01-26);
System.out.println(date);LocalDateTime dateTime LocalDateTime.of(2021, 1, 26, 12, 12, 22);
// 或
LocalDateTime dateTime1 LocalDateTime.parse(2021-01-26T12:12:22);
System.out.println(dateTime);LocalTime time LocalTime.of(12, 12, 22);
// 或
LocalTime time1 LocalTime.parse(12:12:22);
System.out.println(time);二、日期对象转换
Date转LocalDate类
如果要将java.util.Date转换为java.time.LocalDate可以使用以下步骤
1将java.util.Date转换为ZonedDateTime。
2使用它的toLocalDate()方法从ZonedDateTime获取LocalDate。
Date date new Date();
Instant instant date.toInstant();
ZoneId zoneId ZoneId.systemDefault();// atZone()方法返回在指定时区从此Instant生成的ZonedDateTime。
LocalDate localDate instant.atZone(zoneId).toLocalDate();
System.out.println(Date date);
System.out.println(LocalDate localDate);LocalDate转Date类
如果要将LocalDate转换回java.util.Date可以使用以下步骤
1使用ZonedDateTime将LocalDate转换为Instant。
2使用from()方法从Instant对象获取Date的实例
ZoneId zoneId ZoneId.systemDefault();
LocalDate localDate LocalDate.now();
ZonedDateTime zdt localDate.atStartOfDay(zoneId);Date date Date.from(zdt.toInstant());System.out.println(LocalDate localDate);
System.out.println(Date date);三、日期间隔计算
java.util.Date类 //算两个日期间隔多少天计算间隔多少年多少月方法类似String dates1 2022-08-23;String dates2 2022-02-26;SimpleDateFormat format new SimpleDateFormat(yyyy-MM-dd);Date date1 format.parse(dates1);Date date2 format.parse(dates2);int day (int) ((date1.getTime() - date2.getTime()) / (1000 * 3600 * 24));System.out.println(dates1 和 dates2 相差 day 天);//结果2022-08-23和2022-02-26相差178天java.time.LocalDate类
//算两个日期间隔多少天计算间隔多少年多少月
LocalDate date1 LocalDate.parse(2022-08-23);
LocalDate date2 LocalDate.parse(2022-02-26);
Period period Period.between(date1, date2);
System.out.println(date1 到 date2 相隔 period.getYears() 年 period.getMonths() 月 period.getDays() 天);
//打印结果是date1 到 date2 相隔0年-5月-25天
//这里period.getDays()得到的天是抛去年月以外的天数并不是总天数//如果要获取纯粹的总天数应该用下面的方法toEpochDay返回距离1970年1月1日的天数的long值
long day date1.toEpochDay() - date2.toEpochDay();
System.out.println(date1 和 date2 相差 day 天);
//打印结果2022-08-23和2022-02-26相差178天Java8中引入了两个与日期相关的新类
Period - 计算两个“日期”间隔的类Duration - 计算两个“时间”间隔的类
Period 类与 Duration 类都是一段持续时间的概念,如果需要对比时间它们就需要一个固定的时间值,所以就需要 LocalDate 类与 Instant 、LocalTime、LocalDateTime类来配合它们使用:
Period 对应使用 LocalDate 它们的作用范围域都是日期(年/月/日) Duration 对应使用 Instant、LocalTime、LocalDateTime它们的作用范围域都是时间(天/时/分/秒/毫秒/纳秒)
Duration常用API LocalDateTime start LocalDateTime.parse(2022-12-03T10:15:30);
LocalDateTime end LocalDateTime.parse(2023-12-01T10:25:33);//between的用法是end-start的时间若start的时间大于end的时间则所有的值是负的
Duration duration Duration.between(start, end);String timeString duration.toString(); //此持续时间的字符串表示形式,使用基于ISO-8601秒*的表示形式,例如 PT8H6M12.345S
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());
System.out.println(timeString时间timeString);//isNegative返回Duration实例对象是否为负
System.out.println(Duration.between(start, end).isNegative());//false end-start为正所以此处返回false
System.out.println(Duration.between(end, start).isNegative());//true start-end为负所以此处返回true
System.out.println(Duration.between(start, start).isNegative());//false start-start为0所以此处为false执行结果
相差的天数363
相差的小时8712
相差的分钟522730
相差的毫秒31363803000
相差的纳秒31363803000000000
timeString时间PT8712H10M3S
false
true
falsePeriod常用API LocalDate start LocalDate.of(2022,12,3);
LocalDate end LocalDate.of(2023,12,1);
Period period Period.between(start,end);
System.out.println(两个时间之间的差值 年period.getYears()月period.getMonths()日period.getDays());结果
两个时间之间的差值 年0月11日28ChronoUnit也可以计算两个单元之间的天数、月数或年数。
LocalDate start LocalDate.of(2022,12,3);
LocalDate end LocalDate.of(2023,12,1);
long day ChronoUnit.DAYS.between(start , end );
System.out.println(相差天数 day);
long month ChronoUnit.MONTHS.between(start , end );
System.out.println(相差月数 month);
long year ChronoUnit.YEARS.between(start , end );
System.out.println(相差年数 year);// 结果
相差天数363
相差月数11
相差年数0四、获取指定日期
java.util.Date类
下面仅以部分日期为例其他单位年、月、日、1/2 日、时等等大同小异。
//一周后的日期
SimpleDateFormat formatDate new SimpleDateFormat(yyyy-MM-dd);
Calendar ca Calendar.getInstance();
ca.add(Calendar.DATE, 7);
Date d ca.getTime();
String after formatDate.format(d);
System.out.println(当前日期 formatDate.format(new Date()));
System.out.println(一周后日期 after);
//当前日期2023-08-26
//一周后日期2023-09-02SimpleDateFormat format new SimpleDateFormat(yyyy-MM-dd);
//获取当前月第一天
Calendar c Calendar.getInstance();
c.set(Calendar.DAY_OF_MONTH, 1);
String first format.format(c.getTime());
System.out.println(first day: first);//获取当前月最后一天
Calendar ca Calendar.getInstance();
ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
String last format.format(ca.getTime());
System.out.println(last day: last);//当年最后一天
Calendar currCal Calendar.getInstance();
Calendar calendar Calendar.getInstance();
calendar.clear();
calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
calendar.roll(Calendar.DAY_OF_YEAR, -1);
Date time calendar.getTime();
System.out.println(last day: format.format(time));//first day:2023-08-01
//last day:2023-08-31
//last day:2023-12-31java.time.LocalDate类
下面仅以部分日期为例其他单位年、月、日、1/2 日、时等等大同小异。另外这些单位都在 java.time.temporal.ChronoUnit 枚举中定义。
//一周后的日期
LocalDate localDate LocalDate.now();
//方法1
LocalDate after localDate.plus(1, ChronoUnit.WEEKS);
//方法2
LocalDate after2 localDate.plusWeeks(1);
System.out.println(一周后日期 after);
System.out.println(一周后日期 after2);
//一周后日期2023-09-02
//一周后日期2023-09-02LocalDate today LocalDate.now();
//获取当前月第一天
LocalDate firstDayOfThisMonth today.with(TemporalAdjusters.firstDayOfMonth());
// 取本月最后一天
LocalDate lastDayOfThisMonth today.with(TemporalAdjusters.lastDayOfMonth());
//取下一天
LocalDate nextDay lastDayOfThisMonth.plusDays(1);
//当年最后一天
LocalDate lastDay today.with(TemporalAdjusters.lastDayOfYear());
//2023年最后一个周日
LocalDate lastMondayOf2021 LocalDate.parse(2023-12-31).with(TemporalAdjusters.lastInMonth(DayOfWeek.SUNDAY));五、时区
java.util.Date类
java.util.Date 对象实质上存的是 1970 年 1 月 1 日 0 点 GMT至 Date 对象所表示时刻所经过的毫秒数。也就是说不管在哪个时区 new Date它记录的毫秒数都一样和时区无关。但在使用上应该把它转换成当地时间这就涉及到了时间的国际化。 java.util.Date 本身并不支持国际化需要借助 TimeZone。
// 北京时间Sat Aug 26 14:52:29 CST 2023
Date date new Date();SimpleDateFormat bjSdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
// 北京时区
bjSdf.setTimeZone(TimeZone.getTimeZone(Asia/Shanghai));
System.out.println(毫秒数: date.getTime() , 北京时间: bjSdf.format(date));
// 东京时区
SimpleDateFormat tokyoSdf new SimpleDateFormat(yyyy-MM-dd HH:mm:ss);
tokyoSdf.setTimeZone(TimeZone.getTimeZone(Asia/Tokyo)); // 设置东京时区
System.out.println(毫秒数: date.getTime() , 东京时间: tokyoSdf.format(date));
// 如果直接print会自动转成当前时区的时间
System.out.println(date);//毫秒数:1693032749431, 北京时间:2023-08-26 14:52:29
//毫秒数:1693032749431, 东京时间:2023-08-26 15:52:29
//Sat Aug 26 14:52:29 CST 2023ZonedDateTime类
Java8引入了java.time.ZonedDateTime 来表示带时区的时间。它可以看成是 LocalDateTime ZoneId。
//当前时区时间
ZonedDateTime zonedDateTime ZonedDateTime.now();
System.out.println(当前时区时间: zonedDateTime);//东京时间
ZoneId zoneId ZoneId.of(ZoneId.SHORT_IDS.get(JST));
ZonedDateTime tokyoTime zonedDateTime.withZoneSameInstant(zoneId);
System.out.println(东京时间: tokyoTime);// ZonedDateTime 转 LocalDateTime
LocalDateTime localDateTime tokyoTime.toLocalDateTime();
System.out.println(东京时间转当地时间: localDateTime);//LocalDateTime 转 ZonedDateTime
ZonedDateTime localZoned localDateTime.atZone(ZoneId.systemDefault());
System.out.println(本地时区时间: localZoned);当前时区时间: 2023-08-26T14:55:47.66908:00[Asia/Shanghai]
东京时间: 2023-08-26T15:55:47.66909:00[Asia/Tokyo]
东京时间转当地时间: 2023-08-26T15:55:47.669
本地时区时间: 2023-08-26T15:55:47.66908:00[Asia/Shanghai]后续遇到常用的API会继续补充。。。
好了看到了这里觉得有用的朋友们点个赞吧博主整理资料还是很累的需要一点正反馈。