Current location - Education and Training Encyclopedia - Education and training - String training
String training
Hello, according to your requirements, the code is as follows.

Import java.text.dateformat;

Import java.text.parseexception;

Import java.text.simpledateformat;

Import java.util.date;

Import java.util.scanner;

Common class test {

Public static void main(String[] args) {

String[] day = new String[] {"Day", "One", "Two", "Three", "Four", "Five" and "Six"};

date format df = new simple date format(" yyyyMMdd "); //The date entered must be in this format.

Scanner s = new scanner (system. in);

while (true) {

System.out.println ("Enter the date in the format of 20 120 10 1");

Try {

date d = df . parse(s . nextline()); //Convert a string to a date

system . out . println(" week "+day[d . getday()]);

} catch (ParseException e) {

System.out.println ("illegal input");

}

}

}

}

Running results:

Enter the date in the format of 20 120 10 1.

20 1 1 1 123

Wednesday

Enter the date in the format of 20 120 10 1.

20 1 1

Illegal input

Enter the date in the format of 20 120 10 1.