查找今天日期之前和之后的日期

如何解决查找今天日期之前和之后的日期

我已经创建了一个财务应用程序,并且希望该应用程序向用户显示财务月份。财务月通常代表用户获得薪水或最高收入的日子。那天可能是一个月的第一天,该月的最后一天,或者仅仅是每个月的15号。 该值可以在设置中配置。

我尝试运行在ViewModel中调用的两个方法getFirstDayOfMonthgetLastDayOfMonth

为了更好地理解上下文,这里有一些示例,我们将以今天的日期作为参考。 2020年9月9日。“输入值”是我从设置中读取的值(用户可以从中选择),它是1到31之间的一个数字。

示例:输入:5输出:2020年9月5日开始于00:00,2020年10月4日在结束23:59:59

================

输入:31输出:2020年8月31日开始00:00和2020年9月30日23:59:59

要注意的是,如果月份中没有该天,它将获得最左侧的日期,例如,如果选择第一天为31,并且该月份有30天,则将计算30天。第一天,也包括结束日期,如果选择了31天并且我们在2月,并且只有28天,那么将会选择28天。

直到现在我有了这段代码,但是我认为它可以改进,并且无法按预期工作。

fun getFirstDayOfMonth(date: LocalDateTime): Long {
    var tempDate = date
    val firstDayOfMonth = lastDay?.filter { it.isDigit() }!!.toInt()

    if (firstDayOfMonth < tempDate.dayOfMonth) {
        tempDate = tempDate.withDayOfMonth(firstDayOfMonth)
    } else if (firstDayOfMonth > tempDate.dayOfMonth) {
        tempDate = tempDate.minusMonths(1)
        if (tempDate.monthValue == 12) {
//I don't know why minusMonths does not work in the same way as plusMonths,when I write .plusMonths(1) it also change the year if I am in december,with minusMonths if I am in January it does not change the year to minus one year.
            tempDate = tempDate.minusYears(1)
        }

        if (firstDayOfMonth > tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth) {
            tempDate.withDayOfMonth(tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth)
        } else {
            tempDate = tempDate.withDayOfMonth(firstDayOfMonth)
        }
    }

    return tempDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
}

fun getLastDayOfMonth(date: LocalDateTime): Long {
    var tempDate = date
    val firstDayOfMonth = lastDay?.filter { it.isDigit() }!!.toInt()

    if (firstDayOfMonth > tempDate.dayOfMonth && firstDayOfMonth <= tempDate.with(
            TemporalAdjusters.lastDayOfMonth()
        ).dayOfMonth
    ) {
        tempDate = tempDate.withDayOfMonth(firstDayOfMonth).minusDays(1)
    } else {
        tempDate = tempDate.plusMonths(1)
        if (firstDayOfMonth > tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth) {
            tempDate.withDayOfMonth(tempDate.with(TemporalAdjusters.lastDayOfMonth()).dayOfMonth)
        } else {
            tempDate = tempDate.withDayOfMonth(firstDayOfMonth).minusDays(1)
        }
    }

    return tempDate.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli()
}

作为一个例子,我编写了一个测试,该测试运行今天的日期,只将月份从1月更改为12月。这是当前算法的输出。 每月的第一天是用户选择的日期,该日期也在应用程序中默认设置。

Todays date 09.01.2020
Running for month January
Running for 1 st of the month
01.01.2020
31.01.2020
===========================================
Todays date 09.02.2020
Running for month February
Running for 1 st of the month
01.02.2020
29.02.2020
===========================================
Todays date 09.03.2020
Running for month March
Running for 1 st of the month
01.03.2020
31.03.2020
===========================================
Todays date 09.04.2020
Running for month April
Running for 1 st of the month
01.04.2020
30.04.2020
===========================================
Todays date 09.05.2020
Running for month May
Running for 1 st of the month
01.05.2020
31.05.2020
===========================================
Todays date 09.06.2020
Running for month June
Running for 1 st of the month
01.06.2020
30.06.2020
===========================================
Todays date 09.07.2020
Running for month July
Running for 1 st of the month
01.07.2020
31.07.2020
===========================================
Todays date 09.08.2020
Running for month August
Running for 1 st of the month
01.08.2020
31.08.2020
===========================================
Todays date 09.09.2020
Running for month September
Running for 1 st of the month
01.09.2020
30.09.2020
===========================================
Todays date 09.10.2020
Running for month October
Running for 1 st of the month
01.10.2020
31.10.2020
===========================================
Todays date 09.11.2020
Running for month November
Running for 1 st of the month
01.11.2020
30.11.2020
===========================================
Todays date 09.12.2020
Running for month December
Running for 1 st of the month
01.12.2020
31.12.2021
===========================================

Process finished with exit code 0

也选择了31个

Todays date 09.01.2020
Running for month January
Running for 31 th of the month
31.12.2019
30.01.2020
===========================================
Todays date 09.02.2020
Running for month February
Running for 31 th of the month
31.01.2020
30.03.2020
===========================================
Todays date 09.03.2020
Running for month March
Running for 31 th of the month
09.02.2020
30.03.2020
===========================================
Todays date 09.04.2020
Running for month April
Running for 31 th of the month
31.03.2020
30.05.2020
===========================================
Todays date 09.05.2020
Running for month May
Running for 31 th of the month
09.04.2020
30.05.2020
===========================================
Todays date 09.06.2020
Running for month June
Running for 31 th of the month
31.05.2020
30.07.2020
===========================================
Todays date 09.07.2020
Running for month July
Running for 31 th of the month
09.06.2020
30.07.2020
===========================================
Todays date 09.08.2020
Running for month August
Running for 31 th of the month
31.07.2020
30.08.2020
===========================================
Todays date 09.09.2020
Running for month September
Running for 31 th of the month
31.08.2020
30.10.2020
===========================================
Todays date 09.10.2020
Running for month October
Running for 31 th of the month
09.09.2020
30.10.2020
===========================================
Todays date 09.11.2020
Running for month November
Running for 31 th of the month
31.10.2020
30.12.2021
===========================================
Todays date 09.12.2020
Running for month December
Running for 31 th of the month
09.11.2020
30.12.2021
===========================================

Process finished with exit code 0

解决方法

您的计算中有一些错误,一旦您理解了以下给出的解决方案,就可以很容易地发现它们。我在代码中添加了足够的注释,可以帮助您快速理解。

import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.YearMonth;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;

public class Main {
    public static void main(String[] args) {
        // Test for 31
        int startDay = 31;

        System.out.println(Instant.ofEpochMilli(getFirstDayOfMonth(startDay,YearMonth.of(2020,Month.SEPTEMBER))));
        System.out.println(Instant.ofEpochMilli(getLastDayOfMonth(startDay,Month.SEPTEMBER))));
        
        System.out.println(Instant.ofEpochMilli(getFirstDayOfMonth(startDay,Month.FEBRUARY))));
        System.out.println(Instant.ofEpochMilli(getLastDayOfMonth(startDay,Month.FEBRUARY))));
        
        // Test for 30
        startDay = 30;
        System.out.println();
        System.out.println(Instant.ofEpochMilli(getFirstDayOfMonth(startDay,Month.FEBRUARY))));
                
        // Test for 28
        startDay = 28;
        System.out.println();
        System.out.println(Instant.ofEpochMilli(getFirstDayOfMonth(startDay,Month.FEBRUARY))));
    }

    static long getFirstDayOfMonth(int startDay,YearMonth ym) {
        // Get last day of the month
        int lastDayOfTheMonth = ym.getMonth().length(ym.isLeapYear());

        // Start of the day and on the first day of the month
        LocalDateTime ldt = LocalDate.of(ym.getYear(),ym.getMonth(),1)
                            .atStartOfDay();

        if (startDay > lastDayOfTheMonth) {
            ldt = ldt.minusMonths(1) // Go back to the last month
                    .with(TemporalAdjusters.lastDayOfMonth()); // Adjust to the last day of the obtained month
        }
        return ldt.toInstant(ZoneOffset.UTC).toEpochMilli();
    }

    static long getLastDayOfMonth(int startDay,YearMonth ym) {
        return Instant.ofEpochMilli(getFirstDayOfMonth(startDay,ym))// Get the point to start with
                .plus(ym.getMonth().length(ym.isLeapYear()),ChronoUnit.DAYS)// Add the no. of days of the given month
                .atOffset(ZoneOffset.UTC)// Get OffsetDateTime in order to get LocalDate
                .toLocalDate()// Convert to LocalDate
                .atTime(LocalTime.of(23,59,59))// At 23:59:59
                .toInstant(ZoneOffset.UTC)// Convert to Instant
                .toEpochMilli();
    }
}

输出:

2020-08-31T00:00:00Z
2020-09-30T23:59:59Z
2020-01-31T00:00:00Z
2020-02-29T23:59:59Z

2020-09-01T00:00:00Z
2020-10-01T23:59:59Z
2020-01-31T00:00:00Z
2020-02-29T23:59:59Z

2020-09-01T00:00:00Z
2020-10-01T23:59:59Z
2020-02-01T00:00:00Z
2020-03-01T23:59:59Z
,

该LocalDateTime类似乎具有您需要内置的所有功能。检查minusDays和plusDays方法。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-