This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Util package date

 
prince davies
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
previousPlanEffectiveDate != null && previousPlanEffectiveDate.after(date)

I am checking previousPlanEffectiveDate with a date selected from calendar.Do I need to format these two dates to call after method of Date?
Do I need to make both the dates have same format(DD/MM/YYYY)?

If two dates return two different format,will after method still check the conditions whether its after or before?

date : Sun Mar 15 00:00:00 PST 2009
Previous Plan EffectiveDate : 2009-05-08 21:00:00.0


previousPlanEffectiveDate != null && previousPlanEffectiveDate.after(date)
2009-05-08 21:00:00.0 != null && (2009-05-08 21:00:00.0)after(Sun Mar 15 00:00:00 PST 2009 )

Will this dates give the desired result? Do I need to format both dates to same format for chacking with after method?
Ifso,how do I format these two dates?

Do both dates need to be in same time zone and hour?

What would be the scenario in which this check will fail for the above case? Some times it fails the check.I cannot figure it out.

Please guide me.

Thanks
p


 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Date or Calendar objects have no formatting; they can be formatted into string objects, but then you can't apply methods like before/after. So this: "(2009-05-08 21:00:00.0)after(Sun Mar 15 00:00:00 PST 2009)" doesn't make sense because it deals with String objects, not Dates or Calendars.

If you want to consider different time zones then you should use Calendar, not Date - Date has no concept of time zones.
If you have two calendar objects, then they don't need to be in the same time zone in order to be compared - the Calendar class knows how to take that into account.
 
prince davies
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot!

My scenario is comparing two Date objects,not string object using after method.But my concern is whether I need to worry about format of thsese two date objects while comparing with after method? Or after method will not consider the formats of date objects?

Please confirm.

date : Sun Mar 15 00:00:00 PST 2009[Calendar Object = selected from javascript calendar object and assigned to date object]
Previous Plan EffectiveDate : 2009-05-08 21:00:00.0 [assigned to Date Object]

And comparing these two objects using after method.is this returns desired result?

previousPlanEffectiveDate != null && previousPlanEffectiveDate.after(date)
2009-05-08 21:00:00.0 != null && (2009-05-08 21:00:00.0)after(Sun Mar 15 00:00:00 PST 2009 )

these two dates are printed using System.out.prinltn(2009-05-08 21:00:00.0,Sun Mar 15 00:00:00 PST 2009 )

Time Zone is not important in my case.


One question not related to above question
I have one date object and another calender object.Is it possible to compare these two?I do not know the time zone of the date object.

 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prince davies wrote:My scenario is comparing two Date objects,not string object using after method.But my concern is whether I need to worry about format of thsese two date objects while comparing with after method? Or after method will not consider the formats of date objects?

Please confirm.

after, before, compareTo, all work on Date objects, and Date objects don't have any formatting. Sure, toString() returns something, but that's just so you see what the Date object means.

One question not related to above question
I have one date object and another calender object.Is it possible to compare these two?I do not know the time zone of the date object.

Calendar has two methods called getTime() and setTime(Date). Your choice which one you want to use
 
prince davies
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to consider different time zones then you should use Calendar, not Date - Date has no concept of time zones.
If you have two calendar objects, then they don't need to be in the same time zone in order to be compared - the Calendar class knows how to take that into account.



Question 1)According to your reply,When it comes to the comparison using before(),after(),Date Object and Calendar does not care about the time zones? Is this right?

Question 2)How date objects and calendar objects are compared in java?

Question 3) What algorithm is being used by JAVA API to compare date and claendar objects ?

Question 4) Does JAVA API knocks out the time zone difference ,before comparing date and calendar ojects?
 
Campbell Ritchie
Sheriff
Posts: 48445
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answers should be in the Calendar and Date documentation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic