Ainsi, le programme est supposé prendre une date au format "aaaa/jj/mm". Après chaque date, il y a du texte disponible pour être entré. Par exemple, "1776/7/4 Premier Jour de l'Indépendance". Je voulais utiliser un tokenizer String et diviser la date sur les barres obliques mais ensuite je suis confus sur la façon de le diviser sur l'espace après cela. Une fois que je les ai séparés, je peux alors comparer les dates avec d'autres dates correctes?Division des dates saisies par l'utilisateur en Java
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first date: ");
String firstDate = sc.nextLine();
//System.out.print("Enter the second date: ");
//String secondDate = sc.nextLine();
//System.out.println();
//StringTokenizer date = new StringTokenizer(firstDate, "/",false);
//StringTokenizer space = new StringTokenizer(firstDate, " ",false);
//if (firstDate.equals(secondDate))
//{
// System.out.println (
// "There are 0 year(s), 0 month(s) and 0 day(s) between dates");
//}
int firstSpace = firstDate.indexOf(' ');
String event = firstDate.substring(firstSpace + 1); // "First Independence Day"
String date = firstDate.substring(0, firstSpace); // "1776/7/4"
// split on '/'
String dateParts[] = date.split("/");
String dateParts1[] = date.split("/");
String dateParts2[]= date.split("/");
if(dateParts1[1].length() != 2)
dateParts1[1] = "0" + dateParts1[1];
// add 0 to front of day
if(dateParts1[2].length() != 2)
dateParts1[2] = "0" + dateParts1[2];
// do the same for the second date
if(dateParts2[1].length() != 2)
dateParts2[1] = "0" + dateParts2[1];
// convert array to string
String date1 = String.join("/", dateParts1);
String date2 = String.join("/", dateParts2);
// set up the format that you have
SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy/MM/dd");
Date d1 = dateFormat.parse(date1);
Date d2 = dateFormat.parse(date2);
// equal dates
if(d1.equals(d2)) {
// There or 0 days, months and years between dates.
}
vous auriez pu écrire 'split' correctement –