Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.Locale;

public class Main {
    private static int howOld(final Date birthday) {
        int r = -1;
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
        String birthdayY = sdf.format(birthday);
        String nowY = sdf.format(now);
        r = Integer.parseInt(nowY) - Integer.parseInt(birthdayY);

        if (r != -1) {
            if (isOver(birthday)) {
                r += 1;
            }
        }

        return r;
    }

    private static boolean isOver(final Date birthday) {
        boolean b = false;
        SimpleDateFormat sdf = new SimpleDateFormat("M-d", Locale.getDefault());
        String md = sdf.format(birthday);
        Date now = new Date();
        sdf = new SimpleDateFormat("yyyy", Locale.getDefault());
        String y = sdf.format(now);
        String ymd = y + md;
        Date birDate = new Date();
        try {
            birDate = sdf.parse(ymd);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        b = now.getTime() - birDate.getTime() > 0;

        return b;
    }
}