Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I convert a Timestamp to a Calendar?

 
keith tyson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a method that returns a Timestamp and I need to pass this to a method that is expecting a Calendar object.
ie.
deliveryDetails.getWindowStart() // returns a Timestamp
and I need to pass another method this value as a Calendar.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Calendar cal = Calendar.getInstance();
cal.setTime(timestamp);
This will give a Calendar object that has the correct time, but unfortunately it will also set the Date portion to the 1st of January 1970. If you need to get rid of that,
cal.clear(Calendar.YEAR);
cal.clear(Calendar.DAY_OF_YEAR);
Should do it.
- Peter
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!