简体   繁体   English

问题在java中获取未来日期和当前日期之间的天数差异

[英]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. 有人请帮我解决时区问题。

To Offset for time differences 抵消时差

You should use a format to send the data that includes the timezone within it. 您应该使用格式发送包含其中时区的数据。 You could: 你可以:

  1. Use UNIX time which does not use timezones (milliseconds since epoch) with GMT 00, and also use this on java side 使用不使用GMT 00的时区(自纪元以来的毫秒)的UNIX时间,并在java端使用此时间

see: Get current date/time in seconds 请参阅: 以秒为单位获取当前日期/时间

  1. Use ISO-8601 which is a standard and can include the timezone as well of the browser, and then parse this server side: 使用ISO-8601这是一个标准,并且可以包括浏览器的时区,然后解析此服务器端:

see: 看到:

To Calculate the Difference (using MS and Calendar Object) 计算差异(使用MS和日历对象)

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.

相关问题 使用Java 8的给出日期(UTC日期)和当前日期之间的差异(天) - Difference between give date (UTC date) and current date in days using Java 8 Java:生成一个范围之间的随机日期(从当前日期/时间到随机未来日期的范围(例如,从当前日期/时间开始的5-10天) - Java: Generate a random date between a range (range starting from current date/time to a random future date (e.g 5-10 days from current date/time) 如何使用Java计算数据库与当前日期之间的天数差异 - How to calculate difference in days between the one from database and current date using java Java7的日期差异(以天为单位) - Date difference in days for Java7 Android / Java - 日期差异 - Android/Java - Date Difference in days python中两天之间的日期差异(从JAVA代码迁移) - Date difference between two days in python (migration from JAVA code) 用于查找年,月和日中2个日期对象之间差异的Java方法 - Java method to find difference between 2 date objects in years, months and days Java中当前时间和未来时间之间的时差 - Difference between current time and future time in java 计算当前日期和过去日期之间的差额 - Calculate the difference between Current Date and Past Date 在计算 Sailpoint 中用户的当前日期/时间和开始日期/时间之间的差异时出现无法解析的日期错误 - Getting an Unparseable date error while calculating difference between Current date/time and Start date/time for an user in Sailpoint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM