日期:2014-05-20  浏览次数:20738 次

java得到14天前和14天后日期,在线等
java得到14天前和14天后日期,并按下面的格式输出,
yyyy-MM-dd

怎么得到呀,thanks

------解决方案--------------------
转成毫秒 然后加减 然后再格式化
------解决方案--------------------
Java code

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

        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 14);
        System.out.println(sdf.format(cal.getTime()));

        Date d1 = new Date();
        d1.setTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 14);
        System.out.println(sdf.format(d1));

------解决方案--------------------
用joda-time吧,专门为了解决坑爹的java日期问题的
------解决方案--------------------
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 14);
cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 14);
------解决方案--------------------
天数减14
------解决方案--------------------
Java code
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("14天前:"+sdf.format(new Date().getTime()-(1000*60*60*24*14)));
        System.out.println("14天后:"+sdf.format(new Date().getTime()+(1000*60*60*24*14)));

------解决方案--------------------
最好加个L

Java code
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("14天前:"+sdf.format(new Date().getTime()-(1000*60*60*24*14l)));
        System.out.println("14天后:"+sdf.format(new Date().getTime()+(1000*60*60*24*14l)));

------解决方案--------------------
Calendar搞定毫无压力。。。
------解决方案--------------------
力顶9楼,确实没有鸭梨