在java运行时向函数传递日期值

qyswt5oh  于 2021-07-09  发布在  Java
关注(0)|答案(3)|浏览(250)

我试图将值作为开始日期和结束日期传递给函数。现在我硬编码这些值,其中开始日期是20210401,结束日期是20210419。01是日期,04是月份,2021是年份。
我想在运行时传递这些,其中开始日期是当前月份的开始日期,结束日期应该是当前月份的当前日期前2天。例如,如果当前月份是10月,而今天的日期是2021年10月15日。那么开始日期应该是20211001,结束日期应该是20211013。请推荐任何java代码。这真的很有帮助。

mgdq6dx1

mgdq6dx11#

我想在运行时传递这些,其中开始日期是当前月份的开始日期,结束日期应该是当前月份的当前日期前2天。
我建议您使用现代日期时间api进行此操作,如下所示:

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.Locale;

public class Main {
    public static void main(String[] args) {
        LocalDate today = LocalDate.now();

        LocalDate startDate = today.with(TemporalAdjusters.firstDayOfMonth());

        LocalDate endDate = today.minusDays(2);

        // Use the following optional block if your requirement is to reset the end date
        // to the start date in case it falls before the start date e.g. when the
        // current date is on the 1st or the 2nd day of the month
        //////////////////////// Start of optional block/////////////////////
        if (endDate.isBefore(startDate)) {
            endDate = startDate;
        }
        ///////////////////////// End of optional block//////////////////////

        // Get the strings representing the dates in the desired format
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuuMMdd", Locale.ENGLISH);
        String strStartDate = startDate.format(dtf);
        String strEndDate = endDate.format(dtf);
        System.out.println(strStartDate);
        System.out.println(strEndDate);
    }
}

输出:

20210401
20210419

注意:如果应用程序应该在与应用程序的jvm不同的时区中使用,请替换 LocalDateZonedDateTime 初始化 todayZonedDateTime.now(ZoneId zone) 通过适用的时区,例如。 ZoneId.of("Asia/Kolkata") .
从trail:date-time了解有关现代日期时间api的更多信息。

使用传统的日期时间api怎么样?

遗留日期时间api( java.util 日期时间类型及其格式api, SimpleDateFormat )过时且容易出错。建议完全停止使用并切换到 java.time ,现代日期时间api*。

  • 出于任何原因,如果您必须坚持使用Java6或Java7,您可以使用threeten backport,它将大部分java.time功能向后移植到Java6和Java7。如果您正在为一个android项目工作,并且您的android api级别仍然不符合java-8,请检查通过desugaring提供的java8+api以及如何在android项目中使用threetenabp。
ryoqjall

ryoqjall2#

下面是一段代码片段,可能会有所帮助。这里我以本地系统时间作为 endDate 回去两天前拿到你的 startDate .

// the format you'd like to display your date as.
    String pattern ="yyyyMMdd";
    // this formats the date to look how you'd want it to look

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);

    //Get date for 2 day prior to currentDate.
    long SINGLE_DAY_IN_MS = 1000 * 60 * 60 *24;
    long nDaysToGoBack = 2;     
    //create 'startDate' variable from milliseconds as an input
    Date startDate = new Date(System.currentTimeMillis()- (nDaysToGoBack*SINGLE_DAY_IN_MS));

    //  Set 'endDate' as your current date.
    Date endDate = new Date(System.currentTimeMillis());
    System.out.println();
    System.out.println("Start Date: "+ simpleDateFormat.format(startDate));
    System.out.println("End Date: "+ simpleDateFormat.format(endDate));

我确定您是要使用当前的系统日期,还是要手动将不同的日期传递到函数中。
如果要在运行时将日期(例如,2021年1月10日)手动传递到函数中,可以使用以下语句:

Date endDate = simpleDateFormat.parse("20210110");

然后想办法从中减去2天 endDate 变量来获取 startDate 变量。

ecbunoof

ecbunoof3#

下面是一个使用 LocalDate 计算预期日期。请注意,如果当前日期是本月的1号或2号,则代码将使用当前日期作为结束日期,而不是进行任何计算。如果你认为合适,请随意更改。

LocalDate now = LocalDate.now();
LocalDate first = now.withDayOfMonth(1);
LocalDate limit = now.withDayOfMonth(3);

LocalDate last = null;
if (now.isBefore(limit)) {
    last = now;
} else {
    last = now.minusDays(2);
}

相关问题