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

推荐订阅源

T
Troy Hunt's Blog
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
F
Full Disclosure
N
Netflix TechBlog - Medium
C
Check Point Blog
Recent Announcements
Recent Announcements
MyScale Blog
MyScale Blog
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
H
Help Net Security
F
Fortinet All Blogs
P
Proofpoint News Feed
I
InfoQ
Stack Overflow Blog
Stack Overflow Blog
L
LangChain Blog
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Vercel News
Vercel News
A
About on SuperTechFans
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
B
Blog RSS Feed
S
Securelist
Y
Y Combinator Blog
C
Cybersecurity and Infrastructure Security Agency CISA
D
DataBreaches.Net
B
Blog
The Hacker News
The Hacker News
Security Latest
Security Latest
P
Privacy & Cybersecurity Law Blog
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
Martin Fowler
Martin Fowler
人人都是产品经理
人人都是产品经理
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
Latest news
Latest news
月光博客
月光博客
aimingoo的专栏
aimingoo的专栏
Apple Machine Learning Research
Apple Machine Learning Research
J
Java Code Geeks
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
H
Heimdal Security Blog
C
Cisco Blogs

博客园 - 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));
  }
}