我可以在java中解析String到Time

时间:2012-12-14 08:54:24

标签: android

我正在使用android.text.format.Time,我需要一个获取字符串并将该字符串转换为Time对象的函数。出于某种其他原因,我最初将时间对象存储为字符串。我现在只需将字符串传递给Time对象。 代码:

 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)

参数: time - 要解析的字符串 返回: true - 如果生成的时间值是UTC时间

1 个答案:

答案 0 :(得分: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...