Question:
1. Output Monday and Sunday of the week
2. The file name format is student ID. Doc. The student ID and name are extracted from the file name.
3. Calculate the length of a string. Two English letters are required to calculate one.
See if you can write it quickly? The following is the reference answer.
1. Output Monday and Sunday of the week
Calendar now = calendar. getinstance (); // Today
Calendar start = calendar. getinstance (); // indicates Monday
Calendar end = calendar. getinstance (); // indicates Sunday
Int dayofweek = now. Get (calendar. day_of_week); // calculate the day of the week and the day of the week.
// Start. Add (calendar. Date, 2-dayofweek); // calculate Monday
// End. Add (calendar. Date, 8-dayofweek); // calculate Sunday
// Previous errors
Start. Add (calendar. Date, 2-dayofweek> 0? -6:2-dayofweek );
End. Add (calendar. Date, 8-dayofweek = 7? 0: 8-dayofweek );
Dateformat df = new simpledateformat ("yyyy-mmm-dd"); // format the date
System. Out. println (DF. Format (start. gettime (); // output Monday
System. Out. println (DF. Format (end. gettime (); // output Sunday
2. The file name format is student ID. Doc. The student ID and name are extracted from the file name.
/*
* Get the student ID from the file name
*/
Public String getsid (string filename ){
Int Index =-1;
For (Index = 0; index <15; index ++ ){
If (filename. charat (INDEX) <'0' | filename. charat (INDEX)> '9 '){
Break;
}
}
Return filename. substring (0, index );
}
/*
* Extract the Student name from the file name
*/
Public String getname (string filename ){
String SID = getsid (filename );
Int Index = filename. lastindexof ('.');
Return filename. substring (SID. Length (), index). Trim ();
}
3. Calculate the length of a string. Two English letters are required to calculate one.
Int bytes = (taskbook_name.getbytes (). Length + 1)/2;
Li xucheng Csdn blog : Http://blog.csdn.net/javaeeteacher Invite you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5