-5

I want to add 25 years more to the date of birth field.

maxage is a int field of value 25.

if(table1.getregister_date() >= (employee.getdate_of_birth() + maxage))
//do something

want to know can i add and compare date fields this way??

not-a-bug
  • 383
  • 1
  • 2
  • 10
  • 2
    You should provide more code or info about the context. For instance what are the return types of `table1.getregister_date()` and `employee.getdate_of_birth()` – Hakan Dilek Jul 02 '20 at 08:34
  • Did you try it? – Kayaman Jul 02 '20 at 08:34
  • What is the exact type of your date field (returned by `getregister_date` and `getdate_of_birth`)? Is it `java.util.Date`? – Amongalen Jul 02 '20 at 08:35
  • For any Date/Time framework I worked with in java: No. You need to tell it that it's years you want to add. How to do that depends on the framework. – Fildor Jul 02 '20 at 08:35
  • 3
    Unrelated: [Java naming convention](https://www.oracle.com/java/technologies/javase/codeconventions-namingconventions.html) – jhamon Jul 02 '20 at 08:36

1 Answers1

2

It depends on the type that employee.getdate_of_birth() returns. If its a LocalDate, you can do employee.getdate_of_birth().plusYears(maxage). There are similar methods for days, months and seconds.

Trebolete
  • 115
  • 10
  • thanks.. i was just searching some methods like plusYears() – not-a-bug Jul 02 '20 at 08:54
  • But while comparing with >= i am getting build error bad operand types for binary operator '>=' [ERROR] first type: java.time.LocalDateTime [ERROR] second type: java.time.LocalDate – not-a-bug Jul 02 '20 at 09:01
  • 1
    You're comparing `LocalDate` with `LocalDateTime`. You should convert first `LocalDateTime` to `LocalDate` using `LocalDate.from(yourLocalDateTimeVariable)` Difference between those classes is `LocalDateTime` stores info about hours, minutes and seconds and `LocalDate` have info just about year, month and day. – Trebolete Jul 02 '20 at 09:06
  • I typecasted so that now both is LocalDate....but still getting bad operand for ">=". Now both types are LocalDate only – not-a-bug Jul 02 '20 at 09:18
  • bad operand types for binary operator '>=' [ERROR] first type: java.time.LocalDate [ERROR] second type: java.time.LocalDate – not-a-bug Jul 02 '20 at 09:22
  • used method isAfter and isEqual to achive >= – not-a-bug Jul 02 '20 at 09:36
  • Yeah thats also an option. The problem is there is not a method called `isGreaterOrEqual` so you need to do 2 comparations instead 1. I created some dummy test with two `LocalDate` objects and comparing them with `>=` and everything worked... not sure why you were getting that error. – Trebolete Jul 02 '20 at 09:49