我有一个要求计算两个日期之间的剩余天数。它对我来说工作得很好。但问题是,当我发送到英国的构建,那里显示比印度少一天(例如:印度:215天,但在英国:214天)。我使用下面的代码:计算两个日期之间的天数
public static int noOfDays(String currDate, String tarDate){
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date date1 = null;
Date date2 = null;
try {
date1 = sdf.parse(currDate);
date2 = sdf.parse(tarDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (int)((date2.getTime() - date1.getTime())/(1000 * 60 * 60 * 24));
}
为了得到当前日期:
public static String getcurrentdate()
{
Calendar now = Calendar.getInstance();
int month = now.get(Calendar.MONTH) + 1;
int year = now.get(Calendar.YEAR);
int day = now.get(Calendar.DATE);
return day+"/"+month+"/"+year;
}
,我相信现在的日历= Calendar.getInstance();将仅给出本地时区实例。如果它是正确的,它应该在任何位置给出相同的结果,但结果看起来不同。请帮我解决这个问题。
感谢, Ganesh神
你可以使用Calendar.setTimeZone()方法来设置英国时区 –
看看这个http://stackoverflow.com/questions/230126/how-to-handle-calendar-timezones-using-java – RNJ
当前日期取自机器当前的部署时间,设置时区 –