Take the 2-minute tour ×

im a android and java beginner, with a pretty solid understanding of the java stynax. I am making a simple app that takes a date by the user of when they were born, and subtracts that by the current date and tells (the user) how old they are measured in days. What are some ways i could go about doing this?

p.s. i already have a system for determing the users birth date. it uses a date imput field, then saves the date to a var called usrDate and formats the date to a string.

thanks in advance!

share|improve this question
what have tried ?? did you try to search, imyself answered this question twice today –  PermGenError Jan 8 '13 at 16:37
search before asking, and if you don't find, ask about searching, not about comparing dates. –  njzk2 Jan 8 '13 at 16:40
possible duplicate of How do you subtract Dates in Java? –  Peter O. Jan 8 '13 at 21:41
add comment

closed as not a real question by PermGenError, jeremyharris, Simon, Peter O., birryree Jan 8 '13 at 23:38


4 Answers

up vote 0 down vote accepted

Here is the example:

try {
DateFormat df = new SimpleDateFormat("hh:mm:ss_yyyy.MM.dd");
Date date1 = new java.util.Date();
Date date2 = df.parse("00:00:00_2013.01.01");
long diff = date2.getTime() - date1.getTime();
Log.e("TEST" , date1.getTime() + " - " + date2.getTime() + " - " + diff);
} catch (ParseException e) {
    Log.e("TEST", "Exception", e);

date1: 1350921506492
date2: 1356994800000
diff: 6073293508

diff / 1000 / 60 / 60 / 24 = 70,292748935

Replace the dates with the today's date and the date of birth.

share|improve this answer
add comment

You can do

String birthday = ...
long timeInMillis = System.currentTimeMillis() -
                    new SimpleDateFormat("yyyy/MM/dd").parse(birthday).getTime();
long days = timeInMillis / (24L * 60 * 60 * 1000);
share|improve this answer
add comment
  Calendar Day = Calendar.getInstance();
  Day.set(Calendar.YEAR, 1985);

  Calendar today = Calendar.getInstance();

  long diff = today.getTimeInMillis() - Day.getTimeInMillis();

Convert it to hours or minutes how ever the implementation goes.

share|improve this answer
add comment

Try this with java:

import java.util.Locale;

import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

public class CalculateAge {

    public static void main(String args[]){
        String dob = "Feb 24, 1982 12:08:12 PM";
        String currentDate = "Jan 08, 2012 05:03:12 PM";

        DateTimeFormatter dtf = DateTimeFormat.forPattern("MMM d, yyyy h:mm:ss a").withLocale(Locale.ENGLISH);

        DateTime dateTimeDOB = dtf.parseDateTime(dob);
        DateTime dateTimeCurrentDate = dtf.parseDateTime(currentDate);
        Period totalAge = new Period(dateTimeDOB, dateTimeCurrentDate);

        PeriodFormatter formatter = new PeriodFormatterBuilder()
            .appendYears().appendSuffix(" years ")
            .appendMonths().appendSuffix(" months ")
            .appendWeeks().appendSuffix(" weeks ")
            .appendDays().appendSuffix(" days ")
            .appendHours().appendSuffix(" hours ")
            .appendMinutes().appendSuffix(" minutes ")
            .appendSeconds().appendSuffix(" seconds ")

        String age = formatter.print(totalAge);
        System.out.println("You are [" + age + "] old :) ");

output: You are [29 years 10 months 2 weeks 1 days 4 hours 55 minutes ] old :)

share|improve this answer
add comment
