我正在尝试将值作为开始日期和结束日期传递给函数。到目前为止,我对这些值进行了硬编码,其中开始日期为 20210401,结束日期为 20210419。01 是日期,04 是月份,2021 是年份。
我想在运行时传递这些,其中开始日期将是当月的开始日期,结束日期应该是当月当前日期前 2 天。例如,如果当前月份是 10 月,今天的日期是 2021 年 10 月 15 日。那么开始日期应该是 20211001,结束日期应该是 20211013。请建议 java 中的任何代码。这真的很有帮助。
我正在尝试将值作为开始日期和结束日期传递给函数。到目前为止,我对这些值进行了硬编码,其中开始日期为 20210401,结束日期为 20210419。01 是日期,04 是月份,2021 是年份。
我想在运行时传递这些,其中开始日期将是当月的开始日期,结束日期应该是当月当前日期前 2 天。例如,如果当前月份是 10 月,今天的日期是 2021 年 10 月 15 日。那么开始日期应该是 20211001,结束日期应该是 20211013。请建议 java 中的任何代码。这真的很有帮助。
这是一个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);
}
我想在运行时传递这些,其中开始日期将是当月的开始日期,结束日期应该是当月当前日期前 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 不同的时区中使用,请替换LocalDate
为ZonedDateTime
并today
通过ZonedDateTime.now(ZoneId zone)
传递适用的时区进行初始化,例如ZoneId.of("Asia/Kolkata")
.
从Trail: Date Time了解有关现代日期时间 API 的更多信息。
旧的日期时间 API(java.util
日期时间类型及其格式化 API SimpleDateFormat
)已过时且容易出错。建议完全停止使用它们并切换到java.time
现代日期时间 API *。
* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaring和How to use ThreeTenABP in Android Project。
这是一个可能有帮助的代码片段。在这里,我将本地系统时间作为 ,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");
然后找到一种方法从该endDate
变量中减去 2 天以获得您的startDate
变量。