2012-12-14 34 views
3

我正在使用android.text.format.Time,我需要一個函數獲取一個字符串並將該字符串轉換爲一個Time對象。由於其他原因,我最初將時間對象存儲爲字符串。我只需要將字符串傳遞給Time對象。 代碼:我可以解析一個字符串在java中的時間

Time time = new Time(); 
String time = time.toString(); 

    Time t = new Time(); 
    t.parse(time); 
    this.time = t; 

現在

t.parse(time) 

給出了一個布爾值。

boolean value = time.parse(s) 

參數: 時間 - 解析 返回字符串: 真實的 - 如果所得到的時間值是在UTC時間

+2

那麼,什麼是問題?看起來你自己回答了這個問題。除非時間沒有解析方法,並且您在問如何實現您的一種解析方法。 –

回答

4
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
Date date = sdf.parse("2012-12-14 14:05:59"); 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(date.getTime()); 
Time time = new Time(); 
time.set(int second, int minute, int hourOfDay, int monthDay, int month, int year); 

// int second => calendar.get(Calendar.SECOND); 
// etc minute, hourOfDay, monthOfDay, month, year...