如何使用 LocalTime 对象设置 JSpinner 的值?

huangapple go评论67阅读模式
英文:

How to set the value of a JSpinner using a LocalTime object?

问题

以下是翻译好的内容:

我正在尝试使用 JSpinner 和 Java 8 的时间 API 来创建一个时间选择器。我希望创建一个仅包含时间而不包括日期的时间选择器。

之前,我使用 SimpleDateFormat,但是当我打印出 JSpinner 的值时,它会返回带有日期的时间。

那么,我如何将 LocalTime 对象的值传递给 JSpinner 的 setValue 方法呢?

以下是我的当前代码:

JSpinner timeSpinner = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
timeSpinner.setEditor(timeEditor);
LocalTime newTime = LocalTime.of(11, 9);
timeSpinner.setValue(newTime);
		
timeSpinner.setBounds(126, 55, 56, 22);
contentPanel.add(timeSpinner);

或者我对此方法的处理方式是否有误?

这是我的原始代码,它是有效的:

JSpinner timeSpinner = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
timeSpinner.setEditor(timeEditor);
SimpleDateFormat time = new SimpleDateFormat("HH:mm");
try {
	timeSpinner.setValue(time.parseObject("10:00"));
} catch (ParseException timeError) {
	timeError.printStackTrace();
}
timeSpinner.setBounds(126, 55, 56, 22);
contentPanel.add(timeSpinner);
英文:

I am trying to make a time picker using JSpinner and Java 8's time API. I want to make a time picker without date.

Before, I was using SimpleDateFormat, but that would return time with date when I print out the value of the JSpinner.

So how can I pass the value of a LocalTime object into the setValue method of a JSpinner?

Here is my current code:

JSpinner timeSpinner = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
timeSpinner.setEditor(timeEditor);
LocalTime newTime = LocalTime.of(11, 9);
timeSpinner.setValue(newTime);
		
timeSpinner.setBounds(126, 55, 56, 22);
contentPanel.add(timeSpinner);

Or am I taking the wrong approach to this?

This is my original code which works:

JSpinner timeSpinner = new JSpinner(new SpinnerDateModel());
JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(timeSpinner, "HH:mm");
timeSpinner.setEditor(timeEditor);
SimpleDateFormat time = new SimpleDateFormat("HH:mm");
try {
	timeSpinner.setValue(time.parseObject("10:00"));
} catch (ParseException timeError) {
	timeError.printStackTrace();
}
timeSpinner.setBounds(126, 55, 56, 22);
contentPanel.add(timeSpinner);

答案1

得分: 0

是的,SpinnerDateModel类的方法getValue()会返回一个java.util.Date的实例,但你可以按以下方式将其转换为LocalTime

Object obj = timeSpinner.getValue();
if (obj instanceof java.util.Date) {
    java.util.Date theDate = (java.util.Date) obj;
    java.time.Instant inst = theDate.toInstant();
    java.time.ZoneId theZone = java.time.ZoneId.systemDefault();
    java.time.LocalTime theTime = java.time.LocalTime.ofInstant(inst, theZone); // JDK 9及以后版本
    /*
    java.time.LocalTime theTime = java.time.LocalTime.from(java.time.ZonedDateTime.ofInstant(inst, theZone)); // JDK 8及以后版本
     */
    System.out.println(theTime);
}

正如你所见,有两种不同的方式可以将java.util.Date转换为LocalTime,取决于你使用的JDK版本。

上述代码将以HH:mm的格式打印出theTime

英文:

Yes, method getValue(), of class SpinnerDateModel, returns an instance of java.util.Date() but you can convert that into a LocalTime as follows:

Object obj = timeSpinner.getValue();
if (obj instanceof java.util.Date) {
    java.util.Date theDate = (java.util.Date) obj;
    java.time.Instant inst = theDate.toInstant();
    java.time.ZoneId theZone = java.time.ZoneId.systemDefault();
    java.time.LocalTime thetime = java.time.LocalTime.ofInstant(inst, theZone); // Since JDK 9
    /*
    LocalTime.from(ZonedDateTime.ofInstant(inst, theZone)); // Since JDK 8
     */
    System.out.println(thetime);
}

As you can see, there are two ways to convert java.util.Date to LocalTime, depending on which JDK version you are using.

The above code will print theTime in format HH:mm

huangapple
  • 本文由 发表于 2020年8月20日 17:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63502597.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定