java enum 笔记 日期时间格式化
java 的enum也是个类。
public enum Action {
TURN_LEFT,
TURN_RIGHT,
SHOOT;
public String GetDescription(){
switch(this.ordinal()){
case 0: return "left";
case 1: return "right";
case 2: return "shoot";
default:return null;
}
}
}
TURN_LEFT,
TURN_RIGHT,
SHOOT;
public String GetDescription(){
switch(this.ordinal()){
case 0: return "left";
case 1: return "right";
case 2: return "shoot";
default:return null;
}
}
}
e.g.2:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public enum ActionAbstract {
TURN_LEFT
{
public String GetDesc(){return "left";}
},
TURN_RIGHT{
public String GetDesc(){return "right";}
},
SHOOT{
public String GetDesc(){return "shoot";}
}; //以;结束
public abstract String GetDesc();
}
TURN_LEFT
{
public String GetDesc(){return "left";}
},
TURN_RIGHT{
public String GetDesc(){return "right";}
},
SHOOT{
public String GetDesc(){return "shoot";}
}; //以;结束
public abstract String GetDesc();
}
ps:
for(Action act : Action.values()){
System.out.printf( "%s %s %n", act.ordinal() , act );
}
//日期时间格式化表达式:
System.out.println(
String.format( "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS",new Date() )
);
System.out.printf("%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS",new Date() );