2013年8月2日星期五

Java: String and Date, Timestamp switch between

 
  

Java: String and Date, Timestamp transfer between

 
 
  
   

a, String and Date (java.util.Date) Huzhuan

   

   

1.1 String -> Date

   

   
    
     
      Java code             
    
    
         
  1. String dateStr = "2010/05/04 12:34:23" ;
  2.      
  3. Date date = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new Date ();
  4.      
  5. / / note format and date format to match the format String
  6.      
  7. DateFormat sdf = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy / MM / dd HH: mm: ss" );
  8.      
  9. try {
  10.      
  11. date = sdf.parse (dateStr);
  12.      
  13. System.out.println (date.toString ());
  14.      
  15. } catch (Exception e) {
  16.      
  17. e.printStackTrace ();
  18.      
  19. }
  20.     
   
   

   

   

1.2 Date -> String

   

   

date to string conversion, you can set any format conversion format

   
    
     
      Java code             
    
    
         
  1. String dateStr = "" ;
  2.      
  3. Date date = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new Date ();
  4.      
  5. / / format can be any format
  6.      
  7. DateFormat sdf = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy / MM / dd HH: mm: ss" );
  8.      
  9. DateFormat sdf2 = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy-MM-dd HH / mm / ss" );
  10.      
  11. try {
  12.      
  13. dateStr = sdf.format (date);
  14.      
  15. System.out.println (dateStr);
  16.      
  17. dateStr = sdf2.format (date);
  18.      
  19. System.out.println (dateStr);
  20.      
  21. } catch (Exception e) {
  22.      
  23. e.printStackTrace ();
  24.      
  25. }
  26.     
   
   

two, String and Timestamp referrals

   

   

   

2.1 String -> Timestamp

   

   

   

use the Timestamp valueOf () method

   
    
     
      Java code             
    
    
         
  1. Timestamp ts = new Timestamp (System.currentTimeMillis ());
  2.      
  3. String tsStr = < span class = "string" style = "color: blue;"> "2011-05-09 11:49:45" ; < / span>
  4.      
  5. try {
  6.      
  7. ts = Timestamp.valueOf (tsStr);
  8.      
  9. System.out.println (ts);
  10.      
  11. } catch (Exception e) {
  12.      
  13. e.printStackTrace ();
  14.      
  15. }
  16.     
   
   

; Note: String type must be of the form: yyyy-mm-dd hh: mm: ss [. f. ..] such a format, brackets indicate optional, otherwise an error! ! !

   

If the String to other formats, you can consider re-parse the string under and then re ~ ~

   

   

2.2 Timestamp -> String

   

   

Timestamp use the toString () method or borrow DateFormat

   

   
    
     
      Java code             
    
    
         
  1. Timestamp ts = new Timestamp (System.currentTimeMillis ());
  2.      
  3. String tsStr = < span class = "string" style = "color: blue;"> "" ;
  4.      
  5. DateFormat sdf = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new SimpleDateFormat ( "yyyy / MM / dd HH: mm: ss" );
  6.      
  7. try {
  8.      
  9. / / Method One
  10.      
  11. tsStr = sdf.format (ts);
  12.      
  13. System.out.println (tsStr);
  14.      
  15. / / method two
  16.      
  17. tsStr = ts.toString ();
  18.      
  19. System.out.println (tsStr);
  20.      
  21. } catch (Exception e) {
  22.      
  23. e.printStackTrace ();
  24.      
  25. }
  26.     
   
   

was easily able to see it, the method has the advantage of a flexible set of strings can form.

   

three, Date ( java.util.Date ) and Timestamp referrals

   

   

statement: check API shows, Date and Timesta parent-child class relationships

   

   

   

3.1 Timestamp -> Date

   

   

   
    
     
      Java code             
    
    
         
  1. Timestamp ts = new Timestamp (System.currentTimeMillis ());
  2.      
  3. Date date = < span class = "keyword" style = "color: # 7f0055; font-weight: bold;"> new Date ();
  4.      
  5. try {
  6.      
  7. date = ts;
  8.      
  9. System.out.println (date);
  10.      
  11. } catch (Exception e) {
  12.      
  13. e.printStackTrace ();
  14.      
  15. }
  16.     
   
   

very simple, but at the moment the date object is an entity pointing Timestamp, that date has a Date class methods, but the method of execution was covered entities Timestamp medium.

   

   

   

3.2 Date -> Timestamp

   

   

parent class can not be converted directly to the sub-class, can make use of in the middle of String ~ ~ ~ ~

   

Note: Use the following method is more simple

   

Timestamp ts = new Timestamp (date.getTime ());

  
 

没有评论:

发表评论