자바 간단 한 날짜 계산 기능 구현

5058 단어 자바날짜 계산
본 고 에서 말 한 자바 날 짜 는 계산 이 비교적 편 파 적 이 고 사용 하 는 곳 이 매우 적다(예 를 들 어 오늘 이 있 는 월요일 이나 일요일 을 얻 고 오늘 이 이 달 몇 주 째 인지 얻 는 것).이런 방법 들 은 예전 에 프로젝트 를 하면 서 남 겨 진 것 이다.지금 정리 하고 여러분 과 공유 하 자.
도구 류 는 주로 방법 이 있 습 니 다.
public static Date getFirstMondayOfMonth(String dateString, String dateFormat) throws Exception
2014-12 월 의 첫 번 째 월요일 은 2014-12-01 입 니 다.
public static int figureWeekIndexOfMonth(String dateString, String dateFormat) throws Exception
지 정 된 시간 을 계산 하 는 것 은 달의 몇 주 에 속한다.예 를 들 어 2014-12 월 의 첫 주 는 1 일부 터 7 일 까지 이다.그러면 2014-12-05 는 12 월 의 첫째 주 이 고 2014-12-12 는 둘째 주 이다.
public static String getMondyOfToday(String format)
오늘 의 월요일 을 가 져 오고 시간 문자열 을 되 돌려 줍 니 다.오늘 이 2014-12-8 이 라면 2014-12-08(오늘 이 마침 이번 주 월요일)로 돌아 갑 니 다.
public static Date getSundayOfToday()
오늘 이 있 는 주 일요일 을 가 져 옵 니 다.오늘 이 2014-12-8 이 라면 2014-12-14 로 돌아 갑 니 다.
다음은 도구 류 의 상세 코드 입 니 다.

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
/**
 * @     :DateUtil.java
 * @    :com.nerve.human.common.util
 * @     :
 *        
 * @    :     [email protected]
 * @  :IBM GDC
 * @     :2013-4-9
 * @log :
 */
public class DateUtil {
 
 public static Date toDate(String timeString, String format) throws Exception{
 return new SimpleDateFormat(format).parse(timeString);
 }
 
 /**
 * 
 * @method name: toString
 * @return type: String
 * @param date
 * @param format
 * @return
 */
 public static String toString(Date date, String format){
 String strTime = null;
 try {
 SimpleDateFormat simpledateformat = new SimpleDateFormat(format);
 strTime = simpledateformat.format(date);
 } catch (Exception ex) {
 System.err.println("        : " + ex.getMessage());
 }
 return strTime;
 }
 /**
 *            (     )
 * @method name: getFirstMonday
 * @return type: void
 */
 public static Date getFirstMondayOfMonth(String month) throws Exception{
 return getFirstMondayOfMonth(month, "yyyy-MM");
 }
 
 /**
 *            (     )
 * @method name: getFirstMonday
 * @return type: void
 */
 public static Date getFirstMondayOfMonth(String dateString, String dateFormat) throws Exception{
 Date date = toDate(dateString, dateFormat);
 
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 
 int step = (9 - c.get(Calendar.DAY_OF_WEEK)) % 7;
 c.add(Calendar.DAY_OF_YEAR, step);
 
 return c.getTime();
 }
 
 /**
 *                
 *   2014-12      1  7 
 *   2014-12-05   12     
 * 2014-12-12      
 * 
 * @method name: figureWeekIndexOfMonth 
 * @return type: int
 *
 * @param date
 * @return
 */
 public static int figureWeekIndexOfMonth(String dateString, String dateFormat) throws Exception{
 Calendar c = Calendar.getInstance();
 
 Date curDate = toDate(dateString, dateFormat);
 c.setTime(curDate);
 int day = c.get(Calendar.DAY_OF_MONTH);
 
 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
 Date firstMondy = getFirstMondayOfMonth(sdf.format(c.getTime()));
 c.setTime(firstMondy);
 
 int index = 0;
 do{
 c.add(Calendar.DAY_OF_MONTH, 7);
 index ++;
 }
 while(c.get(Calendar.DAY_OF_MONTH) < day);
 
 return index;
 }
 
 /**
 *            
 * @method name: getMondyOfToday 
 * @return type: String
 *
 * @return
 */
 public static String getMondyOfToday(String format){
 Calendar c = Calendar.getInstance();
 int step = c.get(Calendar.DAY_OF_WEEK);
 //   
 if(step == 1)
 step = 6;
 else
 step -= 2;
 
 c.add(Calendar.DAY_OF_YEAR, -step);
 
 return toString(c.getTime(), format);
 }
 
 /**
 *            
 * @method name: getMondyOfToday 
 * @return type: String
 *
 * @return
 */
 public static Date getSundayOfToday(){
 Calendar c = Calendar.getInstance();
 
 int step = c.get(Calendar.DAY_OF_WEEK);
 if(step != Calendar.SUNDAY)
 c.add(Calendar.DAY_OF_YEAR, 8-step);
 return c.getTime();
 }
 
 /**
 *             
 * @param date
 * @return
 */
 public static Date getSundayOfDate(Date date){
 Calendar c = Calendar.getInstance();
 c.setTime(date);
 
 int step = c.get(Calendar.DAY_OF_WEEK);
 if(step != Calendar.SUNDAY)
 c.add(Calendar.DAY_OF_YEAR, 8-step);
 return c.getTime();
 }
}
테스트 캡 처:

이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기