如何取得系统时间

取得方法1
import java.text.SimpleDateFormat; 
import java.util.Date; 

class DataTime { // 以后可以直接调用此类,获取时间日期 

         private SimpleDateFormat sdf    = null; //声明SimpleFormatter对象 
    
   public String getDate(){     // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS 
     this.sdf =  new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss.SSS") ; 
     return  this.sdf.format( new Date()) ; // 将当前日期进行格式化操作 
  } 
   public String getDateComplete(){     // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒 
     this.sdf =  new SimpleDateFormat( "yyyy年MM月dd日HH时mm分ss秒SSS毫秒") ; 
     return  this.sdf.format( new Date()) ; // 将当前日期进行格式化操作 
  } 
   public String getTimeStamp(){     // 得到的是一个时间戳 
     this.sdf =  new SimpleDateFormat( "yyyyMMddHHmmssSSS") ; 
     return  this.sdf.format( new Date()) ; // 将当前日期进行格式化操作 
  } 

    


public  class DataTimeDemo { 

   /** 
    * SimpleDateFormat获取时间 
    */
 
   public  static  void main(String[] args) { 
     // TODO Auto-generated method stub 
    DataTime dataTime =  new DataTime(); 
    System.out.println( "取得系统日期\n" + dataTime.getDate()); 
    System.out.println( "取得中文格式系统日期\n" + dataTime.getDateComplete()); 
    System.out.println( "取得时间戳\n" + dataTime.getTimeStamp()); 
  } 


取得方法2
import java.util.Calendar; 
import java.util.GregorianCalendar; 

class DataTime { // 以后可以直接调用此类,获取时间日期 
   private Calendar calendar =  null; // 声明Calendar对象 
   public DataTime() { // 构造方法中直接实例化 
     super(); 
     this.calendar =  new GregorianCalendar(); 
  } 

   public String getData() { 
     // 得到的是一个日期:格式为:yyyy-MM-dd HH:mm:ss.SSS 
     // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能 
    StringBuffer sb =  new StringBuffer(); 
    sb.append(calendar.get(Calendar.YEAR)).append( "-");  // 增加年 
    sb.append( this.addZero(calendar.get(Calendar.MONTH) + 1, 2)) 
        .append( "-");  // 增加月 
    sb.append( this.addZero(calendar.get(Calendar.DAY_OF_MONTH), 2)).append( 
         " ");  // 取得日 
    sb.append( this.addZero(calendar.get(Calendar.HOUR_OF_DAY), 2)).append( 
         ":");  // 取得时 
    sb.append( this.addZero(calendar.get(Calendar.MINUTE), 2)).append( ":"); 
    sb.append( this.addZero(calendar.get(Calendar.SECOND), 2)).append( "."); 
    sb.append( this.addZero(calendar.get(Calendar.MILLISECOND), 3)); 
     return sb.toString(); 
  } 

   public String getDateComplete() { 
     // 得到的是一个日期:格式为:yyyy年MM月dd日 HH时mm分ss秒SSS毫秒 
     // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能 
    StringBuffer buf =  new StringBuffer(); 
    buf.append(calendar.get(Calendar.YEAR)).append( "年");  // 增加年 
     // 注意补0的操作 
    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(); 
  } 

   private Object addZero( int num,  int len) { // 参数,长度 
     // 由于月份有一位数(9月)和两位数(10月)之分,所以要进行补“0”操作。 
    StringBuffer s =  new StringBuffer(); 
    s.append(num); 
     while (s.length() < len) {  // 如果长度不足,则继续补0 
      s.insert(0,  "0");  // 在第一个位置处补0 
    } 
     return s.toString(); 

  } 

   public String getTimeStamp() {  // 得到的是一个时间戳即无间隔显示一串用时间组成的数 
     // 考虑到程序要频繁修改字符串,所以使用StringBuffer提升性能 
    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), 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(); 

  } 


public  class DataTimeDemo { 

   /** 
    * Calendar获取时间 
    */
 
   public  static  void main(String[] args) { 
     // TODO Auto-generated method stub 
    DataTime dataTime =  new DataTime(); 
    System.out.println( "取得系统日期\n" + dataTime.getData()); 
    System.out.println( "取得中文格式系统日期\n" + dataTime.getDateComplete()); 
    System.out.println( "取得时间戳\n" + dataTime.getTimeStamp()); 
  } 


本文出自 “Android的一点一滴” 博客,请务必保留此出处http://haiyuanxi.blog.51cto.com/4230602/931157

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值