
public class DateSimple {
public static void main(String[] args) {
String startTime = "2012-07-01";
String endTime = "2014-07-31";
String[] arg1 = startTimesplit("-");
String[] arg2 = endTimesplit("-");
int year1 = IntegervalueOf(arg1[0]);
int year2 = IntegervalueOf(arg2[0]);
int month1 = IntegervalueOf(arg1[1]);
int month2 = IntegervalueOf(arg2[1]);
for (int i = year1; i <= year2; i++) {
int monthCount = 12;
int monthStart = 1;
if (i == year1) {
monthStart = month1;
monthCount = 12-monthStart+1;
} else if (i == year2) {
monthCount = month2;
}
for(int j = 0; j < monthCount; j++){
int temp = monthStart+j;
if(temp >=10){
Systemoutprintln(i+"-"+(monthStart+j));
}else{
Systemoutprintln(i+"-0"+(monthStart+j));
}
}
}
}
}
2012-07
2012-08
2012-09
2012-10
2012-11
2012-12
2013-01
2013-02
2013-03
2013-04
2013-05
2013-06
2013-07
2013-08
2013-09
2013-10
2013-11
2013-12
2014-01
2014-02
2014-03
2014-04
2014-05
2014-06
2014-07
Java创建一个日历对象,需要引入javautil包,用当前时间初始化日历时间,计算两个日期之间相隔的天数,实例演示了2014年10月1日和1949年10月1日中间相隔的天数,计算方法如下:
import javautil;public class CalendarDemo {
public static void main(String args[]) {
Calendar calendar = CalendargetInstance(); //创建一个日历对象。
calendarsetTime(new Date()); //用当前时间初始化日历时间。
String year = StringvalueOf(calendarget(CalendarYEAR));
String month = StringvalueOf(calendarget(CalendarMONTH) + 1);
String date = StringvalueOf(calendarget(CalendarDAY_OF_MONTH));
String day = StringvalueOf(calendarget(CalendarDAY_OF_WEEK) - 1);
int hour = calendarget(CalendarHOUR_OF_DAY);
int minute = calendarget(CalendarMINUTE);
int second = calendarget(CalendarSECOND);
Systemoutprintln("现在的时间是:");
Systemoutprintln("" + year + "年" + month + "月" + date + "日 " + "星期" + day);
Systemoutprintln("" + hour + "时" + minute + "分" + second + "秒");
calendarset(1949, 9, 1); //将日历翻到1949年10月1日,注意9表示十月。
// 返回当前时间,作为从开始时间的 UTC 毫秒值。
long time1949 = calendargetTimeInMillis();
calendarset(2014, 9, 1); //将日历翻到2014年10月1日。9表示十月。
// 返回当前时间,作为从开始时间的 UTC 毫秒值。
long time2004 = calendargetTimeInMillis();
long interdays = (time2014 - time1949) / (1000 60 60 24);
Systemoutprintln("2014年10月1日和1949年10月1日相隔" + interdays + "天");
}
}
import javatextParseException;
import javatextSimpleDateFormat;
import javautilDate;
public class Example {
public static strictfp void main(String[] args) throws ParseException {
String string = "2012/10/20";
String string2 = "2013/11/05";
String[] dates = getMonth(string, string2);
for(int i = 0 ; i <dateslength ; i++) {
Systemoutprint(dates[i] + " ");
}
}
@SuppressWarnings("deprecation")
private static String[] getMonth(String string, String string2) throws ParseException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date date1 = sdfparse(string);
Date date2 = sdfparse(string2);
int month = 0;
String[] months;
//假如年份相等
if(date1getYear() == date2getYear()) {
month = date2getMonth() - date1getMonth();
months = new String[month+1];
months[0] = date1getMonth() + 1 + "月" ;
for(int i = 1; i < monthslength; i++) {
months[i] = date1getMonth() + 1 + i + "月";
}
return months;
} else {
//相差的年数乘以12是月数
month = (date2getYear() - date1getYear())12 +date2getMonth() - date1getMonth();
months = new String[month + 1];
months[0] = date1getMonth() + 1 + "月" ;
Systemoutprintln(month);
for(int i = 1; i < monthslength; i++) {
//这里要进行取余 *** 作,,,,,例如 13个月会变成 1月
if((date1getMonth() + 1 + i)%12 == 0) {
months[i] = "12月" ;
continue;
}
months[i] = (date1getMonth() + 1 + i)%12 + "月";
}
return months;
}
}
}
啥也不说了着急要分 代码没重构 string2 必须比 string 要大的 这个你知己排除下拉 大哥求分啦~~
你可以如此计算,
年份差、月份差算出整数部分
小数部分再按剩余日期、时分秒按其在当月的比例计算
如2012-11-24 14:20:00和2012-5-24 14:20:00
年差为0,月差为6,其他差为0,结果为60
若其中出现某差为负数,需调整
如2012-11-24 14:20:00和2012-5-31 14:20:00
年差为0,月差为6,日差为-6,将月差改为5,
即先计算和2012-6-24 14:20:00的差,
2012-6-24 14:20:00到2012-5-31 14:20:00再计算,
可按6月份2012-6-24 14:20:00到2012-6-01 00:00:00 占6月份比例
再计算5月份2012-5-31 24:00:00到2012-5-31 14:20:00占5月份比例
两个比例加起来是小数部分。
以上就是关于java实现两个日期相减得到中间的年份和月份全部的内容,包括:java实现两个日期相减得到中间的年份和月份、java利用Calendar如何计算两个日期只差、java年月相减得出哪几个月份等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)