#Java DayCounter problem
102 messages · Page 1 of 1 (latest)
You don’t provide a return type on your daysDiff method
you might want to use LocalDate instead of Date
and not use Calendar
Whats the difference
next time make sure to read your stack trace
and whats wrong with Calendar?
my bad, I dont understand 😢
the java.time API (containing LocalDate and other classes) is newer, more modern and less subject to errors than Date/Calendar
okay, so just replace Date with LocalDate?
This is the first I've heard of LocalDate, thats why Im making sure lol
ohhhh I see, that makes a lot of sense
but what would Calendar be changed to, another form of LocalDate?
new problem, what does a "cannot find symbol" error mean?
as in, what symbol cannot be found?
it's not a 1:1 change
you can get a LocalDate with LocalDate.of(year, month, day)
a LocalDate represents a date
for the difference, you need a Duration
you can obtain it using Duration.between(someLocalDate, otherLocalDate)
my fault, Period, not Duration
so Period.between(LocalDate, other LocalDate)
wouldnt that essentialy get rid of my method though
that returns a Period object
with a toDays method
so you could do it like that
public static int daysDiff(LocalDate one, LocalDate two){
Period period = Period.between(one, two);
return period.toDays();
}
Oh waiiit, I think I am starting to understand now
but how would I incorperate the LocalDate System into this
I am thinking something like this
LocalDate today = new localDate();
and then
today.of(year, month, day)
LocalDate today=LocalDate.now();
LocalDate someLocalDate=LocalDate.of(year, month, day);
you need to import it
Doesnt util.* import everything time based?
import java.time.*;
java.util.Date is older API... java.time.* is a newer and more powerful incarnation.
i see, I should use that more instead
I would.
oh and use period.get(ChronoUnit.DAYS) if you want to get the total days
for the return statement, correct?
and period.getDays() removes all full months
yes except you want to remove full months
If you have a difference of 1 month and 2 days, getDays will give you 2 while get(ChronoUnit.DAYS) will give you 32 (if it is a month with 30 days)
Ohhh i see
the date/time stuff in java.util is the old thing that shouldn't be used any more unless necessary (e.g. Date/Calendar)
java.time contains the newer stuff
damn, Im really using something super outdated 😭
I wonder why teachers dont teach this stuff instead
bc I've been using this for so long
The result was supposed to be an object, but it helped find the difference between the two days
28
What’s the problem
Apparently Result itself cannot be found
the symbol I mea
mean*
Because of the middle two errors
Did you create a Result class?
for the errors with LocalDate and period, use import java.time.*;
but Result doesn't exist
I did, its just line 1 isnt shown right here
ah ok
okay, i managed to filter it down to one last error
and its that ChronoUnit cannot be found
ChronoUnit is in java.time.temporal
so import java.time.temporal.ChronoUnit;
Does your IDE not automatically add imports?
e.g. Eclipse and IntelliJ
also, I got a lossy conversion error from long to int
at least if you use autocomplete
looks like ill have to use eclipse soon
oh right, those things use long
not int
you have two options: either use long or cast it to int
omg
I just turned it into a long
and it worked
Thank you for being so patient with me
Im not too good at coding lol, especially with all these new things that I wasnt taught
I really appreciate it
all of you : )
You should definitely switch to eclipse or IntelliJ
You can't name your class starting with a number. See the red squiggly underneath it?