java得到14天前和14天后日期,并按下面的格式输出,
yyyy-MM-dd怎么得到呀,thanks

解决方案 »

  1.   

    date 单独设置某天 某月  cals.getTime()
    SimpleDateFormat 按格式打印  
    calendar  加减日期 long now=System.currentTimeMillis();
    System.out.println(now);
        Date date=new Date();
        date.setDate(1);
        System.out.println(date.getYear()+1900);
        System.out.println(date.getMonth()+1);
        System.out.println(date.getDate());
        System.out.println(date.getDay());
        Calendar  cal=new GregorianCalendar();
        System.out.println(cal.get(Calendar.YEAR));
        Calendar cals = Calendar.getInstance();//获得当前时间
        cals.add(Calendar.DAY_OF_YEAR, -2);
        DateFormat fmt = 
            new SimpleDateFormat("yyyy-MM-dd");
          String str = fmt.format(cals.getTime());
          System.out.println(str); 
    }
      

  2.   


    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));
      

  3.   

    joda-time吧,专门为了解决坑爹的java日期问题的
      

  4.   

    Calendar cal = Calendar.getInstance();
    cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 14);
    cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 14);
      

  5.   

    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)));
      

  6.   

    最好加个LSimpleDateFormat 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)));