2012-12-14 28 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...