熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

java裡面一些時間的計算

2013-11-23 18:39:48  來源: Java核心技術 
/**
  
  * 計算指定日期的上一天
  
  *
  
  * @param dateTime
  
  *            日期格式為yyyyMMdd
  
  * @return
  
  */
  
  public static String getBeforeDay(String dateTime) {
  
  Calendar now = CalendargetInstance()
  
  SimpleDateFormat simpledate = new SimpleDateFormat(yyyyMMdd
  
  Date date = null;
  
  try {
  
  date = simpledateparse(dateTime)
  
  } catch (ParseException ex) {
  
  Systemoutprintln(日期格式不符合要求 + exgetMessage())
  
  return null;
  
  }
  
  nowsetTime(date)
  
  int year = nowget(CalendarYEAR)
  
  int month = nowget(CalendarMONTH)
  
  int day = nowget(CalendarDAY_OF_MONTH) ;
  
  nowset(year month day)
  
  String time = simpledateformat(nowgetTime())
  
  return time;
  
  }
  
  /**
  
  * 計算指定日期的下一天
  
  *
  
  * @param dateTime
  
  *            日期格式為yyyyMMdd
  
  * @return
  
  */
  
  public static String getNextDay(String dateTime) {
  
  Calendar now = CalendargetInstance()
  
  SimpleDateFormat simpledate = new SimpleDateFormat(yyyyMMdd
  
  Date date = null;
  
  try {
  
  date = simpledateparse(dateTime)
  
  } catch (ParseException ex) {
  
  Systemoutprintln(日期格式不符合要求 + exgetMessage())
  
  return null;
  
  }
  
  nowsetTime(date)
  
  int year = nowget(CalendarYEAR)
  
  int month = nowget(CalendarMONTH)
  
  int day = nowget(CalendarDAY_OF_MONTH) + ;
  
  nowset(year month day)
  
  String time = simpledateformat(nowgetTime())
  
  return time;
  
  }
  
  /**
  
  * 得到指定月的天數
  
  * @param _year
  
  * @param _month
  
  * @return
  
  */
  
  public static int getMaxDayOfMonth(int _year int _month){
  
  Calendar now = CalendargetInstance()
  
  int year = ;
  
  int month = ;
  
  if(_month==){
  
  year = _year ;
  
  month = ;
  
  }else{
  
  year = _year;
  
  month = _month ;
  
  }
  
  nowset(CalendarYEAR year)
  
  nowset(CalendarMONTH month)
  
  return nowgetActualMaximum(CalendarDATE)
  
  }
  
  /**
  
  * 計算時間差
  
  *
  
  * @param beginTime
  
  *            開始時間格式yyyyMMdd HH:mm:ss
  
  * @param endTime
  
  *            結束時間格式yyyyMMdd HH:mm:ss
  
  * @return 從開始時間到結束時間之間的時間差(秒)
  
  */
  
  public static long getTimeDifference(String beginTime String endTime) {
  
  long between = ;
  
  SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd HH:mm:ss
  
  Date end = null;
  
  Date begin = null;
  
  try {// 將截取到的時間字符串轉化為時間格式的字符串
  
  end = sdfparse(endTime)
  
  begin = sdfparse(beginTime)
  
  } catch (ParseException e) {
  
  eprintStackTrace()
  
  }
  
  between = (endgetTime() begingetTime()) / ;// 除以是為了轉換成秒
  
  return between;
  
  }
  
  /**
  
  * 計算時間差
  
  *
  
  * @param time
  
  *            指定的時間格式為yyyyMMdd HH:mm:ss
  
  * @return 當前時間和指定時間的時間差(秒)
  
  */
  
  public static long getTimeDifference(String time) {
  
  long between = ;
  
  SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMdd HH:mm:ss
  
  String systemTime = sdfformat(new Date())toString()
  
  Date end = null;
  
  Date begin = null;
  
  try {// 將截取到的時間字符串轉化為時間格式的字符串
  
  end = sdfparse(time)
  
  begin = sdfparse(systemTime)
  
  } catch (ParseException e) {
  
  eprintStackTrace()
  
  }
  
  between = Mathabs(endgetTime() begingetTime()) / ;// 除以是為了轉換成秒
  
  return between;
  
  }
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25543.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.