这是一项根据https://en.wikipedia.org/wiki/Biorhythm计算两个人之间兼容性的练习,效果很好。但是,在执行时,程序会显示日期,例如“Fri Apr 08 00:00:00 EET 2014”,但我希望它显示此信息,而不显示小时,分钟,秒和时区。怎么办?
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) + " %.");
}
}
答案 0 :(得分:1)
您声明了SimpleDateFormat但从未使用它:
试试这个:
System.out.println("DOB of " + nameOne + ": " + format.format(dobOne) + ".");
答案 1 :(得分:0)
您应该进行以下修改:
System.out.println("DOB of " + nameOne + ": " + format.format(dobOne) + ".");
System.out.println("DOB of " + nameTwo + ": " + format.format(dobTwo) + ".");
SimpleDateFormat具有用于此目的的格式(日期日期)方法。
答案 2 :(得分:0)
您可以在println语句中打印dobOneIn
和dobTwoIn
,其中包含您需要的格式:
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 + ".");