• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can DateSpinner be used with java.time API ?

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could use a custom spinner model and editor: Temporal Spinners.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic