자바 간단 한 날짜 계산 기능 구현
도구 류 는 주로 방법 이 있 습 니 다.
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();
}
}
테스트 캡 처:이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.