Click here to Skip to main content
15,885,910 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The problem is that, ok, let's get two dates like 4/5/2023 and 6/7/2023. I use the Scanner in java to input these dates. However, the code I keep writing keeps giving me errors and is a pain in the ass to read.

Also I'm a beginner at java and this is for a project so I'm not using in built functions or things like BigDecimals

Is there a way to solve this without using Nested Loops or ArrayLists? because they take too long and are hard to debug

What I have tried:

I have tried to use ArrayLists and repeated loops.
like, for (int i = Month1, i < Month2, i++){
}
for(int x = Day1, i < Day2, x+=){
}
but these are too complex and hard to read

Is there an easy way to do this without the Calendar function?
Posted
Updated 24-Apr-23 8:16am
v2
Comments
Josuha Swannings 24-Apr-23 12:51pm    
here's the full code if you care and don't have headaches
import java.util.Scanner;
public class days2Dates {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("M and D");
int m = kb.nextInt();
int d = kb.nextInt();
System.out.println("M2 and D2");
int m2 = kb.nextInt();
int d2 = kb.nextInt();
kb.close();
int days = 0;
for(int i = m; i<=m2; i++) {
if (i % 2 != 0 || i == 8 || i == 10 || i == 12) {
if (i != 9 && i != 11 && i != m2) {
days = days + (31-d);
}
if (i == 9 && i != m2) {
if (d2<31 && d2>0 && m != 9){
days = days + (d2);
}
if (d2 > 31 && m != 9) {
days = days + 30;
}
if (i == 9 && i != m) {
days = days + (30-d);
}
if (i == m2 && m2 == m) {
days = days + (d2-d);
}

}
if (i == m2 && i != m && i!=9 && i != 11) {
days = days + (31-d);
}
if (i == m2 && m2 == m) {
days = days + (d2-d);
}
if (i == 11 && i != m2) {
if (d2<31 && d2>0 && m != 11){
days = days + d2;
}
if (d2 > 31 && m != 11) {
days = days + 30;
}
if ()
}
}
else if (i % 2 == 0) {
if (i != 2 && i != 8 && i != 10 && i != 12 && i != m) {
if (d < 31 && d > 0){
days = days + d;
}
else {
days = days + 30;
}
}
if (i == 8) {
days = days + d;
}
if (i == 10) {
days = days + d;
}
if (i == 12) {
days = days + d;
}
if (i == m && i != 12){
days = days + d;
}
else if (i == 2){
if (d > 0 && d < 29) {
days = days + d;
}
else {
days = days + 28;
}
}
}
}
System.out.println(days);





//find the number of days from 01/01/2023 till m/d/2023






}
}

1 solution

First off - please do not post code as a comment. Use the green Improve Question button/link to paste your code as a code block in your original question. When you paste you will be asked how to format your text - pick Java code block. Then delete your comment if you can.

Now on to your problem. You state you don't want to use built-in functions or classes. Why that would be is beyond me. Any significant coding you do will use those objects as a matter of course. Using them here will teach how to use them which is useful in and of itself.

Regardless of my opinion, to solve your problem: Since you're dealing with dates only, I will assume you want a difference in days. I would probably convert both dates to a number in the same context. What I mean by that is to convert each one to something like the number of days since a common start date. That could be January 1, 2000 or January 1, 1900. Just pick a base date that is earlier than what either can be. Subtract the 2 values and voila you have the difference in days.

But, you say, all I've done is change taking the difference between 2 dates to that of taking the difference twice - between each given date and the base date. Perhaps so, but what really happens is now you have a common function to get a base value from any date. That function may be somewhat complex, especially if leap years need to be taken into account, but the rest of your code that needs date differences will be simple to write, read and understand.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900