java 中的calendar类的运行错误!

lzmings 发布于 2013/09/04 17:37
阅读 263
收藏 0
package randomming;
import java.util.*;
//import java.util.Calendar;
class DateTime{
private Calendar calendar=null;
public DateTime(){
this.calendar=new GregorianCalendar();  //为何这里出现错误
}
public String getDateComplete(){
StringBuffer buf=new StringBuffer();
buf.append(calendar.get(Calendar.Year)).append("年");
buf.append( this.addZero(  Calendar.get(calendar.MONTH)+1,2)).append("月");
buf.append( this.addZero(  Calendar.get(calendar.DAY_OF_MONTH)+1,2)).append("日");
buf.append( this.addZero(  Calendar.get(calendar.HOUR_OF_DAY)+1,2)).append("时");
buf.append( this.addZero(  Calendar.get(calendar.MINUTE),2)).append("分");
buf.append( this.addZero(  Calendar.get(calendar.SECOND),2)).append("秒");
buf.append( this.addZero(  Calendar.get(calendar.MILLISECOND),3)).append("毫秒");
return buf.toString();
}
public String getDate()  {
StringBuffer buf=new StringBuffer();
buf.append(calendar.get(Calendar.Year)).append("-");
buf.append( this.addZero(  Calendar.get(calendar.MONTH)+1,2)).append("-");
buf.append( this.addZero(  Calendar.get(calendar.DAY_OF_MONTH)+1,2)).append("-");
buf.append( this.addZero(  Calendar.get(calendar.HOUR_OF_DAY)+1,2)).append(":");
buf.append( this.addZero(  Calendar.get(calendar.MINUTE),2)).append(":");
buf.append( this.addZero(  Calendar.get(calendar.SECOND),2)).append(":");
buf.append( this.addZero(  Calendar.get(calendar.MILLISECOND),3));
return buf.toString();
}

public String getTimeStamp()  {
StringBuffer buf=new StringBuffer();
buf.append(calendar.get(Calendar.Year));
buf.append( this.addZero(  Calendar.get(calendar.MONTH)+1,2));
buf.append( this.addZero(  Calendar.get(calendar.DAY_OF_MONTH)+1,2));
buf.append( this.addZero(  Calendar.get(calendar.HOUR_OF_DAY)+1,2));
buf.append( this.addZero(  Calendar.get(calendar.MINUTE),2));
buf.append( this.addZero(  Calendar.get(calendar.SECOND),2));
buf.append( this.addZero(  Calendar.get(calendar.MILLISECOND),3));
return buf.toString();
}
private String addZero(int i, int j) {
// TODO Auto-generated method stub
StringBuffer s=new StringBuffer();
s.append(i);
while(s.length()<j)
s.insert(0,"0" );
return s.toString();
}

}


public class Calendar {




public static  void main(String arg[]){
DateTime dt=new DateTime();
System.out.println("系统时间:"+dt.getDate());
System.out.println("中文时间:"+dt.getDateComplete());
System.out.println("系统时间:"+dt.getTimeStamp());

}



}


还有为什么运行不了了? 求大神帮忙!


加载中
0
开源中国首席攻城师
开源中国首席攻城师

初始化不是可以Calendar calendar = Calendar.getInstance();这样咩。。。

l
lzmings
还是不行哦!谢谢你的帮忙!
返回顶部
顶部