【问题标题】:Can I parse a String to Time in java我可以在java中将字符串解析为时间吗
【发布时间】:2012-12-14 08:54:24
【问题描述】:

我正在使用 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 时间

【问题讨论】:

  • 那么问题是什么?看起来你自己回答了这个问题。除非 Time 没有 parse 方法,并且您正在询问如何实现您的一个 parse 方法。

标签: android


【解决方案1】:
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...

【讨论】:

    猜你喜欢
    • 2017-11-04
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多