Question

This is a exercise that computes compatibility between two persons based on https://en.wikipedia.org/wiki/Biorhythm, it works well. However when executing, the program shows the date as for example "Fri Apr 08 00:00:00 EET 2014", but I would like it to show this info without hours, minutes, seconds and time zone. What to do?

import java.util.Scanner;

import java.util.Date; 

import java.text.ParseException;

import java.text.SimpleDateFormat;


public class bior {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        String nameOne;
        String nameTwo;

        String dobOneIn;
        String dobTwoIn;

        Date dobOne = new Date();
        Date dobTwo = new Date();

        boolean validEntry;

        SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");

        System.out.println("Enter name of first person!");
        nameOne = input.nextLine();

        while (nameOne.equals("")) {
            System.out.println("Enter name of first person!");
            nameOne = input.nextLine();
        }

        System.out.println("Enter name of second person!");
        nameTwo = input.nextLine();

        while (nameTwo.equals("")) {
            System.out.println("Enter name of second person!");
            nameTwo = input.nextLine();
        }   

         do {
                try {
                    System.out.println("Enter date of birth of " + nameOne + "! (MM/DD/YYYY)");
                    dobOneIn = input.nextLine();
                    dobOne = format.parse(dobOneIn);
                    validEntry = true;
                    }
                catch (ParseException e) {
                    validEntry = false;
                     }
            } while (!validEntry);

         do {
                try {
                    System.out.println("Enter date of birth of " + nameTwo + "! (MM/DD/YYYY)");
                    dobTwoIn = input.nextLine();
                    dobTwo = format.parse(dobTwoIn);
                    validEntry = true;
                    }
                catch (ParseException e) {
                    validEntry = false;
                     }
            } while (!validEntry);

        int diff = Math.abs((int)((dobOne.getTime() - dobTwo.getTime()) / (24 * 60 * 60 * 1000)));

        System.out.println();
        System.out.println("Name of second person: " + nameTwo + ".");
        System.out.println("DOB of " + nameOne + ": " + dobOne + ".");
        System.out.println("DOB of " + nameTwo + ": " + dobTwo + ".");
        System.out.println("Difference between DOBs (days): " + diff + ".");

        float physicalBio = diff % 23;
        float emotionalBio = diff % 28;
        float intellectualBio = diff % 33;

        physicalBio = physicalBio / 23;
        emotionalBio = emotionalBio  / 28;
        intellectualBio = intellectualBio / 33;

        if (physicalBio > 0.5) {
            physicalBio = 1 - physicalBio;
        }

        if (emotionalBio > 0.5) {
            emotionalBio = 1 - emotionalBio;
        }

        if (intellectualBio > 0.5) {
            intellectualBio = 1 - intellectualBio;
        }

        physicalBio = 100 - (physicalBio * 100);
        emotionalBio = 100 - (emotionalBio  * 100);
        intellectualBio = 100 - (intellectualBio * 100);

        System.out.println("Physical compatibility: " + java.lang.Math.round(physicalBio) + " %.");
        System.out.println("Emotional compatibility: " + java.lang.Math.round(emotionalBio) + " %.");
        System.out.println("Intellectual compatibility: " + java.lang.Math.round(intellectualBio) + " %.");

    }

}
Was it helpful?

Solution 2

You should make the following modification :

System.out.println("DOB of " + nameOne + ": " + format.format(dobOne) + ".");
    System.out.println("DOB of " + nameTwo + ": " + format.format(dobTwo) + ".");

SimpleDateFormat has the format(Date date) method for this purpose.

OTHER TIPS

You declared a SimpleDateFormat but never used it:

Try this:

System.out.println("DOB of " + nameOne + ": " + format.format(dobOne) + ".");

You can print dobOneIn and dobTwoIn in your println statement which already has format you need:

System.out.println();
System.out.println("Name of second person: " + nameTwo + ".");
System.out.println("DOB of " + nameOne + ": " + dobOneIn + ".");
System.out.println("DOB of " + nameTwo + ": " + dobTwoIn + ".");
System.out.println("Difference between DOBs (days): " + diff + ".");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top