{"id":1717,"date":"2023-06-01T23:44:32","date_gmt":"2023-06-01T15:44:32","guid":{"rendered":"https:\/\/diuut.com\/?p=1717"},"modified":"2023-06-01T23:47:11","modified_gmt":"2023-06-01T15:47:11","slug":"%e5%a4%9a%e4%b8%aa%e6%97%b6%e9%97%b4%e6%ae%b5%e4%b8%ad%e7%ad%9b%e9%80%89%e8%af%86%e5%88%ab%e5%b9%b6%e8%bf%94%e5%9b%9e%e9%87%8d%e5%8f%a0%e6%97%b6%e9%97%b4%e3%80%90java%e3%80%91","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=1717","title":{"rendered":"\u591a\u4e2a\u65f6\u95f4\u6bb5\u4e2d\u7b5b\u9009\u8bc6\u522b\u5e76\u8fd4\u56de\u91cd\u53e0\u65f6\u95f4\u6bb5\u533a\u95f4\u3010Java\u3011"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42<\/p><cite>\u591a\u4e2a\u65f6\u95f4\u6bb5\u4e2d\uff0c\u7b5b\u9009\u51fa\u91cd\u53e0\u7684\u90e8\u5206\u5e76\u8fd4\u56de\uff0c\u7528\u4e8e\u65f6\u95f4\u6bb5\u91cd\u53e0\u6bd4\u8f83\u6821\u9a8c\u3002<\/cite><\/blockquote>\n\n\n\n<p>\u76f4\u63a5\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u3002\u4e0d\u7528\u518d\u5efa\u4e2a\u5bf9\u8c61\u6765\u5b58\u50a8\u3002\u652f\u6301String\uff0cDate ,LocalDate\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport com.alibaba.fastjson2.JSON;\nimport com.alibaba.nacos.shaded.com.google.common.base.MoreObjects;\n\nimport java.text.DateFormat;\nimport java.text.ParseException;\nimport java.text.SimpleDateFormat;\nimport java.time.Instant;\nimport java.time.LocalDate;\nimport java.time.LocalTime;\nimport java.time.ZoneId;\nimport java.util.*;\n\n\/**\n * \u65f6\u95f4\u6bb5\u91cd\u53e0\u6bd4\u8f83\n *\n * @author Diuut M Duan\n *\/\npublic class TimeBucket {\n\n    private static final ThreadLocal&lt;DateFormat&gt; FORMATS = ThreadLocal.withInitial(() -&gt; new SimpleDateFormat(&quot;yyyy-MM-dd&quot;));\n\n    private final Long dateId;\n    private final Date start;\n\n    private final Date end;\n\n    public TimeBucket(Long dateId, Date start, Date end) {\n        if (start.after(end)) {\n            throw new IllegalArgumentException(&quot;\u65f6\u95f4\u6bb5\u65e0\u6548(\u5f00\u59cb\u65e5\u671f\u9700\u8981\u5c0f\u4e8e\u7ed3\u675f\u65e5\u671f)&quot;);\n        }\n        this.dateId = dateId;\n        this.start = start;\n        this.end = end;\n    }\n\n    public TimeBucket(Date start, Date end) {\n        if (start.after(end)) {\n            throw new IllegalArgumentException(&quot;\u65f6\u95f4\u6bb5\u65e0\u6548(\u5f00\u59cb\u65e5\u671f\u9700\u8981\u5c0f\u4e8e\u7ed3\u675f\u65e5\u671f)&quot;);\n        }\n        this.dateId = 0L;\n        this.start = start;\n        this.end = end;\n    }\n\n    public TimeBucket(Long dateId, String start, String end) throws ParseException {\n        this(dateId, parse(start), parse(end));\n    }\n\n    public TimeBucket(Long dateId, LocalDate start, LocalDate end) {\n        this(dateId, parse(start), parse(end));\n    }\n\n    public TimeBucket(String start, String end) throws ParseException {\n        this(0L, start, end);\n    }\n\n    public TimeBucket(LocalDate start, LocalDate end) {\n        this(0L, start, end);\n    }\n\n    public TimeBucket(long startTime, long endTime) {\n        this(0L, startTime, endTime);\n    }\n\n    public TimeBucket(Long timeId, long startTime, long endTime) {\n        this(timeId, new Date(startTime), new Date(endTime));\n    }\n\n    \/**\n     * TimeBucket\u4f1a\u8fd4\u56de\u91cd\u53e0\u7684\u65f6\u95f4\u6bb5\n     * \u82e5\u8fd4\u56denull\u8bf4\u660e\u6ca1\u6709\u91cd\u53e0\u7684\u65f6\u95f4\u6bb5\n     *\n     * @param buckets \u65f6\u95f4\u6bb5\n     * @return Set&lt;Long&gt; \u51b2\u7a81\u65f6\u95f4\u6bb5ID\n     *\/\n    public static Set&lt;Long&gt; union(TimeBucket... buckets) {\n        List&lt;TimeBucket&gt; timeVos = Arrays.asList(buckets);\n        Set&lt;Long&gt; conflictIds = new HashSet&lt;&gt;();\n        timeVos.sort(new Comparator&lt;TimeBucket&gt;() {\n            @Override\n            public int compare(TimeBucket t1, TimeBucket t2) {\n                return t1.getStart().compareTo(t2.getStart());\n            }\n        });\n        for (int i = 0; i &lt; timeVos.size() - 1; i++) {\n            if(timeVos.get(i).getEndTime() &gt;= timeVos.get(i+1).getStartTime() ) {\n                conflictIds.add(timeVos.get(i).getDateId());\n                conflictIds.add(timeVos.get(i+1).getDateId());\n            }\n        }\n        System.out.println(JSON.toJSONString(conflictIds));\n        return conflictIds;\n    }\n\n\n    public Long getDateId() {\n        return dateId;\n    }\n\n    public Date getStart() {\n        return start;\n    }\n\n    public Date getEnd() {\n        return end;\n    }\n\n    public long getStartTime() {\n        return start.getTime();\n    }\n\n    public long getEndTime() {\n        return end.getTime();\n    }\n\n    private static Date parse(String str) throws ParseException {\n        return FORMATS.get().parse(str);\n    }\n\n    private static Date parse(LocalDate date) {\n        Instant instant = date.atTime(LocalTime.MIDNIGHT).atZone(ZoneId.systemDefault()).toInstant();\n        return Date.from(instant);\n    }\n\n    private static String format(Date str) {\n        return FORMATS.get().format(str);\n    }\n\n    @Override\n    public String toString() {\n        return MoreObjects.toStringHelper(this)\n                .add(&quot;dateId&quot;, dateId)\n                .add(&quot;start&quot;, format(start))\n                .add(&quot;end&quot;, format(end))\n                .toString();\n    }\n}\n<\/pre><\/div>\n\n\n<p>\u6d4b\u8bd5\u7c7b<\/p>\n\n\n\n<p><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nimport com.xxx.sdc.rwa.biz.util.TimeBucket;\nimport org.junit.jupiter.api.Test;\nimport org.springframework.boot.test.context.SpringBootTest;\n\nimport javax.annotation.Resource;\nimport java.text.ParseException;\nimport java.util.List;\nimport java.util.Set;\n\n\/**\n * @author Diuut M Duan\n *\/\n@SpringBootTest\npublic class Tests {\n\n\n    @Test\n    public void TimeBucketTest() throws ParseException {\n        TimeBucket&#91;] buckets = {\n                new TimeBucket(1L,&quot;2018-01-01&quot;, &quot;2018-01-11&quot;),\n                new TimeBucket(2L,&quot;2018-02-02&quot;, &quot;2018-02-22&quot;),\n                new TimeBucket(3L,&quot;2018-03-03&quot;, &quot;2018-03-11&quot;),\n                new TimeBucket(4L,&quot;2018-01-08&quot;, &quot;2018-01-22&quot;),\n                new TimeBucket(5L,&quot;2018-02-21&quot;, &quot;2018-02-28&quot;),\n                new TimeBucket(6L,&quot;2018-03-11&quot;, &quot;2018-03-13&quot;),\n                new TimeBucket(7L,&quot;2018-03-14&quot;, &quot;2018-03-15&quot;),\n                new TimeBucket(8L,&quot;2018-01-10&quot;, &quot;2018-01-30&quot;)\n        };\n        \/\/\u9884\u671f\u7ed3\u679c\uff1a1.4.8.2.5.3.6\n        Set&lt;Long&gt; union = TimeBucket.union(buckets);\n        System.out.println(union);\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u7b5b\u9009\u5904\u7b97\u6cd5\u53c2\u8003\uff1a<a rel=\"noreferrer noopener\" href=\"https:\/\/blog.csdn.net\/qq_41384351\/article\/details\/114578644\" target=\"_blank\">https:\/\/blog.csdn.net\/qq_41384351\/article\/details\/114578644<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42 \u591a\u4e2a\u65f6\u95f4\u6bb5\u4e2d\uff0c\u7b5b\u9009\u51fa\u91cd\u53e0\u7684\u90e8\u5206\u5e76\u8fd4\u56de\uff0c\u7528\u4e8e\u65f6\u95f4\u6bb5\u91cd\u53e0\u6bd4\u8f83\u6821\u9a8c\u3002 \u76f4\u63a5\u4e00\u4e2a\u7c7b\u5b9e\u73b0\u3002\u4e0d\u7528\u518d\u5efa\u4e2a\u5bf9\u8c61\u6765\u5b58\u50a8\u3002\u652f<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=1717\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">\u591a\u4e2a\u65f6\u95f4\u6bb5\u4e2d\u7b5b\u9009\u8bc6\u522b\u5e76\u8fd4\u56de\u91cd\u53e0\u65f6\u95f4\u6bb5\u533a\u95f4\u3010Java\u3011<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":657,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[37,12,53],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1717"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1717"}],"version-history":[{"count":4,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1717\/revisions"}],"predecessor-version":[{"id":1721,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/1717\/revisions\/1721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/657"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1717"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1717"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1717"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}