1

I have a back-end written in Java and a front-end written in Angular. I pretend to show the age of an user doing maths (actual date - birth date) but I can't find a way to do that.

I tried this two options:

1.- Front-end: I do the maths and get the result. The problem is that the types do not look the same. If I print both this is the result:

  • BirthDate: "1996-07-24T00:00:00.000+0000" (I got it from API)
  • ActualDate: "Sat Jul 11 2020 20:27:02 GMT+0200 (Central European Summer Time)" (I got it doing let date: Date = new Date;)

When I use any function with the birthdate I got an error. for example getTime() to get it in miliseconds. Both variables are stored in Date type variables. Errors looks like this:

ERROR TypeError: user.birthDate.getTime is not a function
    at SafeSubscriber._next (user-list.component.ts:47)
    at SafeSubscriber.__tryOrUnsub (Subscriber.js:183)
    at SafeSubscriber.next (Subscriber.js:122)
    at Subscriber._next (Subscriber.js:72)
    at Subscriber.next (Subscriber.js:49)
    at MapSubscriber._next (map.js:35)
    at MapSubscriber.next (Subscriber.js:49)
    at FilterSubscriber._next (filter.js:33)
    at FilterSubscriber.next (Subscriber.js:49)
    at MergeMapSubscriber.notifyNext (mergeMap.js:72)

2.- Backend. I added a new attribute in the class and I called it Age. I do not want to store it in the DB I just want to calculate it and give it to the front-end. with this, looks like Java tries to share this new attribute but it is not in the DB because I do not want to store it. I thought that If I do not add the @Column above the attribute, it would not connect to a column in the database but looks like it does not work.

Any ideas? I accept any of the 2 approaches.

Thank you

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
carlossiry
  • 19
  • 1
  • in the front, you need to convert the string you got to a Date object to be able to use `getTime()`. this means `new Date('your date string here').getTime()`. should work for both BirthDate and ActualDate – Stavm Jul 11 '20 at 19:03
  • God, I thought I tried that. Thank you @Stavm. It worked – carlossiry Jul 11 '20 at 19:14

1 Answers1

2

I do not know angular. I've answered below how you can do it with backend (your Option-2):

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class Main {
    public static void main(String[] args) {
        String strBirthDay = "1996-07-24T00:00:00.000+0000";

        // Define formatter
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");

        // Define time-zone
        ZoneId zone = ZoneId.of("Etc/UTC");

        // Get dates
        ZonedDateTime birthDay = LocalDateTime.parse(strBirthDay, formatter).atZone(zone);
        ZonedDateTime zdtNow = ZonedDateTime.now(zone);

        // Calculate years between dates
        long years = ChronoUnit.YEARS.between(birthDay, zdtNow);

        // Calculate months part
        ZonedDateTime forward = birthDay.plusYears(years);
        long months = ChronoUnit.MONTHS.between(forward, zdtNow);

        // Calculate days part
        forward = forward.plusMonths(months);
        long days = ChronoUnit.DAYS.between(forward, zdtNow);

        System.out.println(years + " years " + months + " months " + days + " days");
    }
}

Output:

23 years 11 months 17 days

I see in your question that you have tried something like, Date = new Date(). Note that java.util.Date class has been deprecated for ages. I recommend you stop using the outdated and error-prone java.util date-time API and switch to the modern date-time API.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110