• 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
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Setting date and time of Date object, separately

 
Ranch Hand
Posts: 199
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've created a method `createDateTime` which takes 2 parameters. The first parameter will be a date in String format (American style - MM/dd/yyyy).
The second parameter will be time in AM/PM style.


The problem I am running into is as follows: When I use `DateFormat` to parse my `time` input parameter, I surely get a date object, with the time set, but as soon as I try to set `date` on it, the time gets overwritten!

Ultimately, I want to return a generate/return a string which I will write to an SQLite database, in this format:
"YYYY-MM-DD HH:MM:SS.SSS"

I'll be happy if I can return a Date object first though... and then I can worry about turning that into a String...






 
Marshal
Posts: 69492
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Richardson wrote:. . . //apparently, I can have one or the other... I can't set time and date separately . . .

No, you are creating two different objects and when you create the second you are discarding the first.
Which Date class are you using? You know how useless java.util.Date is? Remember that it comes with setXXX() methods which you can probably use for what you want. Find out some 21st century date classes: look here in the Java™ Tutorials.
If you want a String, why not use a class designed for manipulating Strings: this, for example.
 
Mark Richardson
Ranch Hand
Posts: 199
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Mark Richardson wrote:. . . //apparently, I can have one or the other... I can't set time and date separately . . .

No, you are creating two different objects and when you create the second you are discarding the first.
Which Date class are you using? You know how useless java.util.Date is? Remember that it comes with setXXX() methods which you can probably use for what you want. Find out some 21st century date classes: look here in the Java™ Tutorials.
If you want a String, why not use a class designed for manipulating Strings: this, for example.



As instructed, I am now using the `java.time` library. However, right away, I receive an exception where "05:05 PM" could not be parsed.




 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And is it really necessary to pass the timestamp to that method in two separate parts? If you're stuck with that, then the first thing you should do inside the method is to combine them into a single string which looks like "11/01/2020 05:05 PM". Then you can easily create a pair of formatters to transform that to the SQLite standard form.
 
Mark Richardson
Ranch Hand
Posts: 199
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:And is it really necessary to pass the timestamp to that method in two separate parts? If you're stuck with that, then the first thing you should do inside the method is to combine them into a single string which looks like "11/01/2020 05:05 PM". Then you can easily create a pair of formatters to transform that to the SQLite standard form.



Yasssssssss! :D So simple and right under my nose. Thank you! and Thank you to Campbell Ritchie for pointing out the obsolete Date library I was using.
 
Campbell Ritchie
Marshal
Posts: 69492
277
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you were making the same mistake again; you created an object representing the time, and probably today's date (line 18) and then replacing it with the date only in line 21. You can create a LocalDate and then add a time to create a new object.
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
Try Free Java/.NET Libraries for Word Excel PowerPoint and PDF
htttp://www.e-iceblue.com/free-apis.html
    Bookmark Topic Watch Topic
  • New Topic