我想设置simpledate值,以便我可以比较它们以获得年龄

时间:2015-04-19 19:22:37

标签: java input simpledateformat

如何将dateBirthdateToday设置为等于我以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();




   }
}

1 个答案:

答案 0 :(得分:0)

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