java 关于时间格式化

package util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.ParseException;


//时间格式转化


class DateTime{ 
  Calendar c = Calendar.getInstance();
    // 声明日期格式化操作对象,直接对new Date()进行实例化
     private SimpleDateFormat sdf = null ; 
    // 得到完整的日期,格式为:yyyy-MM-dd HH:mm:ss.SSS
     public String getDate(){ 
        this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        getTime( c);
        return this.sdf.format(c.getTime()) ;         
    } 
    // 得到完整的日期,格式为:yyyy年MM月dd日HH时mm分ss秒SSS毫秒
     public String getDateComplete(){ 
        this.sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒"); 
          return this.sdf.format(new Date()) ; 
//        return this.sdf.format(c.getTime()) ;            
    } 
    public String getTimeStamp(){   // 得到时间戳:yyyyMMddHHmmssSSS
         this.sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 
           return this.sdf.format(new Date()) ; 
//         return this.sdf.format(c.getTime()) ;
    }
    public Date getDateFromString() throws java.text.ParseException {
     Date date=null;
     String str = "2008-10-19 10:11:30.345";
      try {
       this.sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    return sdf.parse(str);
  } catch (ParseException e) {
   e.printStackTrace();
  }
  return date;
    }
    public void getTime(Calendar c){
     System.out.println("year:"+c.get(c.YEAR)+" moth:"+(c.get(c.MONTH)+1)+" day"+c.get(c.DAY_OF_MONTH));
        System.out.println("hour:"+c.get(c.HOUR_OF_DAY)+" minute:"+c.get(c.MINUTE)+" second:"+c.get(c.SECOND));
    }
   
}; 


public class DateFormatTest { 
    public static void main(String[] args) throws java.text.ParseException { 
        DateTime dt = new DateTime(); // 实例化DateTime对象
         System.out.println("系统日期:" + dt.getDate()); 
        System.out.println("中文日期::" + dt.getDateComplete()); 
        System.out.println("时间戳:" + dt.getTimeStamp());
        System.out.println("系统输出时间:" +Calendar.getInstance().getTime()+" || "+new Date());     
        System.out.println("**************test********");
        System.out.println("系统日期"+
          new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒SSS毫秒").format(dt.getDateFromString()));
        Calendar time = Calendar.getInstance();
        time.setTime(dt.getDateFromString());
        dt.getTime(time);      
    } 
}

 

 

你可能感兴趣的:(java,java,calendar,date,string,class,null)