Win a copy of Building Blockchain Apps this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

can DateSpinner be used with java.time API ?

 
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]
 
Sheriff
Posts: 21866
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
Marshal
Posts: 68147
258
  • 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: 68147
258
  • 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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a custom spinner model and editor: Temporal Spinners.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!