惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
美团技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
aimingoo的专栏
aimingoo的专栏
腾讯CDC
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
F
Fortinet All Blogs
G
Google Developers Blog
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园_首页
B
Blog RSS Feed
D
Docker
M
MIT News - Artificial intelligence
爱范儿
爱范儿
I
InfoQ

博客园 - BigOrang

在vim中搜索关键字 linux top快捷键 druid 获取数据库连接失败,一直wait.DruidDataSource.takeLast -Xmx3G -Xms2G 在已经指定了最小内存2G后,启动的时候,就会直接分配2G给jvm吗 ?还是动态从1m到2G逐步分配的 java8类加载器示例&类加载1.8和1.8+的区别 windows查看端口占用 vmware Docker 设置代理 腾讯云域名托管到 cloudflare nginx 代理eureka后css/js/fonts无法访问 docker 基础镜像损坏 一起来找bug茬-01 mysql SHOW PROFILE 将所有容器docker都重启, 但是不重启mysql 正则 .*? 和 .* 的区别是什么 nginx打印所有配置内容 NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder kubesphere org.tmatesoft.svn.core.SVNException: svn: E160013: '/leifengyang/yygh-parent.git' path not found: 404 Not Found (https://gitee.com) 布隆过滤器原理及应用场景 linux中,使用alias, 应该在/etc/bashrc 中写,还是~/.bashrc中写,哪个更好
java date 时间最大连续天数
BigOrang · 2024-03-07 · via 博客园 - BigOrang

java localdate,date 时间最大连续天数

 
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.stream.Collectors;

public class DateUtils {
  public static final String DAY_FORMAT = "yyyy-MM-dd";
  
  public static Date getDateByPattern(String time, String pattern) {
    if (time == null || time.isEmpty()) {
      return null;
    }
    try {
      SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.ENGLISH);
      sdf.setLenient(false);
      return sdf.parse(time);
    } catch (Exception ex) {
      // log.error
      return null;
    }
  }
  
  public static Date addDays(Date time, Integer day) {
    try {
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
      Calendar cd = Calendar.getInstance();
      cd.setTime(time);
      cd.add(Calendar.DATE, day);
      return getDateByPattern(sdf.format(cd.getTime()), DAY_FORMAT);
    } catch (Exception ex) {
      //log.error("日期增加自然天出错:{}", ex);
      return null;
    }
  }
  
  
  public static int continuousDayDate(List<Date> dateList) {
    if (dateList == null || dateList.isEmpty()) {
      //log.error("参数有误,连续天数为0  dayList{}" , dateList);
      return 0;
    }
    
    List<LocalDate> datetimeList = dateList.stream()
      .map(e -> LocalDateTime.ofInstant(e.toInstant(), ZoneId.systemDefault()).toLocalDate())
      .sorted(LocalDate::compareTo)
      .collect(Collectors.toList());
    return continuousDayLocalDate(datetimeList);
  }
  
  public static int continuousDayLocalDate(List<LocalDate> dateList) {
    if (dateList == null || dateList.size() == 0) {
      return 0;
    }
    if (dateList.size() == 1) {
      return 1;
    }

    dateList = dateList.stream().filter(Objects::nonNull).sorted(LocalDate::compareTo).collect(Collectors.toList());
    int maxContinuousDay = 1;
    int continuousDay = 1;
    
    for (int i = 1; i < dateList.size(); i++) {
      LocalDate date = dateList.get(i);
      LocalDate previousDate = dateList.get(i - 1);
      if (date.minusDays(1).equals(previousDate)) {
        continuousDay++;
        if (continuousDay > maxContinuousDay) {
          maxContinuousDay = continuousDay;
        }
      } else {
        continuousDay = 1;
      }
    }
    return maxContinuousDay;
  }
  
  public static void main(String[] args) {
    List<Date> dates = new ArrayList<>();
    Date date = new Date();
    dates.add(addDays(date, 2));
    dates.add(addDays(date, 3));
    System.out.println(continuousDayDate(dates));
    
    List<LocalDate> list = new ArrayList<>();
    list.add(LocalDate.now());
    list.add(LocalDate.now().plusDays(1));
    list.add(LocalDate.now().plusDays(2));
    System.out.println(continuousDayLocalDate(list));
  }
}