时间格式化01

来源:互联网 发布:如何优化库存管理 编辑:程序博客网 时间:2024/06/14 17:59

import java.util.Calendar;
import java.util.GregorianCalendar;

public class DateTimeDemo {
 private Calendar calendar = new GregorianCalendar(); // 实例化Calendar对象

 public String getDate() {// 2010-04-22
  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), 2));
  return buf.toString();
 }

 public String getDateTime() {// 2010-04-22 16:19:34.123
  StringBuffer buf = new StringBuffer();
  buf.append(this.getDate()).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 getDateComplete() {// 2010年04月22日
  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), 2))
    .append("日");
  return buf.toString();
 }

 public String getDateTimeComplete() {// 2010年04月22日16时19分34秒123毫秒
  StringBuffer buf = new StringBuffer();
  buf.append(this.getDateComplete());
  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();
 }

 private String addZero(int temp, int len) {
  StringBuffer str = new StringBuffer();
  str.append(temp);// 加入数字
  while (str.length() < len) {
   str.insert(0, 0); // 在第一个位置加上字母0
  }
  return str.toString();
 }
 
 public static void main(String[] args) {
  System.out.println(new DateTimeDemo().getDateTimeComplete());
 }

}