• Post Reply Bookmark Topic Watch Topic
  • New Topic

can DateSpinner be used with java.time API ?  RSS feed

 
Scott A Burch
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using a DateSpinner to get a date and time to begin a task. I am trying to use java.time, but the error I am getting when converting to string through the formatter tells me that the getValue method is outputting a util.date, incompatible with java.time. Is there a way to get a string value from DateSpinner that is compatible with java.time? Or should I not use java.time and go back to using the older util.date?

Thanks as always!


[code=java]

try {
//Date and time to start event

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd-yy hh:mm a");
String aTime = formatter.format(aSpinner.getValue()); // this is the problem!
jTextArea1.append("date is " + aTime);
LocalDateTime aDateTime = LocalDateTime.parse(aTime, formatter);
Instant instant = aDateTime.atZone(ZoneId.systemDefault()).toInstant();
Date aDate = Date.from(instant);

timer.schedule(new ATask(), aDate);

} catch (DateTimeException e) {
System.exit(0);
}


[/code]
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case I don't see why you need to use java.time when you convert the LocalDateTime into a Date anyway. But you can also do the opposite - turn the Date into an Instant, then convert that into a LocalDateTime.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. The date chooser applet long predates the java.time package. Have a look in the Java™ Tutorials and see whether there is any simple way to convert java.util.Date to a newer date class.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Damn! Rob Spoor hasn't lost his touch. He has give you the answer less than 1 minute before I posted anything.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a custom spinner model and editor: Temporal Spinners.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!