1

I am creating a Calendar instance, and setting the date to July 1, 1997 like so:

int currentYear = Calendar.getInstance().get(Calendar.YEAR); 
Calendar calendar = Calendar.getInstance();
calendar.clear();
calendar.set(1997, 6, 1);

What I want to do, is that without using an external library, get the following output from that date (proper calculating of leap years / seconds would be good, but not required) prior to current date (e.g. November 1, 2015 02:45:30):

18 years, 4 months, 0 days, 2 hours, 45 minutes, 30 seconds

I am not quite sure if this is possible at all. I've tried some weird, not very logical calculations, which needed lots of improvements, but couldn't make it work:

int years = currentYear - calendar.get(Calendar.YEAR);
int months = calendar.get(Calendar.MONTH);

if(currentMonth > months) {
    years -= 1;
}

UPDATE - Code until now:

Calendar currentDate = Calendar.getInstance();
currentDate.clear();

Calendar birthDate = Calendar.getInstance();
birthDate.clear();
birthDate.set(this.birthYear, this.birthMonth - 1, this.birthDay);

Calendar date = Calendar.getInstance();
date.clear();
date.setTimeInMillis(birthDate.getTimeInMillis() - currentDate.getTimeInMillis());

System.out.println(Integer.toString(date.get(Calendar.YEAR)));
Amar Syla
  • 3,523
  • 3
  • 29
  • 69
  • Convert both time instants into milliseconds since epoch, UTC, and then convert the difference (in ms) into years, months, days, etc. – Mick Mnemonic Nov 01 '15 at 01:57
  • @MickMnemonic Didn't understand properly. Anyways, I tried converting birthDate and currentDate both to millis, then subtracted currentDate from birthDate, and got... 1997. What am I doing wrong? – Amar Syla Nov 01 '15 at 02:08
  • Can you share the code where you've done that? What you've posted now doesn't include e.g. the conversion to milliseconds. – Mick Mnemonic Nov 01 '15 at 02:10
  • which version of java you are using? – JAVAC Nov 01 '15 at 02:27

2 Answers2

0

if you are using java 8 then you have LocalDateTime and PlainTimeStamp classes to use

here you find some answers Java 8: Calculate difference between two LocalDateTime

Community
  • 1
  • 1
JAVAC
  • 1,230
  • 3
  • 17
  • 38
0

This might help

    Calendar startCalendar = Calendar.getInstance();
    startCalendar.clear();
    startCalendar.set(1997, 6, 1);
    Date start = startCalendar.getTime();

    Calendar endCalendar = Calendar.getInstance();
    // endCalendar.clear();
    // endCalendar.set(2015, 10, 1);
    Date end = endCalendar.getTime();

    long diff = end.getTime() - start.getTime();

    long days = TimeUnit.MILLISECONDS.toDays(diff);
    long hours = TimeUnit.MILLISECONDS.toHours(diff) % TimeUnit.DAYS.toHours(1);
    long minutes = TimeUnit.MILLISECONDS.toMinutes(diff) % TimeUnit.HOURS.toMinutes(1);
    long seconds = TimeUnit.MILLISECONDS.toSeconds(diff) % TimeUnit.MINUTES.toSeconds(1);

    System.out.println(days + " " + hours + " " + minutes + " " + seconds);

from the days we can write the logic to find the number of leap years, months using modulo division

Java 8 has a new Date API you can try that too since you're using Java 8

Saravana
  • 12,647
  • 2
  • 39
  • 57