Reputation:
I am trying to pass values to a function as start date and end date. As of now i hard code these values where the start date would be 20210401 and end date would be 20210419. 01 is the date, 04 is the month and 2021 is the year.
I want to pass these during run time where start date would be start date of that current month, end date should be 2 days before the current date of the current month. For example if the current month is october and todays date is 15th October, 2021. Then the start date should be 20211001 and end date should be 20211013. Please suggest any code in java. It would be really helpful.
Upvotes: 0
Views: 1986
Reputation: 79425
I want to pass these during run time where start date would be start date of that current month, end date should be 2 days before the current date of the current month.
I suggest you do it using the modern date-time API as demonstrated below:
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);
}
}
Output:
20210401
20210419
Note: If your application is supposed to be used in a different timezone than that of your application's JVM, replace LocalDate
with ZonedDateTime
and intitialize today
with ZonedDateTime.now(ZoneId zone)
passing the applicable timezone e.g. ZoneId.of("Asia/Kolkata")
.
Learn more about the modern date-time API from Trail: Date Time.
The legacy date-time API (java.util
date-time types and their formatting API, SimpleDateFormat
) are outdated and error-prone. It is recommended to stop using them completely and switch to java.time
, the modern date-time API* .
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.
Upvotes: 2
Reputation: 52013
Here is a solution using LocalDate
to calculate the expected dates. Note that if the current date is on the 1st or 2nd of the month the code will use current date as end date rather than doing any calculation. Feel free to change this as you see fit.
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);
}
Upvotes: 2
Reputation: 1
Here's a code snippet which may help. Here I am taking the local system time as the endDate
and going back TWO days prior to get your 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));
I am sure whether you want to use your current system date or whether you want to manually pass a different date into your function.
If you want to manually pass in a date (for example, 10 Jan 2021) into your function during runtime you can use the statement:
Date endDate = simpleDateFormat.parse("20210110");
and then find a way to subtract the 2 days from that endDate
variable to get your startDate
variable.
Upvotes: 0