• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating numer of days between future date and current date  RSS feed

 
bujji yarra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi , I'm working on web application where i need to select a date from date from dojo date picker and send it to the server. At server end i need to calculate the days difference between the future date which come from the webpage and the current date . and i need to send the days difference to the webservice . When I read the it from service it will reurn remaing days from the current date, depeninding on the remianing days from the current date i need calculate the date and send it to the user which should to display the user selected date essentially. its working fine when the server and the users are in same timezone , but if i test when browser is in IST timezone and server is in MST timezone i'm getting one less days than actual days. Please help me to solve the issue ...

in java script ..
content : {

enteredDate : new Date(parseDate);

},
in java (Spring contrller) :--

@RequestParam("enteredDate") Date enteredDate

and the days calculation

Date currentdate = new Date();
int diffInDays = (int)((request.getEnteredDate().getTime()-currentdate.getTime())/(1000*60*60*24));

our server will be in MST time zone always , but users may be in diff time zone. Please help me how to solve the issue.

 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bujji,

Welcome to the Ranch!

Please read the following: UseCodeTags, ShowSomeEffort (←click links)

you should use TimeZone and translate to/from a common time zone, such as UTC. I would not make the assumption that the server will always be in MST. You're just leaving yourself wide open to problems when someone in your data center decides to migrate your application to a server in another time zone.

The Joda-Time library has the concept of Duration. I highly recommend Joda-Time.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bujji yarra wrote:our server will be in MST time zone always , but users may be in diff time zone. Please help me how to solve the issue.

Basically, it then depends on what you mean by a "day". If a day is always defined in MST terms, then Junilu's method is the way to go; however, from what I see, what you've written should work fine too.

If a day is defined in clock time, then you'll probably have to use Calendars (I'd use DAY_OF_YEAR). It seems an odd way to do things though.

Winston
 
Ramesh Pramuditha Rathnayake
Ranch Hand
Posts: 178
2
Java MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can easily do this with Calender class in java.util package. Refer API for help..!
It easy to work with Calender....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!