[英]Issue getting the days difference between future date and current date in java
I need to pick a future date from calendar, suppose the date I am selecting is 04/30/2013. 我需要从日历中选择一个未来的日期,假设我选择的日期是04/30/2013。 Now what I want is to send the date to server.
现在我想要的是将日期发送到服务器。 At server end I need to calculate the number of days between the future date and current date and send it to database.
在服务器端,我需要计算未来日期和当前日期之间的天数,并将其发送到数据库。
The problem is that when I do the calculation locally (because my server and browser are in same timezone) it works fine, but when the server is in a different timezone than the browser the difference in days does not come as expected. 问题是,当我在本地进行计算时(因为我的服务器和浏览器在同一时区),它可以正常工作,但是当服务器与浏览器处于不同的时区时,天数的差异不会达到预期。 Someone please help me how to solve the timezone issues.
有人请帮我解决时区问题。
You should use a format to send the data that includes the timezone within it. 您应该使用格式发送包含其中时区的数据。 You could:
你可以:
see: Get current date/time in seconds 请参阅: 以秒为单位获取当前日期/时间
see: 看到:
You need to use the calendar object. 您需要使用日历对象。 You create a calendar object and set with a future date: eg How to set Java.util.calendar to a specific time period in the future Then you can perform a calculation by subtracting the current date (as long) with the long returned by the calendar, and dividing by (1000*60*60*24).
您创建一个日历对象并使用将来日期进行设置 :例如, 如何将Java.util.calendar设置为将来的特定时间段然后您可以通过使用由返回的长度减去当前日期(一样长)来执行计算。日历,除以(1000 * 60 * 60 * 24)。
Some code I used in my own application to find entries greater than a specific datetime (midnight): 我在自己的应用程序中使用的一些代码,用于查找大于特定日期时间(午夜)的条目:
long currentTimeStamp = System.currentTimeMillis();
Calendar cal;
cal = Calendar.getInstance();
cal.setTime(new Date(currentTimeStamp));
//GET MS OF MIDNIGHT FROM BEGINNING OF THE DAY
cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND,0);
long midnight = cal.getTimeInMillis();
if(currentTimeStamp - midnight > (30*24*60*60*1000))break;
在一个公共时区中转换两个日期,例如GMT,然后计算差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.