gpt4 book ai didi

java - SimpleDateFormat 显示太多信息

转载 作者:行者123 更新时间:2023-12-01 13:05:39 25 4
gpt4 key购买 nike

这是一个基于 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) + " %.");

}

}

最佳答案

您声明了 SimpleDateFormat 但从未使用过它:

试试这个:

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

关于java - SimpleDateFormat 显示太多信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23289701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com