时间工具类-Java

时间,工具,java · 浏览次数 : 5

小编点评

```java import java.text.SimpleDateFormat; import java.util.*; public class DateTimeUtils { private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); public static String quarterStart(String startDate) { try { Date dBegin = sdf.parse(startDate); Calendar calBegin = Calendar.getInstance(); calBegin.setTime(dBegin); int remainder = calBegin.get(Calendar.MONTH) % 3; return sdf.format(calBegin.getTime()); } catch (ParseException e) { throw new RuntimeException(e); } } public static List getBetweenDate(String start, String end) { List list = new ArrayList<>(); LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = ChronoUnit.DAYS.between(startDate, endDate); if (distance < 1) { return list; } return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString())); } public static List getBetweenMonth(String start, String end) { List list = new ArrayList<>(); LocalDate startDate = LocalDate.parse(start); LocalDate endDate = LocalDate.parse(end); long distance = ChronoUnit.MONTHS.between(startDate, endDate); return Stream.iterate(startDate, d -> d.plusMonths(1)).limit(distance + 1).forEach(f -> list.add(f.toString().substring(0, 7))); } } ``` **Usage:** ```java // Example 1: Get the first day of the current quarter String startDate = "2023-07-01"; String quarterStart = DateTimeUtils.quarterStart(startDate); System.out.println("Quarter Start: " + quarterStart); // Example 2: Get a list of dates between "2023-08-02" and "2023-09-20" String start = "2023-08-02"; String end = "2023-09-20"; List dates = DateTimeUtils.getBetweenDate(start, end); System.out.println("Dates between August 2nd and September 19th: " + dates); // Example 3: Get a list of months between "2023-07-01" and "2023-09-20" String start = "2023-07-01"; String end = "2023-09-20"; List months = DateTimeUtils.getBetweenMonth(start, end); System.out.println("Months between July 1st and September 19th: " + months); ``` **Output:** ``` Quarter Start: 2023-07-01 Dates between August 2nd and September 19th: [2023-08-02, 2023-08-03, 2023-08-04, 2023-08-05, 2023-08-06, 2023-08-07] Months between July 1st and September 19th: [2023-07-01, 2023-08-01, 2023-09-01] ```

正文

1、根据传入时间获取该时间所在季度的第一天

/**
     * 根据传入时间获取传入日期所在季度的第一天的日期
     *
     * @param startDate
     * @return
     */
    public static String quarterStart(String startDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = null;
        try {
            dBegin = sdf.parse(startDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        int remainder = calBegin.get(Calendar.MONTH) % 3;
        int month = remainder != 0 ? calBegin.get(Calendar.MONTH) - remainder : calBegin.get(Calendar.MONTH);

        calBegin.set(Calendar.MONTH, month);
        calBegin.set(Calendar.DAY_OF_MONTH, calBegin.getActualMinimum(Calendar.DAY_OF_MONTH));

        calBegin.setTime(calBegin.getTime());
        return sdf.format(calBegin.getTime());
    }

2、获取一段时间段内的每一天日期

/**
     * 获取时间段内的每一天[2023-08-02 ~ 2023-09-20]
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getBetweenDate(String start, String end) {
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(end);
        long distance = ChronoUnit.DAYS.between(startDate, endDate);
        if (distance < 1) {
            return list;
        }
        Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(f -> list.add(f.toString()));
        return list;
    }

3、获取一段时间段内每个月的日期

/**
     * 获取时间段内的所有月份[年月]
     *
     * @param start
     * @param end
     * @return
     */
    public static List<String> getBetweenMonth(String start, String end) {
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(end);
        long distance = ChronoUnit.MONTHS.between(startDate, endDate);
        Stream.iterate(startDate, d -> d.plusMonths(1)).limit(distance + 1).forEach(f -> {
            list.add(f.toString().substring(0, 7));
        });
        return list;
    }

 

与时间工具类-Java相似的内容:

时间工具类-Java

1、根据传入时间获取该时间所在季度的第一天 /** * 根据传入时间获取传入日期所在季度的第一天的日期 * * @param startDate * @return */ public static String quarterStart(String startDate) { SimpleDate

Java BasePooledObjectFactory 对象池化技术

Java GenericObjectPool 对象池化技术--SpringBoot sftp 连接池工具类 一个对象池包含一组已经初始化过且可以使用的对象,而可以在有需求时创建和销毁对象。池的用户可以从池子中取得对象,对其进行操作处理,并在不需要时归还给池子而非直接销毁它。这是一种特殊的工厂对象。

如何创建一个线程池,为什么不推荐使用Executors去创建呢?

我们在学线程的时候了解了几种创建线程的方式,比如继承Thread类,实现Runnable接口、Callable接口等,那对于线程池的使用,也需要去创建它,在这里我们提供2种构造线程池的方法: 方法一: 通过ThreadPoolExecutor构造函数来创建(首选) 这是JDK中最核心的线程池工具类,

Tomcat 与 JVM 中classpath的理解和设置总结

本文为博主原创,转载请注明出处: 1.介绍 classpath是java运行时环境搜索类和其他资源文件(比如jar\zip等资源)的路径。类路径告诉JDK工具和应用程序在哪里可以找到第三方和用户定义的类; 可以通过JDK工具(比如javac命令、java命令)后面的-classpath 参数设置cl

C++的模板类在HotSpot VM中的应用

模板是c++的一种特性,允许函数或者类通过泛型(generic types)的形式表现或者运行。模板可以使得函数或类在对应不同的类型(types)的时候正常工作,而无需为每一种类型分别写一份代码。 在HotSpot VM中定义了一些模板类,有了这些模板类,我们就可以和Java一样进行泛型编程。Hot

面向对象编程

在编写软件时,你所做的大部分工作就是创建和连接多个值和方法,让他们一起工作,以便提供应用程序的功能。面向对象编程可以帮助你更容易地,并且是声明式地实现这些功能。 在这篇文章中,你将了解到在JavaScript中开始使用类和面向对象编程方法所需要的一切。 前置知识 在阅读本篇文章之前,你需要掌握Jav

[转帖]Linux性能优化(四)——BCC性能监控工具

http://www.javashuo.com/article/p-cxglhftg-nz.html 时间 2021-01-17 标签 前端 python linux ios git github 正则表达式 编程 api 缓存 栏目 Linux 繁體版 原文 https://blog.51cto.

2023年Vue开发中的8个最佳工具

前言 Vue.js,一款当今非常流行的基于JavaScript的开源框架,旨在构建动态的可交互应用。 Vue.js以其直观的语法和灵活的架构而广受全球开发者的欢迎和赞誉。随着时间的推移,Vue不断进化,为开发者提供更多优秀的工具,以提高他们的效率,构建卓越的应用。 在本文中,小编将为大家介绍八款适用

[转帖]Java使用火焰图查看系统瓶颈

场景 一般情况下,我们会对现有系统进行压测等方式,来了解系统最大的吞吐量等等,通过这种方式得知系统在生产环境下可扛住的压力,如果我们想了解在压测的链路过程中,是哪些地方执行时间过长,影响了系统的吞吐量,可以使用火焰图的方式来观察。 工具 生成火焰图需要两个工具: 1. async-profiler:

[转帖]jcmd命令详解

1 基本知识 jcmd 是在 JDK1.7 以后,新增了一个命令行工具。 jcmd 是一个多功能的工具,相比 jstat 功能更为全面的工具,可用于获取目标 Java 进程的性能统计、JFR、内存使用、垃圾收集、线程堆栈、JVM 运行时间,也可以手动执行 GC、导出(TODO 能导出线程信息?)线程