Java - Count day of month occurrences in an interval of time - java

I am using Java 8, i need to count how many days we have in an interval having index = x. Example: from 1-1-2019 till 31-1-2019 we have 1 occurrence of Day 1.
I do not want the range of days, just the count of day X
If its the last day of month, i want to count them all example: 30 +30 +31+28

try this method:
public static int getDateCount(LocalDate startDate, LocalDate endDate, final int index) {
long numOfDaysBetween = ChronoUnit.DAYS.between(startDate, endDate);
return IntStream.iterate(0, i -> i + 1)
.limit(numOfDaysBetween)
.mapToObj(i -> startDate.plusDays(i))
.filter(i -> i.getDayOfMonth() == index)
.collect(Collectors.toList()).size();
}
usage:
public static void main(String[] args){
LocalDate startDate = LocalDate.of(2019,1,1);
LocalDate endDate = LocalDate.of(2019,1,31);
int index=1;
System.out.println(getDateCount(startDate,endDate,index));
}
output:
1
Here i first calculated days between two dates then extracted all days occuring between these dates then filtered them to desired day e.g 1 in that case.
Note : that might not be the best and effective solution

I don't know if the java.time API provides methods to directly do this, but you can always loop through all the dates between the start and end check them one by one.
LocalDate start = LocalDate.of(2019, 1, 1);
LocalDate end = LocalDate.of(2022, 12, 31);
long daysBetween = ChronoUnit.DAYS.between(start, end);
System.out.println(daysBetween);
// you might want to use a parallel stream. It might speed things up a little
long count = LongStream.range(0, daysBetween + 1)
.mapToObj(start::plusDays)
.filter(x -> x.getDayOfMonth() == index)
.count();
System.out.println(count);
There is a special case for the last day of the month, you need:
.filter(x -> x.equals(x.with(TemporalAdjusters.lastDayOfMonth())))

here is the solution:
LocalDate start = LocalDate.of(2019, 1, 1);
LocalDate end = LocalDate.of(2019, 3, 31);
long daysBetween = ChronoUnit.DAYS.between(start, end);
int index = 1;
int count = 0;
start = start.minusDays(1);
for(int i = 0; i <= daysBetween; i++){
start = start.plusDays(1);
if (start.getDayOfMonth() == index) {
count++;
}
}
System.out.println(count);

finding days in-between using:
Days d = Days.daysBetween(startDate, endDate);
int days = d.getDays();
finding specific day was there or not, suppose 11th of month.
(startDay + days) contains the day you require something like that.

public long getNumberOfDayOfMonthBetweenDates(LocalDate startDate, LocalDate endDate, int dayOfMonth) {
long result = -1;
if (startDate != null && endDate != null && dayOfMonth > 0 && dayOfMonth < 32) {
result = 0;
LocalDate startDay = getDayInCurrentMonth(startDate, dayOfMonth);
// add one day as end date is exclusive
// add + 1 to cover higher possibilities (month and a half or half month or so)
long totalMonths = ChronoUnit.MONTHS.between(startDate, endDate.plusDays(1)) + 2;
for (int i = 0; i < totalMonths; i++) {
if ((!startDay.isBefore(startDate) && startDay.isBefore(endDate)) || startDay.equals(startDate) || startDay.equals(endDate)) {
result++;
}
startDay = getDayInCurrentMonth(startDay.plusMonths(1), dayOfMonth);
}
}
return result;
}
private LocalDate getDayInCurrentMonth(LocalDate startDate, int dayOfMonth) {
LocalDate dayOfThisMonth;
try {
dayOfThisMonth = startDate.withDayOfMonth(dayOfMonth);
} catch (DateTimeException e) {
// handle cases where current month does not contain the given day (example 30 in Feb)
dayOfThisMonth = startDate.withDayOfMonth(startDate.lengthOfMonth());
}
return dayOfThisMonth;
}

Related

Java Parking fee calculations

It seems like, I couldn't find the answer for my problem, so here I am, first on Stackoverflow :)
The If statement tree that will be mentioned:
buttonSzamol.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//Változók
int StartHour = 18;
int StartMin = 50;
int StopHour = 20;
int StopMin = 49;
int DayTimeIntervalStart = 6;
int DayTimeIntervalStop = 17;
int NightTimeIntervalLateStart = 18;
int NightTimeIntervalLateStop = 23;
int NightTimeIntervalEarlyStart = 0;
int NightTimeIntervalEarlyStop = 5;
int DayHoursTotal = 0;
int NightHoursTotal = 0;
int DayTimePricePerHour = Integer.parseInt(NappaliOraDij.getText());
int NightTimePricePerHour = Integer.parseInt(EjszakaiOraDij.getText());
int StartDay = Integer.parseInt((DatumStart.getText()).replace(".", ""));
int StopDay = Integer.parseInt((DatumStart.getText()).replace(".", ""));
//1 started hour
if( (StartDay == StopDay) && ( ( (StartHour == StopHour) && (StartMin < StopMin) ) || ( ((StartHour + 1) == StopHour) && (StartMin >= StopMin) ) ) ) {
if((DayTimeIntervalStart <= StartHour) && (StopHour <= DayTimeIntervalStop)) {
DayHoursTotal++;
}
if((NightTimeIntervalLateStart <= StartHour) && (StopHour <= NightTimeIntervalLateStop)) {
NightHoursTotal++;
}
} else/*More hours*/if( (StartDay == StopDay) && ( ( (StartHour < StopHour) && (StartMin <= StopMin) ) || ( (StartHour < StopHour) && (StartMin > StopMin) ) ) ) {
if( (StartHour < StopHour) && (StartMin < StopMin) ) {
if((DayTimeIntervalStart <= StartHour) && (StopHour <= DayTimeIntervalStop)) {
DayHoursTotal = DayHoursTotal + (StopHour - StartHour);
DayHoursTotal++;
}
if((NightTimeIntervalLateStart <= StartHour) && (StopHour <= NightTimeIntervalLateStop)) {
NightHoursTotal = NightHoursTotal + (StopHour - StartHour);
NightHoursTotal++;
}
}else if(( (StartHour < StopHour) && (StartMin >= StopMin) )) {
if((DayTimeIntervalStart <= StartHour) && (StopHour <= DayTimeIntervalStop)) {
DayHoursTotal = DayHoursTotal + (StopHour - StartHour);
if(StartMin != StopMin) {
DayHoursTotal--;
}
}
if((NightTimeIntervalLateStart <= StartHour) && (StopHour <= NightTimeIntervalLateStop)) {
NightHoursTotal = NightHoursTotal + (StopHour - StartHour);
if(StartMin != StopMin) {
NightHoursTotal--;
}
}
}
}
NappaliOrak.setText(Integer.toString(DayHoursTotal));
EjszakaiOrak.setText(Integer.toString(NightHoursTotal));
OrakOsszesen.setText(Integer.toString(DayHoursTotal + NightHoursTotal));
NappaliOsszeg.setText(Integer.toString(DayHoursTotal * DayTimePricePerHour));
EjszakaiOsszeg.setText(Integer.toString(NightHoursTotal * NightTimePricePerHour));
VegOsszeg.setText(Integer.toString((DayHoursTotal * DayTimePricePerHour) + (NightHoursTotal * NightTimePricePerHour)));
}
});
So, the problem in a nutshell is.
I've tried to create a parking fee calculator for my colleague at work.
The main idea is, that it needs to calculate how many Daytime and how many Nighttime hours the client started, and it needs to calculate the price of those hours. I've changed the StartHour/Min-StopHour/Min fields to straight integers to be more understanable. I don't know if there is a module for this, but I started doing this with a lot of If statements, where I just got tangled up. In the included pastebin, there is starting time 18:50 and stop time 20:49. If we input this data, the output should be 2 started day hours. Now if the minute is the same, it does not count as a started hour. But if we change the input to 20:51, then it started an another hour, so the DayHoursTotal should be equal to 3.
Thank you in advance, for any help. If you have more questions about my code or idea, just ask.
It seems that you are trying to calculate the started hours not just between 2 times, but also between different dates.
For this it is best to use the java.time package and more specifically the LocalDateTime class.
LocalDateTime.of(startYear, startMonth, startDay, startHour, startMinute)
LocalDateTimes in conjuction with the between() method from the Java 8 ChronoUnit class gets exactly what you need.
ChronoUnit.MINUTES.between(Temporal t1, Temporal t2)
PS: You don't need that many 'interval' variables.
Just the start hour of the day (dayTimeIntervalStart) and night (nightTimeIntervalLateStart) rate is enough.
The hours rates before and after can be derived from those two intervals.
Spoiler!! look away if you want to investigate further yourself! ;)
Here is a runnable code sample that shows the parking logic for >1 day:
(I have omitted the user input parsing/logic, because that depends on your implementation)
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class ParkingFee {
private static long hoursDifference(LocalDateTime ldt1, LocalDateTime ldt2) {
long minutesDiff = ChronoUnit.MINUTES.between(ldt1, ldt2);
long hoursDiff = Math.round(Math.ceil(minutesDiff/60.0));
return hoursDiff;
}
public static long hoursDifference(
int startDay, int startMonth, int startYear, int startHour, int startMinute,
int endDay, int endMonth, int endYear, int endHour, int endMinute) {
return hoursDifference(
LocalDateTime.of(startYear, startMonth, startDay, startHour, startMinute),
LocalDateTime.of(endYear, endMonth, endDay, endHour, endMinute));
}
public static int determineDayCycle(int dayTimeIntervalStart, int nightTimeIntervalLateStart) {
return nightTimeIntervalLateStart - dayTimeIntervalStart;
}
public static void main(String[] args) {
// Hourly rates
int dayTimePricePerHour = 5;
int nightTimePricePerHour = 10;
// Rate intervals
int dayTimeIntervalStart = 6;
int nightTimeIntervalLateStart = 18;
// Counted hours per rate
int dayHoursTotal = 0;
int nightHoursTotal = 0;
// Start date and time
int startYear = 2019;
int startMonth = 1;
int startDay = 1;
int startHour = 20;
int startMinute = 50;
// End date and time
int endYear = 2019;
int endMonth = 1;
int endDay = 3;
int endHour = 2;
int endMinute = 49;
// Calculate the hours difference
long hourDiff = hoursDifference(
startDay, startMonth, startYear, startHour, startMinute,
endDay, endMonth, endYear, endHour, endMinute);
System.out.println("Hour difference found: "+ hourDiff);
// Handle parking for full days
if (hourDiff > 24) {
int dayCycle = determineDayCycle(dayTimeIntervalStart, nightTimeIntervalLateStart);
long fullDays = hourDiff / 24;
nightHoursTotal += (24-dayCycle)*fullDays;
dayHoursTotal += dayCycle*fullDays;
hourDiff = hourDiff % 24;
}
// Handle the parking for less than full day
while (hourDiff > 0) {
if (startHour < dayTimeIntervalStart) { // Before the day interval -> night
nightHoursTotal++;
} else if(startHour < nightTimeIntervalLateStart) { // Before the night interval -> day
dayHoursTotal++;
} else { // After the day interval -> night
nightHoursTotal++;
}
startHour++;
if (startHour > 23) // At midnight reset the hour to 0
startHour = 0;
hourDiff--;
}
System.out.println("Day hours: "+ dayHoursTotal);
System.out.println("Night hours: "+ nightHoursTotal);
System.out.println("Total hours: "+ (dayHoursTotal + nightHoursTotal));
System.out.println("Day rate charged at "+ dayTimePricePerHour +": "+ (dayHoursTotal * dayTimePricePerHour));
System.out.println("Night rate charged at "+ nightTimePricePerHour +": "+ (nightHoursTotal * nightTimePricePerHour));
System.out.println("Total rate charged: "+ ((dayHoursTotal * dayTimePricePerHour) + (nightHoursTotal * nightTimePricePerHour)));
}
}
This outputs:
Hour difference found: 30
Day hours: 12
Night hours: 18
Total hours: 30
Day rate charged at 5: 60
Night rate charged at 10: 180
Total rate charged: 240
First, you need to parse the Integers differently. Your method is dangerous, e.g. loses information. Plus you need to make the code failsafe in case somebody tries to put in values that won't work. Refer to this question: How do I convert a String to an int in Java?
Apart from that, working with just minutes and hours is always difficult. I suggest using the absolute times in milliseconds which makes it far easier to do calculations. Refer to this question: Difference in hours of two Calendar objects
Time Zone
Your code and the other Answers fail to account for time zone anomalies. If you are tracking actual moments, when people actually parked, as opposed to theoretical 24-hour long days, then you must account for anomalies such as Daylight Saving Time (DST). Politicians around the world have shown a penchant for redefining the time zone(s) in their jurisdiction. So days can be any length, such as 25-hours long, 23-hours, 23.5 hours, 24.25, or others.
A time zone is a history of the past, present, and future changes to the offset-from-UTC used by the people of a particular region.
The LocalDateTime class is exactly the wrong class to use for this purpose. That class intentionally has no concept of time zone or offset-from-UTC. You can use it as a building block piece in your code, but it must be assigned a ZoneId to determine an actual moment via the ZonedDateTime class.
ZoneId
Specify your time zone.
If no time zone is specified, the JVM implicitly applies its current default time zone. That default may change at any moment during runtime(!), so your results may vary. Better to specify your desired/expected time zone explicitly as an argument. If critical, confirm the zone with your user.
Specify a proper time zone name in the format of Continent/Region, such as America/Montreal, Africa/Casablanca, or Pacific/Auckland. Never use the 2-4 letter abbreviation such as EST or IST as they are not true time zones, not standardized, and not even unique(!).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
If you want to use the JVM’s current default time zone, ask for it and pass as an argument. If omitted, the code becomes ambiguous to read in that we do not know for certain if you intended to use the default or if you, like so many programmers, were unaware of the issue.
ZoneId z = ZoneId.systemDefault() ; // Get JVM’s current default time zone.
Assemble date, time, & zone to determine a moment
Assemble your date and time-of-day.
LocalDate startDate = LocalDate.of( 2019 , 1 , 23 ) ; // 23rd of January in 2019.
LocalTime startTime = LocalTime.of( 18 , 50 ) ; // 6:50 PM.
ZonedDateTime startMoment = ZonedDateTime.of( startDate , startTime , z ) ;
LocalDate stopDate = LocalDate.of( 2019 , 1 , 23 ) ; // 23rd of January in 2019.
LocalTime stopTime = LocalTime.of( 20 , 50 ) ; // Two hours later, exactly — maybe! Depends on any anomalies at that time in that zone.
ZonedDateTime stopMoment = ZonedDateTime.of( stopDate , stopTime , z ) ;
➥ Note that in this example, we may have a span of time of exactly 2 hours, but maybe not. It might be 3 hours or some other length of time, depending on anomalies scheduled for that date at that time in that zone.
Elapsed time
To calculate elapsed time it terms of days (24-hour chunks of time, unrelated to the calendar), hours, minutes, and seconds, use Duration. (For year-months-days, use Period.)
Duration d = Duration.between( startMoment , stopMoment ) ;
Interrogate for entire span-of-time in terms of whole hours.
long hours = d.toHours() ; // Entire duration as count of whole hours.
Half-Open
In the included pastebin, there is starting time 18:50 and stop time 20:49. If we input this data, the output should be 2 started day hours. Now if the minute is the same, it does not count as a started hour. But if we change the input to 20:51, then it started an another hour, so the DayHoursTotal should be equal to 3.
This approach is know as Half-Open, when the beginning is inclusive, while the ending is exclusive. This is commonly used in date-time handling. The Duration and Period classes apply this approach.
But be careful about matching the minute number alone. Your date-time objects might be holding seconds and/or a fractional second, which would throw off your algorithm. As a habit, truncate your date-time objects explicitly if there is any possibility of smaller granularity than you want. For example, ZonedDateTime.truncatedTo.
Rate changes
Obviously, rate changes complicate matters. The other Answers seem to have covered this, so I'll not repeat. But I can add a big tip: See the ThreeTen-Extra for its classes Interval and LocalDateRange that may be of help to you. They include handy comparison methods for overlaps, contains, abuts, and so on.
divide et impera
Cutting down the big logic in small blocks make it easier to achieve
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
class Scratch {
static int StartHour = 18;
static int StartMin = 50;
static int StopHour = 20;
static int StopMin = 48;
static int DayTimeIntervalStart = 6;
static int DayTimeIntervalStop = 17;
static int NightTimeIntervalLateStart = 18;
static int NightTimeIntervalLateStop = 23;
static int NightTimeIntervalEarlyStart = 0;
static int NightTimeIntervalEarlyStop = 5;
static int DayTimePricePerHour = 10;
static int NightTimePricePerHour = 5;
static LocalTime dayStart = LocalTime.of(DayTimeIntervalStart, 0);
static LocalTime dayStop = LocalTime.of(DayTimeIntervalStop, 0);
static LocalTime nightEarlyStart = LocalTime.of(NightTimeIntervalEarlyStart, 0);
static LocalTime nightEarlyStop = LocalTime.of(NightTimeIntervalEarlyStop, 0);
static LocalTime nightLateStart = LocalTime.of(NightTimeIntervalLateStart, 0);
static LocalTime nightLateStop = LocalTime.of(NightTimeIntervalLateStop, 0);
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.of(2019, 1, 1, StartHour, StartMin);
LocalDateTime stop = LocalDateTime.of(2019, 1, 1, StopHour, StopMin);
for(int i = 0; i < 3; i++){
stop = stop.plusMinutes(1L);
System.out.println(process(start, stop));
System.out.println("******");
}
stop = stop.plusDays(1L);
System.out.println(process(start, stop));
System.out.println("******");
}
public static int process(LocalDateTime start, LocalDateTime stop){
System.out.println(String.format("checking between %s and %s", start, stop));
if(stop.toLocalDate().isAfter(start.toLocalDate())){
// start and stop not on the same date
// split the computation, first currentDay then the rest
LocalDateTime endOfDay = LocalDateTime.of(start.toLocalDate(), LocalTime.MAX);
int resultForCurrentDay = process(start, endOfDay);
// not for the rest
LocalDateTime startOfNextDay = LocalDateTime.of(start.toLocalDate().plusDays(1L), LocalTime.MIN);
int resultForRest = process(startOfNextDay, stop);
return resultForCurrentDay + resultForRest;
}else{
// start and stop on the same date
return processIntraDay(start, stop);
}
}
private static int processIntraDay(LocalDateTime start, LocalDateTime stop) {
int result = 0;
LocalTime startTime = start.toLocalTime();
LocalTime stopTime = stop.toLocalTime();
// step 1: check early morning
result += checkBoundaries(startTime, stopTime, nightEarlyStart, nightEarlyStop, NightTimePricePerHour);
// step 2: check day time
result += checkBoundaries(startTime, stopTime, dayStart, dayStop, DayTimePricePerHour);
// step 3: check late night
result += checkBoundaries(startTime, stopTime, nightLateStart, nightLateStop, NightTimePricePerHour);
return result;
}
private static int checkBoundaries(LocalTime startTime, LocalTime stopTime, LocalTime lowerBoundary, LocalTime upperBoundary, int priceRatePerHour) {
// check if the period [start;stop] is crossing the period [lowerBoundary;upperBoundary]
if(stopTime.isAfter(lowerBoundary) && startTime.isBefore(upperBoundary)){
// truncate start time to not be before lowerBoundary
LocalTime actualStart = (startTime.isBefore(lowerBoundary))?lowerBoundary:startTime;
// symetrically, truncate stop to not be after upperBounday
LocalTime actualStop = (stopTime.isAfter(upperBoundary))?upperBoundary:stopTime;
// now that we have the proper start and stop of the period, let's compute the price of it
return compute(actualStart, actualStop, priceRatePerHour);
}else{
return 0;
}
}
private static int compute(LocalTime startTime, LocalTime stopTime, int pricePerHour) {
Duration duration = Duration.between(startTime, stopTime);
int hours = (int) duration.toHours();
long minutes = duration.toMinutes();
if(minutes % 60 > 0L){
// hour started, increasing the number
hours++;
}
int result = hours * pricePerHour;
System.out.println(String.format("%d hours at %d price/h => %d", hours, pricePerHour, result));
return result;
}
}
Went directly for the calculation of the final price. Updating to store total number of day hours and night hours should be much of a challenge
Result of my exemple:
checking between 2019-01-01T18:50 and 2019-01-01T20:49
2 hours at 5 price/h => 10
10
******
checking between 2019-01-01T18:50 and 2019-01-01T20:50
2 hours at 5 price/h => 10
10
******
checking between 2019-01-01T18:50 and 2019-01-01T20:51
3 hours at 5 price/h => 15
15
******
checking between 2019-01-01T18:50 and 2019-01-02T20:51
checking between 2019-01-01T18:50 and 2019-01-01T23:59:59.999999999
5 hours at 5 price/h => 25
checking between 2019-01-02T00:00 and 2019-01-02T20:51
5 hours at 5 price/h => 25
11 hours at 10 price/h => 110
3 hours at 5 price/h => 15
175
******
Might need more tests to ensure it's good in all conditions but should be a usefull starting point for you

Java: How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

I am new to programming and java and I am trying to solve the following problem:
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Here is my code:
int count, sum = 0;
for (int i = 1901; i < 2001; i++) {
LocalDate test = LocalDate.of(i, 1, 1);
sum += test.lengthOfYear();
}
for (int i = 1; i < sum; i++) {
LocalDate date1 = LocalDate.of(1901, 1, 1);
date1 = date1.plusDays(i);
if (date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY) {
count++;
}
}
System.out.println(count);
If I print the results, it seems to be working fine.
My result is 443, but the correct answer is 171. What am I doing wrong?
Thank you!
I suspect 443 is the total number of Sundays in January in the twentieth century. This happens because you walk over all possible days of the twentieth century and then check if the current month is January and if the current day is Sunday.
This is not what you want.
I would use a different approach:
Walk over the 1st day of each month of each year.
And then check if it's a Sunday.
The code will probably be much faster.
// Each year
for (int y = 1901; y < 2001; y++) {
// Each month of the year
for (int m = 1; m <= 12; m++) {
if (LocalDate.of(y, m, 1).getDayOfWeek() == DayOfWeek.SUNDAY) {
count++;
}
}
}
Your code would have been correct if you changed date1.getMonth() == JANUARY to date1.getDayOfMonth() == 1. However, the method is very inefficient, because it checks each day of the twentieth century, while it only needs to check the first day of each month. The abovementioned code is approximately 40 times faster on my machine.
Here is an equivalent of the abovementioned code, with functional style:
long count = Stream.iterate(YearMonth.of(1901, 1), ym -> ym.plusMonths(1))
.takeWhile(ym -> ym.isBefore(YearMonth.of(2001, 1)))
.map(ym -> ym.atDay(1).getDayOfWeek())
.filter(DayOfWeek.SUNDAY::equals)
.count();
Using Todd's Java-TimeStream, with functional style:
YearMonthStream
.from(YearMonth.of(1901, 1))
.until(YearMonth.of(2000, 12))
.stream()
.map(ym -> ym.atDay(1).getDayOfWeek())
.filter(DayOfWeek.SUNDAY::equals)
.count();
I see some mistakes:
public static void main(String[] args) {
int count, sum = 0;
for (int i = 1901; i < 2001; i++) { // There is a mistake here, I dont know what you want to compute in this loop!
LocalDate test = LocalDate.of(i,1,1);
sum += test.lengthOfYear();
}
for (int i = 1; i < sum; i++) {
LocalDate date1 = LocalDate.of(1901,1,1); // There is a mistake here, date1 must be outside of this loop
date1 = date1.plusDays(i); // There is a mistake here, plusDays why??
if(date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY) { // There is a mistake here, why are you cheking this: date1.getMonth() == JANUARY ?
count++;
}
}
System.out.println(count);
}
A simple solution:
public static void main(String[] args) {
int count = 0;
LocalDate date1 = LocalDate.of(1901, Month.JANUARY, 1);
LocalDate endDate = LocalDate.of(2001, Month.JANUARY, 1);
while (date1.isBefore(endDate)) {
date1 = date1.plusMonths(1);
if (date1.getDayOfWeek() == DayOfWeek.SUNDAY) {
count++;
}
}
System.out.println(count);
}
Apart from the error that has already been flagged, you could reconsider your design and use the YearMonth class which seems better suited to your use case than LocalDate:
public static void main(String[] args) {
YearMonth start = YearMonth.of(1901, 1);
YearMonth end = YearMonth.of(2000, 12);
int count = 0;
for (YearMonth ym = start; !ym.isAfter(end); ym = ym.plusMonths(1)) {
//is first day of month a sunday?
if (ym.atDay(1).getDayOfWeek() == SUNDAY) count ++;
}
System.out.println(count); //171
}
You start by finding the number of days in the period you're checking, and then start a for loop to run through that period. So far so good. But your condition for incrementing the count is wrong:
if(date1.getMonth() == JANUARY && date1.getDayOfWeek() == SUNDAY)
This means that as you loop over each day, you increase count if that date is a Sunday in January. You don't check whether that Sunday was the first of January, and you don't count anything from February to December.
You should be checking that it's the first day of the month and Sunday, not the first month of the year and Sunday.
The following code should output the correct value.
public static void main(String[] args) {
int count = 0, sum = 0;
for (int i = 1901; i < 2001; i++) {
LocalDate test = LocalDate.of(i, 1, 1);
sum += test.lengthOfYear();
}
for (int i = 1; i < sum; i++) {
LocalDate date1 = LocalDate.of(1901, 1, 1);
date1 = date1.plusDays(i);
if (date1.getDayOfMonth() == 1 && date1.getDayOfWeek() == java.time.DayOfWeek.SUNDAY) {
count++;
}
}
System.out.println(count);
}
Note that:
You don't need to check that month is January, because your requirement is about any 1st of the month.
You must also ensure that the current day is the first day of the month.
443 is number of Sundays in January, if you want the Sundays on the first of the month, you have to change your code to:
if(date1.getDayOfMonth() == 1 && date1.getDayOfWeek() == SUNDAY) {
count++;
}
}
Sugestion: instead of walk over all days of each year, you can just add 1 month to the date util 12 month:
for (int i = 1901; i < 2001; i++) {
for(int mon =0; mon<12; mon++){
LocalDate date1= LocalDate.of(i,mon,1);
if(date1.getDayOfWeek() == SUNDAY) {
count++;
}
}
}
You are considering only January month from 1901 to 2000, which is incorrect, you should consider whether first day of every month from 1901 to 2000 was sunday or not.

Iterate over subrange of weekdays

I'm trying to iterate over a range of weekdays, where each weekday maps to an integer (Sunday = 1, Monday = 2, ..., Saturday = 7).
The range is defined by a start and end day. This is fine for a start and end day like Monday(2) - Thursday(5), as I can just do:
for(int i = startDay; i <= endDay; i++) { ... }
I'm having difficulty when the range is split across the end and start of a week, like Friday(6) - Monday(2). This obviously doesn't work with the above for loop - e.g.
for(int i = 6; i <= 2; i++) { ... } // wouldn't even execute once.
The only solution I can see is implementing some sort of circular buffer, which I feel is overkill for such a simple problem. I'm just stumped, and most likely missing something obvious here. I think a solution may have something to do with modular arithmetic, but I'm not quite sure.
You can do:
int numberOfDays = endDay >= startDay ? endDay - startDay : 8 - (startDay - endDay);
for (int i = startDay; i <= startDay + numberOfDays; i++) {
int day = (i - 1) % 7 + 1;
}
This makes use of the % modulo operator to ensure all values remain within 1 - 7.
For example, once i becomes 8 the calculation will wrap day back to 1: (8 - 1) % 7 + 1 == 1.
It's probably clearest just to use a break, then you don't have to worry about all the different cases:
for (int day = startDay; ; day = (day - 1) % 7 + 1) {
// ... do your stuff
if (day == endDay) {
break;
}
}
or, some might prefer this:
int day = startDay;
while (true) {
// ... do something
if (day == endDay) {
break;
}
day = (day - 1) % 7 + 1;
}
or:
int day = startDay;
while (true) {
// ... do something
if (day == endDay) {
break;
}
if (++day > 7) {
day = 1;
}
}
Trying to get all the different cases right with a for loop can be a headache. You have to make sure these are handled:
startDay == 1, endDay == 7
endDay == startDay
endDay == startDay - 1 (which should go through every day of the week in some order)
endDay > startDay
endDay < startDay
In Java 8:
// input section
DayOfWeek start = DayOfWeek.FRIDAY;
DayOfWeek end = DayOfWeek.MONDAY;
// execution section
DayOfWeek dow = start;
DayOfWeek stop = end.plus(1);
do {
// ... your code
dow = dow.plus(1);
} while (dow != stop);
The do-while-construction ensures that for the condition start == end the loop will be executed at least once.
UPDATE due to use of your localized indices (Sunday = 1 instead of 7)
You can convert the indices to DayOfWeek-objects this way:
private static DayOfWeek convert(int localIndex) {
int iso = localIndex - 1;
if (iso == 0) {
iso = 7;
}
return DayOfWeek.of(iso);
}
You can use a % operator for some sort of circularity
for (int i=6; i!=2; i=(i+1)%7) { ... }
That will index days from 0, so Sunday = 0 ... Saturday = 6. If you really want to have index starting from 1 it's only interpretation problem. You can for example do:
for (int i=6; i!=2; i=(i+1)%7) {
int day=i+1;
// use day here
}

How to calculate the number of Tuesday in one month?

How to calculate number of Tuesday in one month?
Using calender.set we can set particular month, after that how to calculate number of Mondays, Tuesdays etc. in that month?
Code is :
public static void main(String[] args )
{
Calendar calendar = Calendar.getInstance();
int month = calendar.MAY;
int year = 2012;
int date = 1 ;
calendar.set(year, month, date);
int MaxDay = calendar.getActualMaximum(calendar.DAY_OF_MONTH);
int mon=0;
for(int i = 1 ; i < MaxDay ; i++)
{
calendar.set(Calendar.DAY_OF_MONTH, i);
if (calendar.get(Calendar.DAY_OF_WEEK) == calendar.MONDAY )
mon++;
}
System.out.println("days : " + MaxDay);
System.out.println("MOndays :" + mon);
}
Without writing whole code here is the general idea:
Calendar c = Calendar.getInstance();
c.set(Calendar.MONTH, Calendar.MAY); // may is just an example
c.set(Calendar.YEAR, 2012);
int th = 0;
int maxDayInMonth = c.getMaximum(Calendar.MONTH);
for (int d = 1; d <= maxDayInMonth; d++) {
c.set(Calendar.DAY_OF_MONTH, d);
int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
if (Calendar.THURSDAY == dayOfWeek) {
th++;
}
}
This code should count number of Thursday. I believe you can modify it to count number of all days of week.
Obviously this code is not efficient. It iterates over all the days in the month. You can optimize it by getting just get day of the week of the first day, the number of days in month and (I believe) you can write code that calculates number of each day of week without iterating over the whole month.
With Java 8+, you could write it in a more concise way:
public static int countDayOccurenceInMonth(DayOfWeek dow, YearMonth month) {
LocalDate start = month.atDay(1).with(TemporalAdjusters.nextOrSame(dow));
return (int) ChronoUnit.WEEKS.between(start, month.atEndOfMonth()) + 1;
}
Which you can call with:
int count = countDayOccurenceInMonth(DayOfWeek.TUESDAY, YearMonth.of(2012, 1));
System.out.println(count); //prints 5
AlexR mentioned the more efficient version. Thought I would give it a whirl:
private int getNumThursdays() {
// create instance of Gregorian calendar
Calendar gc = new GregorianCalendar(TimeZone.getTimeZone("EST"), Locale.US);
int currentWeekday = gc.get(Calendar.DAY_OF_WEEK);
// get number of days left in month
int daysLeft = gc.getActualMaximum(Calendar.DAY_OF_MONTH) -
gc.get(Calendar.DAY_OF_MONTH);
// move to closest thursday (either today or ahead)
while(currentWeekday != Calendar.THURSDAY) {
if (currentWeekday < 7) currentWeekday++;
else currentWeekday = 1;
daysLeft--;
}
// calculate the number of Thursdays left
return daysLeft / 7 + 1;
}
note: When getting the current year, month, day etc. it is dependent on your environment. For example if someone manually set the time on their phone to something wrong, then this calculation could be wrong. To ensure correctness, it is best to pull data about the current month, year, day, time from a trusted source.
Java calendar actually has a built in property for that Calendar.DAY_OF_WEEK_IN_MONTH
Calendar calendar = Calendar.getInstance(); //get instance
calendar.set(Calendar.DAY_OF_WEEK, 3); //make it be a Tuesday (crucial)
//optionally set the month you want here
calendar.set(Calendar.MONTH, 4) //May
calendar.getActualMaximum(Calendar.DAY_OF_WEEK_IN_MONTH); //for this month (what you want)
calendar.getMaximum(Calendar.DAY_OF_WEEK_IN_MONTH); //for any month (not so useful)

Calculating days between certain dates with certain days being excluded using java Calendar

I'm trying to come up with a way to calculate the number of days between two different dates, however there will be certain days of the week that are only to be accounted for. For example, let's say we want to calculate the number of work days between 8/1 and 8/31, but employee only works Monday, Tuesday and Wednesday. The result would be that this employee only works 15 days during that period.
Has anyone put together something similar using the java Calendar class?
Try Joda Time, is the best solution to manage Date and Time.
The code of dogbane corrected:
final Calendar current = Calendar.getInstance();
current.set(2011, 7, 1);
final Calendar end = Calendar.getInstance();
end.set(2011, 7, 31);
int count = 0;
while (!current.after(end)) {
int dayOfWeek = current.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.MONDAY ||
dayOfWeek == Calendar.TUESDAY ||
dayOfWeek == Calendar.WEDNESDAY) {
count++;
}
current.add(Calendar.DAY_OF_MONTH, 1);
}
System.out.println(count);
If use Joda Time:
DateTime current = new DateTime(2011, 8, 1, 0, 0, 0, 0);
DateTime end = new DateTime(2011, 8, 31, 0, 0, 0, 0);
int count = 0;
while (!current.isAfter(end)) {
int dayOfWeek = current.getDayOfWeek();
if (dayOfWeek == DateTimeConstants.MONDAY || dayOfWeek == DateTimeConstants.TUESDAY
|| dayOfWeek == DateTimeConstants.WEDNESDAY) {
count++;
}
current = current.plusDays(1);
}
System.out.println(count);
You can do this by incrementing the date by one day until you reach the end date. At each iteration, check if the day is Mon, Tue or Wed and increment a counter.
For example:
final Calendar current = Calendar.getInstance();
current.set(2011, 7, 1);
final Calendar end = Calendar.getInstance();
end.set(2011, 7, 31);
int count = 0;
while (current.compareTo(end) != 0) {
current.add(Calendar.DAY_OF_MONTH, 1);
int dayOfWeek = current.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek == Calendar.MONDAY ||
dayOfWeek == Calendar.TUESDAY ||
dayOfWeek == Calendar.WEDNESDAY) {
count++;
}
}
System.out.println(count);
You could roll your own solution with java.util.Calendar; however, I suggest looking at some existing library, such as ObjectLab Kit date utilities.

Categories