#Java DayCounter problem

102 messages · Page 1 of 1 (latest)

cerulean thicket
#

This code is supposed to take two dates
and then find how many days are in between them
but I keep getting exit status 1 and I do not understand why. Can someone help me please?

snow magnet
#

You don’t provide a return type on your daysDiff method

fringe jacinth
#

you might want to use LocalDate instead of Date

cerulean thicket
#

oh bruh lol my bad

#

It completely missed my eye

fringe jacinth
#

and not use Calendar

cerulean thicket
snow magnet
#

next time make sure to read your stack trace

cerulean thicket
#

my bad, I dont understand 😢

fringe jacinth
#

the java.time API (containing LocalDate and other classes) is newer, more modern and less subject to errors than Date/Calendar

cerulean thicket
#

okay, so just replace Date with LocalDate?

#

This is the first I've heard of LocalDate, thats why Im making sure lol

snow magnet
cerulean thicket
#

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?

fringe jacinth
#

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

cerulean thicket
#

so Period.between(LocalDate, other LocalDate)

#

wouldnt that essentialy get rid of my method though

fringe jacinth
#

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();
}
cerulean thicket
#

Oh waiiit, I think I am starting to understand now

cerulean thicket
#

I am thinking something like this

#

LocalDate today = new localDate();

#

and then

#

today.of(year, month, day)

fringe jacinth
#

LocalDate today=LocalDate.now();

cerulean thicket
#

^ with those 3 variables being the 3 input.nextInts()

#

oh

fringe jacinth
#

LocalDate someLocalDate=LocalDate.of(year, month, day);

cerulean thicket
#

I seem to have gotten a bunch of errors lol

fringe jacinth
#

you need to import it

cerulean thicket
#

Doesnt util.* import everything time based?

fringe jacinth
#

import java.time.*;

frozen oak
cerulean thicket
#

i see, I should use that more instead

frozen oak
#

I would.

fringe jacinth
#

oh and use period.get(ChronoUnit.DAYS) if you want to get the total days

cerulean thicket
fringe jacinth
#

and period.getDays() removes all full months

fringe jacinth
#

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)

cerulean thicket
#

Ohhh i see

fringe jacinth
#

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

cerulean thicket
#

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

fringe jacinth
#

most other stuff in java.util is fine

#

but not datetime related stuff

cerulean thicket
#

oh okay

#

also, what is to become of my Result object?

fringe jacinth
#

ig Result is one of your classes?

#

what should it contain?

cerulean thicket
#

The result was supposed to be an object, but it helped find the difference between the two days

#

28

cerulean thicket
#

Apparently Result itself cannot be found

#

the symbol I mea

#

mean*

#

Because of the middle two errors

fringe jacinth
#

Did you create a Result class?

#

for the errors with LocalDate and period, use import java.time.*;

#

but Result doesn't exist

cerulean thicket
fringe jacinth
#

ah ok

cerulean thicket
#

okay, i managed to filter it down to one last error

#

and its that ChronoUnit cannot be found

fringe jacinth
#

ChronoUnit is in java.time.temporal

#

so import java.time.temporal.ChronoUnit;

#

Does your IDE not automatically add imports?

cerulean thicket
#

no it doesn't 😭

#

what IDE does?

fringe jacinth
#

e.g. Eclipse and IntelliJ

cerulean thicket
#

also, I got a lossy conversion error from long to int

fringe jacinth
#

at least if you use autocomplete

cerulean thicket
#

looks like ill have to use eclipse soon

fringe jacinth
#

oh right, those things use long

#

not int

#

you have two options: either use long or cast it to int

cerulean thicket
#

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 : )

snow magnet
#

You should definitely switch to eclipse or IntelliJ

cerulean thicket
#

hiii, I keep getting a error saynig i cant find a class with the main method

frozen oak
# cerulean thicket

You can't name your class starting with a number. See the red squiggly underneath it?