首页 > 解决方案 > 如何在文本小部件中读取 LocalTime 变量

问题描述

  import org.eclipse.swt.widgets.Text;
  ...
  private Text textora;
   ...
  LocalTime ora = textora.getLocalTime();

我无法从文本格式中读取 LocalTime 变量以进行输入

错误:

Multiple markers at this line
- The method getLocalTime() is undefined for the 
 type Text
- The method getTime() is undefined for the type 
 Text

标签: javaeclipseswt

解决方案


SWTText控件只是纯文本,只有一种getText方法可以获取输入的字符串。要将文本转换为LocalTime您必须解析文本:

String timeStr = textora.getString();

LocalTime time = LocalTime.parse(timeStr);

parseDateTimeParseException如果字符串不是有效时间,将抛出异常。

您还可以使用 SWTDateTime控件来允许输入时间:

import org.eclipse.swt.widgets.DateTime;

...

DateTime dt = new DateTime(parent, SWT.TIME);

...

LocalTime time = LocalTime.of(dt.getHours(), dt.getMinutes(), dt.getSeconds());

推荐阅读