Java 计算两个日期之间的月份、分钟等
m0skit0在这里,它在.Net中。八行代码。用Java显示它。如果你认为你能证明这一点Java 计算两个日期之间的月份、分钟等,java,android,Java,Android,m0skit0在这里,它在.Net中。八行代码。用Java显示它。如果你认为你能证明这一点 Dim StartDate As Date = Now Dim EndDate As Date = Now EndDate = EndDate.AddSeconds(15) Dim Months As Long = DateDiff(DateInterval.Month, StartDate, EndDate) Dim Days As Long = DateDiff(DateInterval.Day,
Dim StartDate As Date = Now
Dim EndDate As Date = Now
EndDate = EndDate.AddSeconds(15)
Dim Months As Long = DateDiff(DateInterval.Month, StartDate, EndDate)
Dim Days As Long = DateDiff(DateInterval.Day, StartDate, EndDate)
Dim Hours As Long = DateDiff(DateInterval.Hour, StartDate, EndDate)
Dim Minutes As Long = DateDiff(DateInterval.Minute, StartDate, EndDate)
Dim Seconds As Long = DateDiff(DateInterval.Second, StartDate, EndDate)
下面的代码是错误的。以Calendar cal2=开始的下半部分将在TimerTask中计算两次之间的差异,并将其分解为月、日、小时、分钟和秒
在顶部,我得到当前日期/时间,然后再加上15秒。然后尝试计算差异。这似乎取决于我在第二次约会上增加了多少时间。我已经为此挣扎了好几天了。在某一点上,除了休息4小时之外,其他一切都匹配,但我是GMT+8,所以这毫无意义。这就是我在时区(UTC)中添加的内容。我读过Joda time的文章,但我对Java和Android还是新手,在几次尝试安装这些库之后,我又回到了纯Java。痛苦
谁设计过日期/时间?要么是疯子,要么是受虐狂,要么两者兼而有之
(在.Net中,这是8行代码。8行!)
这项工作没有任何额外的进口,它占闰年
import java.util.Calendar;
public class Interval {
int currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond,
otherYear, otherMonth, otherDay, otherHour, otherMinute, otherSecond;
public static void main(String[] args) {
new Interval();
}
public Interval() {
Calendar cal = Calendar.getInstance();
//Set current date values
currentYear = cal.get(Calendar.YEAR);
currentMonth = cal.get(Calendar.MONTH);
currentDay = cal.get(Calendar.DAY_OF_MONTH);
currentHour = cal.get(Calendar.HOUR_OF_DAY); //Automatically in military time
currentMinute = cal.get(Calendar.MINUTE);
currentSecond = cal.get(Calendar.SECOND);
//Set other date values for testing
otherYear = 2015;//year is the same
otherMonth = 5;//months start at 0 (0-11)
otherDay = 1;//days start at 1
otherHour = 19;//hours start at 0 (0-23, military time)
otherMinute = 59;//minutes start at 0 (0-59)
otherSecond = 0;//seconds start at 0 (0-59)
int yearDifference = 0, monthDifference = 0, dayDifference = 0, hourDifference = 0,
minuteDifference = 0, secondDifference = 0;
//Calculate Differences
yearDifference = otherYear - currentYear;
monthDifference = otherMonth - currentMonth;
dayDifference = otherDay - currentDay;
hourDifference = otherHour - currentHour;
minuteDifference = otherMinute - currentMinute;
secondDifference = otherSecond - currentSecond;
//Determine Days in given month
int daysInCurrentMonth = 0;
if(currentMonth == 1) {
//Check for Leap Year
if(currentYear % 4 == 0) {
daysInCurrentMonth = 29;
}
else {
daysInCurrentMonth = 28;
}
}
else if(currentMonth == 0 || currentMonth == 2 || currentMonth == 4 || currentMonth == 6 ||
currentMonth == 7 || currentMonth == 9 || currentMonth == 11) {
daysInCurrentMonth = 31;
}
else {
daysInCurrentMonth = 30;
}
//Deduct 1 from upper value if current is negative
if(secondDifference < 0) {
minuteDifference--;
secondDifference += 60;
}
if(minuteDifference < 0) {
hourDifference--;
minuteDifference += 60;
}
if(hourDifference < 0) {
dayDifference--;
hourDifference += 24;
}
if(dayDifference < 0) {
monthDifference--;
dayDifference += daysInCurrentMonth;
}
if(monthDifference < 0) {
yearDifference--;
monthDifference += 12;
}
System.out.println("Difference of " + yearDifference + " years, " + monthDifference + " months, " + dayDifference + " days, " +
hourDifference + " hours, " + minuteDifference + " minutes, and " + secondDifference + " seconds.");
}
}
import java.util.Calendar;
公课间隔{
int currentYear、currentMonth、currentDay、currentHour、currentMinute、currentSecond、,
其他年、其他月、其他日、其他小时、其他分钟、其他秒;
公共静态void main(字符串[]args){
新区间();
}
公共间隔(){
Calendar cal=Calendar.getInstance();
//设置当前日期值
currentYear=cal.get(日历年);
currentMonth=cal.get(日历.MONTH);
currentDay=cal.get(日历日期,每月的第天);
currentHour=cal.get(Calendar.HOUR OF_DAY);//在军事时间自动
currentMinute=cal.get(Calendar.MINUTE);
currentSecond=cal.get(Calendar.SECOND);
//设置测试的其他日期值
otherYear=2015;//年份相同
otherMonth=5;//月份从0(0-11)开始
otherDay=1;//天从1开始
otherHour=19;//小时从0开始(0-23,军事时间)
otherMinute=59;//分钟从0开始(0-59)
otherSecond=0;//秒从0开始(0-59)
年内差异=0,月差异=0,日差异=0,小时差异=0,
分钟差=0,秒差=0;
//计算差异
年份差异=其他年份-当前年份;
monthDifference=其他月份-当前月份;
日差=其他日-当前日;
hourDifference=其他小时-当前小时;
分钟差异=其他分钟-当前分钟;
秒差=其他秒-当前秒;
//确定给定月份的天数
int daysInCurrentMonth=0;
如果(当前月份==1){
//检查闰年
如果(当前年份%4==0){
daysInCurrentMonth=29;
}
否则{
daysInCurrentMonth=28;
}
}
如果(currentMonth==0 | | currentMonth==2 | | currentMonth==4 | | currentMonth==6||
当前月==7 | |当前月==9 | |当前月==11){
daysInCurrentMonth=31;
}
否则{
daysInCurrentMonth=30;
}
//如果电流为负,则从上限值中减去1
如果(二次差<0){
分钟差异--;
二次差+=60;
}
if(分钟差异<0){
时差--;
分钟差+=60;
}
如果(小时差异<0){
日差--;
时差+=24;
}
如果(日差<0){
月差--;
日差+=当前月的日数;
}
如果(月差<0){
年差--;
月差+=12;
}
System.out.println(“年差+年差+月差+月差+日差+日差,”+
小时差+小时差+分钟差+分钟差+秒差);
}
}
import java.util.Calendar;
public class Interval {
int currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond,
otherYear, otherMonth, otherDay, otherHour, otherMinute, otherSecond;
public static void main(String[] args) {
new Interval();
}
public Interval() {
Calendar cal = Calendar.getInstance();
//Set current date values
currentYear = cal.get(Calendar.YEAR);
currentMonth = cal.get(Calendar.MONTH);
currentDay = cal.get(Calendar.DAY_OF_MONTH);
currentHour = cal.get(Calendar.HOUR_OF_DAY); //Automatically in military time
currentMinute = cal.get(Calendar.MINUTE);
currentSecond = cal.get(Calendar.SECOND);
//Set other date values for testing
otherYear = 2015;//year is the same
otherMonth = 5;//months start at 0 (0-11)
otherDay = 1;//days start at 1
otherHour = 19;//hours start at 0 (0-23, military time)
otherMinute = 59;//minutes start at 0 (0-59)
otherSecond = 0;//seconds start at 0 (0-59)
int yearDifference = 0, monthDifference = 0, dayDifference = 0, hourDifference = 0,
minuteDifference = 0, secondDifference = 0;
//Calculate Differences
yearDifference = otherYear - currentYear;
monthDifference = otherMonth - currentMonth;
dayDifference = otherDay - currentDay;
hourDifference = otherHour - currentHour;
minuteDifference = otherMinute - currentMinute;
secondDifference = otherSecond - currentSecond;
//Determine Days in given month
int daysInCurrentMonth = 0;
if(currentMonth == 1) {
//Check for Leap Year
if(currentYear % 4 == 0) {
daysInCurrentMonth = 29;
}
else {
daysInCurrentMonth = 28;
}
}
else if(currentMonth == 0 || currentMonth == 2 || currentMonth == 4 || currentMonth == 6 ||
currentMonth == 7 || currentMonth == 9 || currentMonth == 11) {
daysInCurrentMonth = 31;
}
else {
daysInCurrentMonth = 30;
}
//Deduct 1 from upper value if current is negative
if(secondDifference < 0) {
minuteDifference--;
secondDifference += 60;
}
if(minuteDifference < 0) {
hourDifference--;
minuteDifference += 60;
}
if(hourDifference < 0) {
dayDifference--;
hourDifference += 24;
}
if(dayDifference < 0) {
monthDifference--;
dayDifference += daysInCurrentMonth;
}
if(monthDifference < 0) {
yearDifference--;
monthDifference += 12;
}
System.out.println("Difference of " + yearDifference + " years, " + monthDifference + " months, " + dayDifference + " days, " +
hourDifference + " hours, " + minuteDifference + " minutes, and " + secondDifference + " seconds.");
}
}