因此该程序假设采用“yyyy / dd / mm”格式的日期。在每个日期之后,可以输入文本。例如,“1776/7/4第一独立日”。我想使用一个String tokenizer并在斜杠上分割日期,但后来我很困惑如何在那之后将它拆分。一旦我拆分它们,我可以将日期与其他日期进行比较吗?
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.
}
答案 0 :(得分:0)
String x = "yyyy/dd/mm";
String[] split = x.split("/");
for (String s : split) {
System.out.println(s);
}
Output:
yyyy
dd
mm
答案 1 :(得分:0)
我认为获取第一个空格字符的索引是个好主意。
String text = "1776/7/4 First Independence Day";
^
int firstSpace = text.indexOf(' ');
String event = text.substring(firstSpace + 1); // "First Independence Day"
String date = text.substring(0, firstSpace); // "1776/7/4"
// split on '/'
String dateParts[] = date.split("/");
即使事件名称包含/
,这也将确保一切正常。
修改强>
要比较日期,首先需要确保像April
这样的月份的值为04而不是4.假设您有2个数组,dateParts1和dateParts2。
// add 0 to front of month
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;
// 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)) {
// do something
}
if(date2.after(date1))
return true;
else
return false;
编辑2 。
String firstDate = "1776/7/4 First Independence Day";
int firstSpace = firstDate.indexOf(' ');
String event = firstDate.substring(firstSpace + 1); // "First Independence Day"
String date = firstDate.substring(0, firstSpace); // "1776/7/4"
String dateParts1[] = date.split("/");
String dateParts2[] = date.split("/");
// add 0 to front of month
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];
// add 0 to front of day
if (dateParts2[2].length() != 2)
dateParts2[2] = "0" + dateParts2[2];
// convert array to string
String date1 = String.join("/", dateParts1);
String date2 = String.join("/", dateParts2);
// set up the format that you have
java.text.SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
java.util.Date d1 = dateFormat.parse(date1);
java.util.Date d2 = dateFormat.parse(date2);
if (d1.equals(d2))
System.out.println("equal");
答案 2 :(得分:0)
我建议使用正则表达式和DateFormat ...
String input = "1776/07/04 First Independence Day";
Pattern pattern = Pattern.compile("(\\d+/\\d+/\\d+)\\s+(.*)");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
DateFormat format = new SimpleDateFormat("yyyy/M/d");
Date date = format.parse(matcher.group(1));
System.out.println(date + " " + matcher.group(2));
}
答案 3 :(得分:0)
我不太明白你的问题。 你可以把第一个令牌作为日期并将其保存在一个新的字符串中,然后你可以简单地使用string.compareTo(另一个字符串)进行比较;或者您可以使用简单的scanner.nextInt():方法将年,月和日作为整数,然后比较它们!希望我帮忙。
答案 4 :(得分:-1)
只需像这样使用splite
字符串数据;
string [] parts;
parts = data.splite('');