Three Java questions to see if you can make them

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.