百韵网 >>  正文

菜鸟求教,用java的Canlendar类实现计算从键盘输入的两个日期相隔的天数

来源:www.baiyundou.net   日期:较早时间

主要用到Canendar的before、compareTo、add方法,代码如下:

@Test
public void test() throws ParseException {

    SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

    String str1 = "2010-5-27";
    Calendar c1 = Calendar.getInstance();
    Date date1 = sdf.parse(str1);
    c1.setTime(date1);

    String str2 = "2010-6-27";
    Calendar c2 = Calendar.getInstance();
    Date date2 = sdf.parse(str2);
    c2.setTime(date2);

    int diff = 0;
    if (c1.before(c2)) {
        while (true) {
            diff++;
            c1.add(Calendar.DAY_OF_MONTH, 1);
            if (c1.compareTo(c2) == 0) {
                System.out.println("相隔:"+ diff + "天");
                return;
            }
        }
    } else if (c2.before(c1)){
        while (true) {
            diff++;
            c2.add(Calendar.DAY_OF_MONTH, 1);
            if (c1.compareTo(c2) == 0) {
                System.out.println("相隔:"+ diff + "天");
                return;
            }
        }
    } else {
        System.out.println("相隔:"+ diff + "天");
    }
}

输出结果:

相隔:31天



java中如何用calendar类计算2个时间中间差多少天~

Calendar类计算两个日期的差,需要转化成毫秒计算,然后再转化成天
例:
import java.util.Calendar;public class Test{public static void main(String[] args){Calendar a = Calendar.getInstance(), b = Calendar.getInstance();a.set(2015, Calendar.MARCH, 31);b.set(2015, Calendar.APRIL, 1);long diffDays = (b.getTimeInMillis() - a.getTimeInMillis()) / (1000 * 60 * 60 * 24);System.out.println(diffDays);}}

Java创建一个日历对象,需要引入java.util.*包,用当前时间初始化日历时间,计算两个日期之间相隔的天数,实例演示了2014年10月1日和1949年10月1日中间相隔的天数,计算方法如下:
import java.util.*;public class CalendarDemo {public static void main(String args[]) {Calendar calendar = Calendar.getInstance(); //创建一个日历对象。calendar.setTime(new Date()); //用当前时间初始化日历时间。String year = String.valueOf(calendar.get(Calendar.YEAR));String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);String date = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));String day = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK) - 1);int hour = calendar.get(Calendar.HOUR_OF_DAY); int minute = calendar.get(Calendar.MINUTE); int second = calendar.get(Calendar.SECOND);System.out.println("现在的时间是:");System.out.println("" + year + "年" + month + "月" + date + "日 " + "星期" + day);System.out.println("" + hour + "时" + minute + "分" + second + "秒");calendar.set(1949, 9, 1); //将日历翻到1949年10月1日,注意9表示十月。 // 返回当前时间,作为从开始时间的 UTC 毫秒值。long time1949 = calendar.getTimeInMillis();calendar.set(2014, 9, 1); //将日历翻到2014年10月1日。9表示十月。 // 返回当前时间,作为从开始时间的 UTC 毫秒值。long time2004 = calendar.getTimeInMillis();long interdays = (time2014 - time1949) / (1000 * 60 * 60 * 24);System.out.println("2014年10月1日和1949年10月1日相隔" + interdays + "天");}}

相关要点总结:

17554266933:帮忙做个C语言程序,谢谢!再麻烦写完整点,我是个菜鸟!!!谢谢谢谢...
颛师答:static signed short len=5;void printMenu(){ cout<<"---"<<endl;cout<<"Press 1 -->Add a new player information. "<<endl;cout<<"Press 2 -->Delete a player Info. by ID."<<endl;cout<<"Press 3 -->Update a player Info. by ID."<<endl;cout<<"Press 4 -->Display ...

17554266933:关于SQL数据库存储过程的问题
颛师答:T_Line是一个什么结构的表呀 你这样调用一下试试:EXECUTE p_qry 'aa','bb'GO

17554266933:[200分][有追加200分]如何使html页不被调用?
颛师答:这样没有任何资源的网站利用了别的网站的资源来展示给浏览者,提高了自己的访问量,而大部分浏览者又不会很容易地发现,这样显然,对于那个被利用了资源的网站是不公平的。一些不良网站为了不增加成本而扩充自己站点内容,经常盗用其他网站的链接。一方面损害了原网站的合法利益,另一方面又加重了服务器的...

17554266933:请假一个java问题 请高手指点
颛师答:大学时上JAVA课基本上都打游戏去了,这里是C里面数组指针方法,希望能给你提供一个思路 当然其中的a = "Hello World"int len = strlen(a);int i;for (i = 0; i < len / 2; i++) { a[len] = a[i];a[i] = a[len - i - 1];a[len - i - 1] = a[len];} ...

(编辑:本站网友)
相关推荐
关于我们 | 客户服务 | 服务条款 | 联系我们 | 免责声明 | 网站地图
@ 百韵网