我想设置 simpledate 值,以便我可以比较它们以获得年龄
I want to set simpledate values so I can compare them to get an age
如何将 dateBirth
和 dateToday
设置为等于我以 mm/dd/yyyy 格式输入的值?
public class AgeClientFL {
public static void main(String [] args){
int month, day, year;
day = UtilsFL.readInt("Enter birth day: ",false); // String mode
month = UtilsFL.readInt("Enter birth month: ",false);
year = UtilsFL.readInt("Enter birth year: ",false);
SimpleDate dateBirth = new SimpleDate();
day = UtilsFL.readInt("Enter todays day: ",true); // JOptionPane mode
month = UtilsFL.readInt("Enter todays month: ",true);
year = UtilsFL.readInt("Enter todays year: ",true);
SimpleDate dateToday = new SimpleDate();
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
...
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse(dateString);
System.out.println(sdf.format(date));
然而,这个 java.util.Date
并不是很有用,一切都已弃用,您只能使用 getTime()。 java.sql.Date
java.util.GregorianCalendar
更有用。我会看一下,你可以很容易地得到年、月、日等。https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html
如何将 dateBirth
和 dateToday
设置为等于我以 mm/dd/yyyy 格式输入的值?
public class AgeClientFL {
public static void main(String [] args){
int month, day, year;
day = UtilsFL.readInt("Enter birth day: ",false); // String mode
month = UtilsFL.readInt("Enter birth month: ",false);
year = UtilsFL.readInt("Enter birth year: ",false);
SimpleDate dateBirth = new SimpleDate();
day = UtilsFL.readInt("Enter todays day: ",true); // JOptionPane mode
month = UtilsFL.readInt("Enter todays month: ",true);
year = UtilsFL.readInt("Enter todays year: ",true);
SimpleDate dateToday = new SimpleDate();
}
}
import java.text.SimpleDateFormat;
import java.util.Date;
...
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
Date date = sdf.parse(dateString);
System.out.println(sdf.format(date));
然而,这个 java.util.Date
并不是很有用,一切都已弃用,您只能使用 getTime()。 java.sql.Date
java.util.GregorianCalendar
更有用。我会看一下,你可以很容易地得到年、月、日等。https://docs.oracle.com/javase/7/docs/api/java/util/GregorianCalendar.html