I have a Date object in Java stored as Java's Date type.
I also have a Gregorian Calendar created date. The gregorian calendar date has no parameters and therefore is an instance of today's date (and time?).
With the java date, I want to be able to get the year, month, day, hour, minute, and seconds from the java date type and compare the the gregoriancalendar date.
I saw that at the moment the Java date is stored as a long and the only methods available seem to just write the long as a formatted date string. Is there a way to access Year, month, day, etc?
I saw that the getYear()
, getMonth()
, etc. methods for Date
class have been deprecated. I was wondering what's the best practice to use the Java Date instance I have with the GregorianCalendar
date.
My end goal is to do a date calculation so that I can check that the Java date is within so many hours, minutes etc of today's date and time.
I'm still a newbie to Java and am getting a bit puzzled by this.
Be carefully mount start from 0 not 1. And also Calendar use pm like am so that be very carefully when use Hours ans minutes.
EDIT: The output for
date
=Fri Jun 15 09:20:21 CEST 2018
is:With Java 8 and later, you can convert the Date object to a LocalDate object and then easily get the year, month and day.
Note that
getMonthValue()
returns an int value from 1 to 12.You could do something like this, it will explain how the
Date
class works.It might be easier