Java 8 日期新特性

转载:https://www.cnblogs.com/binghe001/p/13028868.html

日期格式的互相转换

转载:https://www.cnblogs.com/puke/p/11314431.html

  1. DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  2. DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
  3. LocalDateTime localDateTime = LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1);
  4. LocalDate localDate = LocalDate.parse("2019-07-31",dateTimeFormatter2);
  5. Date date = Date.from(LocalDateTime.parse("2019-07-31 00:00:00",dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant());
  6. String strDateTime = "2019-07-31 00:00:00";
  7. String strDate = "2019-07-31";
  8. Long timestamp=1564502400000l;
  9. /** LocalDateTime 转 LocalDate */
  10. System.out.println("LocalDateTime 转 LocalDate: "+localDateTime.toLocalDate());
  11. /** LocalDateTime 转 Long */
  12. System.out.println("LocalDateTime 转 Long: "+localDateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli());
  13. /** LocalDateTime 转 Date */
  14. System.out.println("LocalDateTime 转 Date: "+Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant()));
  15. /** LocalDateTime 转 String */
  16. System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter1));
  17. System.out.println("-------------------------------");
  18. /** LocalDate 转 LocalDateTime */
  19. System.out.println("LocalDate 转 LocalDateTime: "+LocalDateTime.of(localDate,LocalTime.parse("00:00:00")));
  20. /** LocalDate 转 Long */
  21. System.out.println("LocalDate 转 Long: "+localDate.atStartOfDay(ZoneId.systemDefault()).toInstant().toEpochMilli());
  22. /** LocalDate 转 Date */
  23. System.out.println("LocalDate 转 Date: "+Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
  24. /** LocalDate 转 String */
  25. System.out.println("LocalDateTime 转 String: "+localDateTime.format(dateTimeFormatter2));
  26. System.out.println("-------------------------------");
  27. /** Date 转 LocalDateTime */
  28. System.out.println("Date 转 LocalDateTime: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()));
  29. /** Date 转 Long */
  30. System.out.println("Date 转 Long: "+date.getTime());
  31. /** Date 转 LocalDate */
  32. System.out.println("Date 转 LocalDate: "+LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate());
  33. /** Date 转 String */
  34. SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS" );
  35. System.out.println("Date 转 String: "+sdf.format(date));
  36. System.out.println("-------------------------------");
  37. /** String 转 LocalDateTime */
  38. System.out.println("String 转 LocalDateTime: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1));
  39. /** String 转 LocalDate */
  40. System.out.println("String 转 LocalDate: "+LocalDateTime.parse(strDateTime,dateTimeFormatter1).toLocalDate());
  41. System.out.println("String 转 LocalDate: "+LocalDate.parse(strDate,dateTimeFormatter2));
  42. /** String 转 Date */
  43. System.out.println("String 转 Date: "+Date.from(LocalDateTime.parse(strDateTime,dateTimeFormatter1).atZone(ZoneId.systemDefault()).toInstant()));
  44. System.out.println("-------------------------------");
  45. /** Long 转 LocalDateTime */
  46. System.out.println("Long 转 LocalDateTime:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()));
  47. /** Long 转 LocalDate */
  48. System.out.println("Long 转 LocalDate:"+LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault()).toLocalDate());
  49. LocalDateTime now = LocalDateTime.now();//2020-09-11T09:41:01.547
  50. LocalDateTime DiscountStartTime = now.minusDays(3);//减法 2020-09-08T09:41:01.547
  51. LocalDateTime DiscountEndTime = now.plusDays(3);//加法 2020-09-14T09:41:01.547
  52. // now>DiscountStartTime && DiscountEndTime>now
  53. // DiscountEndTime>now>DiscountStartTime
  54. if(now.isAfter(DiscountStartTime) && DiscountEndTime.isAfter(now)){
  55. true;//输出true
  56. }

日期处理

  1. @Test
  2. public void contextLoads() {
  3. LocalDate today = LocalDate.now(); // 当前日期 年 月 日
  4. LocalTime minTime = LocalTime.MIN; // 最小时间 00:00:00
  5. LocalDate twoDaysLaterDate = today.plusDays(2); // 两天之后的日期 年 月 日
  6. LocalTime maxTime = LocalTime.MAX; // 最大时间 23:59:59
  7. // 将日期和时间组合起来,并转换成想要的格式
  8. String beginDate = LocalDateTime.of(today, minTime).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
  9. String endDate = LocalDateTime.of(twoDaysLaterDate, maxTime).format(DateTimeFormatter
  10. .ofPattern("yyyy-MM-dd HH:mm:ss"));
  11. System.out.println("开始时间:" + beginDate);
  12. System.out.println("结束时间:" + endDate);
  13. }

获得一个月最大天数的方法(备忘)

转载:https://www.cnblogs.com/relucent/p/4566582.html

获取某段时间内的周几的日期

  1. /**
  2. * 获取某段时间内的周几的日期
  3. *
  4. * @param dataBegin 开始日期
  5. * @param dataEnd 结束日期
  6. * @param weekDays 获取周几,1-6代表周一到周六。0代表周日
  7. * @return 返回日期List
  8. */
  9. public static List<String> getDayOfWeekWithinDateInterval(String dataBegin, String dataEnd, int weekDays) {
  10. SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
  11. List<String> dateResult = new ArrayList<>();
  12. Calendar cal = Calendar.getInstance();
  13. String[] dateInterval = {dataBegin, dataEnd};
  14. Date[] dates = new Date[dateInterval.length];
  15. for (int i = 0; i < dateInterval.length; i++) {
  16. String[] ymd = dateInterval[i].split("[^\\d]+");
  17. cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));
  18. dates[i] = cal.getTime();
  19. }
  20. for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {
  21. cal.setTime(date);
  22. if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {
  23. String format = sd.format(date);
  24. dateResult.add(format);
  25. }
  26. cal.add(Calendar.DATE, 1);
  27. date = cal.getTime();
  28. }
  29. return dateResult;
  30. }

获取两个指定日期之间的所有月份

转载:https://www.cnblogs.com/beanbag/p/9719907.html

  1. public void getAmongstMonth() {
  2. String y1 = "2022-03";// 开始时间
  3. String y2 = "2022-05";// 结束时间
  4. try {
  5. Date startDate = new SimpleDateFormat("yyyy-MM").parse(y1);
  6. Date endDate = new SimpleDateFormat("yyyy-MM").parse(y2);
  7. Calendar calendar = Calendar.getInstance();
  8. calendar.setTime(startDate);
  9. // 获取开始年份和开始月份
  10. int startYear = calendar.get(Calendar.YEAR);
  11. int startMonth = calendar.get(Calendar.MONTH);
  12. // 获取结束年份和结束月份
  13. calendar.setTime(endDate);
  14. int endYear = calendar.get(Calendar.YEAR);
  15. int endMonth = calendar.get(Calendar.MONTH);
  16. //
  17. List<String> list = new ArrayList<String>();
  18. for (int i = startYear; i <= endYear; i++) {
  19. String date = "";
  20. if (startYear == endYear) {
  21. for (int j = startMonth; j <= endMonth; j++) {
  22. if (j < 9) {
  23. date = i + "-0" + (j + 1);
  24. } else {
  25. date = i + "-" + (j + 1);
  26. }
  27. list.add(date);
  28. }
  29. } else {
  30. if (i == startYear) {
  31. for (int j = startMonth; j < 12; j++) {
  32. if (j < 9) {
  33. date = i + "-0" + (j + 1);
  34. } else {
  35. date = i + "-" + (j + 1);
  36. }
  37. list.add(date);
  38. }
  39. } else if (i == endYear) {
  40. for (int j = 0; j <= endMonth; j++) {
  41. if (j < 9) {
  42. date = i + "-0" + (j + 1);
  43. } else {
  44. date = i + "-" + (j + 1);
  45. }
  46. list.add(date);
  47. }
  48. } else {
  49. for (int j = 0; j < 12; j++) {
  50. if (j < 9) {
  51. date = i + "-0" + (j + 1);
  52. } else {
  53. date = i + "-" + (j + 1);
  54. }
  55. list.add(date);
  56. }
  57. }
  58. }
  59. }
  60. // 所有的月份已经准备好
  61. //System.out.println(list);
  62. for (int i = 0; i < list.size(); i++) {
  63. System.out.println(list.get(i));
  64. }
  65. System.out.println("里面有" + list.size() + "个月份");
  66. } catch (Exception e) {
  67. e.printStackTrace();
  68. }
  69. }

对当前时间进行加减

  1. // 将分钟转换为毫秒
  2. int fiveMinutes = 5 * 60 * 1000;
  3. Date now = new Date();
  4. // 5分钟之后
  5. Date fiveMinutesAgo = new Date(now.getTime() + fiveMinutes);
  6. // 5分钟之前
  7. Date fiveMinutesAgo = new Date(now.getTime() - fiveMinutes);

两个日期中间有多少天

  1. /**
  2. * 两个日期中间有多少天
  3. *
  4. * @param startDate
  5. * @param endDate
  6. * @return
  7. */
  8. public static Long getAmongstDays(Date startDate, Date endDate) {
  9. long intervalMills = endDate.getTime() - startDate.getTime();
  10. return intervalMills / (1000 * 60 * 60 * 24);
  11. }