java計算時間

來源:互聯網
上載者:User
java 擷取時間到毫秒
 
 Calendar CD = Calendar.getInstance();
  
  int YY = CD.get(Calendar.YEAR) ;
  int MM = CD.get(Calendar.MONTH)+1;
  int DD = CD.get(Calendar.DATE);
  int HH = CD.get(Calendar.HOUR_OF_DAY);
  int NN = CD.get(Calendar.MINUTE);
  int SS = CD.get(Calendar.SECOND);
  int MI = CD.get(Calendar.MILLISECOND);
  
  String timeNow = YY + "/" + MM + "/" + DD + " " + HH + "." + NN + "." + SS + "." + MI; 你可以用時間差來計算啊! 
  把代碼塊放在其中就是了. 
      線程的啟動並執行時間也是類似的 
            public  static  void  testRunningTime()  {  
                  long  t1  =  System.currentTimeMillis();  
                    //  invoke  your  program  here  
                    long  t2  =  System.currentTimeMillis();  
                      System.out.println("Your  program  has  executed  for  "  
                        +  (int)((t2-t1)/1000)+  "  seconds  "    
                            +  ((t2-t1)  %  1000)  +  "  micro  seconds");  
                        }  
/**
* 計算指定日期的上一天
* 
* @param dateTime
*            日期,格式為:yyyy-MM-dd
* @return
*/
public static String getBeforeDay(String dateTime) {
   Calendar now = Calendar.getInstance();
   SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
   Date date = null;
  try {
    date = simpledate.parse(dateTime);
   } catch (ParseException ex) {
    System.out.println("日期格式不符合要求:" + ex.getMessage());
   return null;
   }
   now.setTime(date);
  int year = now.get(Calendar.YEAR);
  int month = now.get(Calendar.MONTH);
  int day = now.get(Calendar.DAY_OF_MONTH) - 1;
   now.set(year, month, day);
   String time = simpledate.format(now.getTime());
  return time;
}


/**
* 計算指定日期的下一天
* 
* @param dateTime
*            日期,格式為:yyyy-MM-dd
* @return
*/
public static String getNextDay(String dateTime) {
   Calendar now = Calendar.getInstance();
   SimpleDateFormat simpledate = new SimpleDateFormat("yyyy-MM-dd");
   Date date = null;
  try {
    date = simpledate.parse(dateTime);
   } catch (ParseException ex) {
    System.out.println("日期格式不符合要求:" + ex.getMessage());
   return null;
   }
   now.setTime(date);
  int year = now.get(Calendar.YEAR);
  int month = now.get(Calendar.MONTH);
  int day = now.get(Calendar.DAY_OF_MONTH) + 1;
   now.set(year, month, day);
   String time = simpledate.format(now.getTime());
  return time;
}

/**
* 得到指定月的天數
* @param _year
* @param _month
* @return
*/
public static int getMaxDayOfMonth(int _year, int _month){
   Calendar now = Calendar.getInstance();
  int year = 0;
  int month = 0;
  if(_month==1){
    year = _year - 1;
    month = 12;
   }else{
    year = _year;
    month = _month - 1;
   }
  
   now.set(Calendar.YEAR, year);
   now.set(Calendar.MONTH, month);

  return now.getActualMaximum(Calendar.DATE);
}

/**
* 計算時間差
* 
* @param beginTime
*            開始時間,格式:yyyy-MM-dd HH:mm:ss
* @param endTime
*            結束時間,格式:yyyy-MM-dd HH:mm:ss
* @return 從開始時間到結束時間之間的時間差(秒)
*/
public static long getTimeDifference(String beginTime, String endTime) {
  long between = 0;
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

   Date end = null;
   Date begin = null;
  try {// 將截取到的時間字串轉化為時間格式的字串
    end = sdf.parse(endTime);
    begin = sdf.parse(beginTime);
   } catch (ParseException e) {
    e.printStackTrace();
   }

   between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是為了轉換成秒

  return between;
}

/**
* 計算時間差
* 
* @param time
*            指定的時間,格式為:yyyy-MM-dd HH:mm:ss
* @return 目前時間和指定時間的時間差(秒)
*/
public static long getTimeDifference(String time) {
  long between = 0;
   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
   String systemTime = sdf.format(new Date()).toString();

   Date end = null;
   Date begin = null;
  try {// 將截取到的時間字串轉化為時間格式的字串
    end = sdf.parse(time);
    begin = sdf.parse(systemTime);
   } catch (ParseException e) {
    e.printStackTrace();
   }

   between = Math.abs(end.getTime() - begin.getTime()) / 1000;// 除以1000是為了轉換成秒

  return between;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.