重温switch,case

package test;

/**
 * 重温Switch,case
 * 
 * @author Zhou-Jingxian
 *
 */
public class TestDate {

	public static void main(String args[]) {

		datechange("2009");
		datechange("200910");
		datechange("20091020");
		datechange("2009102233");
	}
	
	public static void datechange(String enter_time){
		System.out.println("enter_time:"+enter_time);
		int len = enter_time.length();
		switch(len){//switch 的参数只能是byte,char,short,int型的,其他的都不可以 
		case 4:
			enter_time = enter_time.substring(0,4);
			break;
		case 6:
			enter_time = enter_time.substring(0,4)+"-"+enter_time.substring(4,6);
			break;
		case 8:
			enter_time = enter_time.substring(0,4)+"-"+enter_time.substring(4,6)+"-"
                                            +enter_time.substring(6, 8);
			break;
		default:
			enter_time = "";
			break;
		}
		System.out.println("change:"+enter_time);
	}
}
 

你可能感兴趣的:(case,switch)