1.如何获取当前的系统时间,又如何各种精度很高的日期格式?
2.如何判断二个时间参数之间的日期前后?并说明精确的前后差距(如相差5天又一小时23分XX秒XXXms,或刚好相关五天整之类)
3.多个时间参数之间,如何求得其平均值?

解决方案 »

  1.   

    查 java api doc 咯..
      

  2.   

    1.
    Date d=new Date();
    SimpleDateFormat form=new SimpleDateFormat("yyyy:MM:dd ' ' HH:mm:ss ' ' SSS");
    System.out.println(form.format(d));
    输出格式:年:月:日 小时:分钟:秒 毫秒2.long one =date1.getTime();//自1970.1.1 00:00:00起的毫秒数
    lone two = date2.getTime();
    long time=two-one;
    然后自己算,注意这里的都是毫秒值;3.long average= (date1.getTime()+date2.getTime()+...+dateN.getTime())/N;
      Date averDate=new Date(average);
      System.out.println(form.format(averDate));
      

  3.   

    最好用Calendar ,Date的大多数方法已经被Deprecated
    1. Calendar now = Calendar.getInstance();
    //精确到毫秒SSS
    SimpleDateFormat yearAndMonth = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
    String strCurrTime = yearAndMonth.format(now.getTime());
    now = null;
    2.计算差值
    Calendar xmas = new GregorianCalendar(2004, Calendar.DECEMBER, 25);
    Calendar newyears = new GregorianCalendar(2005, Calendar.JANUARY, 1);
    // 相差毫秒数
    long diffMillis = newyears.getTimeInMillis()-xmas.getTimeInMillis();
    // 相差秒数
    long diffSecs = diffMillis/(1000);           
    // 相差分数
    long diffMins = diffMillis/(60*1000);        
    // 相差小时数
    long diffHours = diffMillis/(60*60*1000);    
    // 相差天数
    long diffDays = diffMillis/(24*60*60*1000);  
    3.
    Calendar xmas = new GregorianCalendar(2004, Calendar.DECEMBER, 25);
    Calendar newyears = new GregorianCalendar(2005, Calendar.JANUARY, 1);
    long average= (xmas.getTimeInMillis()+newyears.getTimeInMillis)/2;