import java.util.*;
class DateTime {
private Calendar calendar;
public DateTime() {
calendar = new GregorianCalendar();
}
public String getDate() {
StringBuffer buf = new StringBuffer();
buf.append(this.addZero(calendar.get(Calendar.YEAR), 4)).append("-");
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(
"-");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2))
.append(" ");
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 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 getDateCompete() {
StringBuffer buf = new StringBuffer();
buf.append(this.addZero(calendar.get(Calendar.YEAR), 4)).append("年");
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(
"月");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2))
.append("日");
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 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 getTimeStamp() {
StringBuffer buf = new StringBuffer();
buf.append(this.addZero(calendar.get(Calendar.YEAR), 4));
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2));
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 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 time, int len) {
StringBuffer b = new StringBuffer();
b.append(time);
while (b.length() < len) {
b.insert(0, 0);
}
return b.toString();
}
};
public class APIDemo {
public static void main(String args[]) {
DateTime dt = new DateTime();
System.out.println(dt.getDate());
System.out.println(dt.getDateCompete());
System.out.println(dt.getTimeStamp());
}
};
class DateTime {
private Calendar calendar;
public DateTime() {
calendar = new GregorianCalendar();
}
public String getDate() {
StringBuffer buf = new StringBuffer();
buf.append(this.addZero(calendar.get(Calendar.YEAR), 4)).append("-");
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(
"-");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2))
.append(" ");
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 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 getDateCompete() {
StringBuffer buf = new StringBuffer();
buf.append(this.addZero(calendar.get(Calendar.YEAR), 4)).append("年");
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2)).append(
"月");
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2))
.append("日");
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 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 getTimeStamp() {
StringBuffer buf = new StringBuffer();
buf.append(this.addZero(calendar.get(Calendar.YEAR), 4));
buf.append(this.addZero((calendar.get(Calendar.MONTH) + 1), 2));
buf.append(this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2));
buf.append(this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 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 time, int len) {
StringBuffer b = new StringBuffer();
b.append(time);
while (b.length() < len) {
b.insert(0, 0);
}
return b.toString();
}
};
public class APIDemo {
public static void main(String args[]) {
DateTime dt = new DateTime();
System.out.println(dt.getDate());
System.out.println(dt.getDateCompete());
System.out.println(dt.getTimeStamp());
}
};