Android時間戳與字符串相互轉換


    import java.text.ParseException;   
    import java.text.SimpleDateFormat;   
    import java.util.Date;   
     public class TestTime {   
     public static void main(String[] args) {   
     String time = "2010年12月08日11時17分00秒";    
     System.out.println(time);   
     // 字符串=======>時間戳    
     String re_str = getTime(time);  
     System.out.println(re_str);   
     // 時間戳======>字符串  String data = getStrTime(re_str);   
     System.out.println(data);    
     }   
     // 將字符串轉為時間戳  
     public static String getTime(String user_time) {  
     String re_time = null;   
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");   
     Date d;   
     try {   
     d = sdf.parse(user_time);  
     long l = d.getTime();   
     String str = String.valueOf(l);  
     re_time = str.substring(0, 10);    
     }catch (ParseException e) {   
     // TODO Auto-generated catch block e.printStackTrace();  
     }   
     return re_time;   
     }    
     // 將時間戳轉為字符串   
     public static String getStrTime(String cc_time) {   
     String re_StrTime = null;   
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH時mm分ss秒");   
     // 例如:  
     cc_time=1291778220 ;  
     long lcc_time = Long.valueOf(cc_time);   
     re_StrTime = sdf.format(new Date(lcc_time * 1000L));    
     return re_StrTime;   
     }  
     }    
     //打印結果為: 2010年12月08日11時17分00秒   
     //1291778220 2010年12月08日11時17分00秒  只精確到秒。         
      
     public class TimeStamp {       
     private long timeStamp = System.currentTimeMillis();   
         public String printTimeStamp(){        
       return "TimeStamp: " + String.valueOf(timeStamp);     
       }   
         public String swapDateToStr(){         
       SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");       
         return "Date: " + format.format(new Date(timeStamp));    
       }  
     } 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM