코드 사본은 다음과 같습니다.
import java.text.parseexception;
import java.text.simpledateformat;
java.util.calendar 가져 오기;
import java.util.date;
java.util.GregorianCalendar 가져 오기;
java.util.timezone import;
/**
* 시간 처리 기능
*
* @20080509 15:50
*/
공개 수업 dateUtil {
개인 정적 최종 문자열 default_pattern = "yyyy-mm-dd hh : mm : ss";
공개 정적 최종 문자열 time_year = "yyyy";
공개 정적 최종 문자열 time_monen = "mm";
공개 정적 최종 문자열 time_day = "dd";
public static string getDate (문자열 간격, 날짜 시작 시간, 문자열 패턴) {
Calendar temp = calendar.getInstance (timezone.getDefault ());
temp.settime (starttime);
temp.add (temp.month, integer.parseint (Interval));
simpledateformat sdf = 새로운 simpledateformat (패턴);
return sdf.format (temp.gettime ());
}
/**
* 문자열 유형을 시간 유형으로 변환합니다
*
* @반품
*/
공개 정적 날짜 str2date (String str) {
날짜 d = null;
simpledateformat sdf = new simpledateformat (default_pattern);
노력하다 {
d = sdf.parse (str);
} catch (예외 e) {
e.printstacktrace ();
}
반환 d;
}
공개 정적 날짜 str2date (문자열 str, 문자열 패턴) {
날짜 d = null;
simpledateformat sdf = 새로운 simpledateformat (패턴);
노력하다 {
d = sdf.parse (str);
} catch (예외 e) {
e.printstacktrace ();
}
반환 d;
}
/**
* 시간을 형식화하십시오
*
* @반품
*/
공개 정적 날짜 날짜 패턴 (날짜 날짜) {
simpledateformat sdf = new simpledateformat (default_pattern);
노력하다 {
문자열 dd = sdf.format (날짜);
날짜 = str2date (dd);
} catch (예외 e) {
e.printstacktrace ();
}
반환 날짜;
}
/**
* 시간을 형식화하십시오
*/
공개 정적 날짜 날짜 패턴 (날짜 날짜, 문자열 패턴) {
simpledateformat sdf = 새로운 simpledateformat (패턴);
노력하다 {
문자열 dd = sdf.format (날짜);
날짜 = str2date (dd, 패턴);
} catch (예외 e) {
e.printstacktrace ();
}
반환 날짜;
}
공개 정적 문자열 date2str (날짜 날짜) {
simpledateformat sdf = new simpledateformat (default_pattern);
반환 sdf.format (날짜);
}
공개 정적 문자열 date2str (날짜 날짜, 문자열 형식) {
simpledateformat sdf = 새로운 simpledateformat (형식);
반환 sdf.format (날짜);
}
/**
* 어제 가십시오
*
* @param 날짜
* @반품
* @throws 예외
*/
공개 정적 날짜 getLastDate (날짜 날짜) {
Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
calendar.settime (날짜);
calendar.add (calendar.date, -1);
return str2date (date2str (calendar.gettime ()));
}
/**
* 며칠 전에 받으십시오
* @param 날짜
* @반품
*/
공개 정적 날짜 getBeforedate (날짜, int 날짜) {
Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
calendar.settime (날짜);
calendar.add (calendar.date, -dates);
return str2date (date2str (calendar.gettime ()));
}
/**
* 지난주 첫날 (월요일)
*
* @param 날짜
* @반품
* @throws 예외
*/
공개 정적 날짜 getLastWeekStart (날짜 날짜) {
Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
calendar.settime (날짜);
int i = calendar.get (calendar.day_of_week) -1;
int startnum = 0;
if (i == 0) {
startnum = 7 + 6;
} 또 다른 {
startnum = 7 + i -1;
}
calendar.add (calendar.date, -startnum);
return str2date (date2str (calendar.gettime ()));
}
/**
* 지난 주 마지막 날 (주말)
*
* @param 날짜
* @반품
* @throws 예외
*/
공개 정적 날짜 getLastweekend (날짜 날짜) {
Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
calendar.settime (날짜);
int i = calendar.get (calendar.day_of_week) -1;
int endnum = 0;
if (i == 0) {
endnum = 7;
} 또 다른 {
endnum = i;
}
calendar.add (calendar.date, - (endnum -1));
return str2date (date2str (calendar.gettime ()));
}
/**
* 연도와 월에 기준으로 일을하십시오
* @Param Num Month
* @param 년
* @반품
*/
공개 정적 int getday (int num, int 년) {
if (num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12) {
반환 31;
} else if (num == 2) {
// 도약 연도 여부를 판단합니다
if (년%400 == 0 || (년%4 == 0 && 년%100! = 0)) {
반환 29;
}또 다른{
반환 28;
}
}또 다른{
반환 30;
}
}
/*
* 다음 달 현재 날짜에 며칠이 있는지 계산하십시오.
*/
공개 정적 int getDaymis (날짜 시간) {
int 년 = integer.parseint (
new simpledateformat (time_year) .format (time)); // 년
int mm = integer.parseint (
새로운 simpledateformat (time_monen) .format (time)); // 월
int dd = integer.parseint (
새로운 simpledateformat (time_day) .format (time)); // day
// 현재 연도 및 월에 총 일수를받습니다.
int sdd = getday (mm, 년);
SDD-DD를 반환합니다.
}
/**
* 날짜 수
* @param datestring
* @반품
*/
공개 정적 긴 gettime (String datestring) {
오랜 시간 = 0;
노력하다 {
날짜 ret = null;
simpledateformat sdf = new simpledateformat ( "yyyy-mm-dd hh : mm : ss");
ret = sdf.parse (datestring);
time = ret.gettime ()/1000;
} catch (예외 e) {
}
반환 시간;
}
/**
* 시간 차이를 정확하게 계산하고 하루에 정확합니다.
* @Param Fistill 시작 날짜
* @param nowtime 종료 날짜
* @param 유형 유형은 1이고 연도, 월 및 일 (예 : 2 년, 3 개월 및 5 일)이 1 년, 그렇지 않으면 총 일수를 반환합니다.
* @반품
*/
public static string patienage (날짜 주먹, 날짜 nowtime, 정수 유형) {
int fight = integer.parseint (
새로운 simpledateformat (time_year) .format (Fistill)); // 첫해
int fmm = integer.parseint (
새로운 simpledateformat (time_monen) .format (Fistill)); // 첫 달
int fdd = integer.parseint (
새로운 simpledateformat (time_day) .format (Fistill)); // 첫 데이트
int nyear = integer.parseint (
새로운 simpledateformat (time_year) .format (nowtime)); // 종료 연도
int nmm = integer.parseint (
새로운 simpledateformat (time_monen) .format (nowtime)); // 종료 월
int ndd = integer.parseint (
new simpledateformat (time_day) .format (nowtime)); // 종료 날짜
int 사이버 = nyear -fyear;
int cmmm = nmm -fmm;
int cdd = ndd -fdd;
int zyear = 0;
int zmm = 0;
int zdd = 0;
int countddd = 0; // 연도, 월 및 일의 누적 일수
if (cdd <0) {
if (cm <0) {
zyear = cyear -1;
ZMM = (CMM + 12) -1;
int dd = getday (zmm, nyear-1);
ZDD = DD + CDD;
countddd = Zyear*365+zmm*30+zdd;
} else if (cmm == 0) {
zyear = cyear -1;
ZMM = 12-1;
int dd = getday (zmm, nyear-1);
ZDD = DD + CDD;
countddd = Zyear*365+zmm*30+zdd;
}또 다른{
zyear = cinear;
zmm = cmmm -1;
int dd = getday (zmm, nyear);
ZDD = DD + CDD;
countddd = Zyear*365+zmm*30+zdd;
}
} else if (cdd == 0) {
if (cm <0) {
zyear = cyear -1;
zmm = cmmm + 12;
ZDD = 0;
countddd = Zyear*365+zmm*30;
} else if (cmm == 0) {
zyear = cinear;
zmm = 0;
ZDD = 0;
countddd = Zyear*365+zmm*30;
}또 다른{
zyear = cinear;
zmm = cmmm;
ZDD = 0;
countddd = Zyear*365+zmm*30;
}
}또 다른{
if (cm <0) {
zyear = cyear -1;
zmm = cmmm + 12;
ZDD = CDD;
countddd = Zyear*365+zmm*30+zdd;
} else if (cmm == 0) {
zyear = cinear;
zmm = 0;
ZDD = CDD;
countddd = Zyear*365+zmm*30+zdd;
}또 다른{
zyear = cinear;
zmm = cmmm;
ZDD = CDD;
countddd = Zyear*365+zmm*30+zdd;
}
}
문자열 ptime = null;
if (zdd! = 0) {
if (zmm! = 0) {
if (Zyear! = 0) {
ptime = zyear+"year"+zmm+"month"+"Zero"+zdd+"day";
}또 다른{
ptime = zmm+"month"+"zero"+zdd+"day";
}
}또 다른{
if (Zyear! = 0) {
ptime = zyear+"year"+"zero"+zdd+"day";
}또 다른{
ptime = zdd+"day";
}
}
}또 다른{
if (zmm! = 0) {
if (Zyear! = 0) {
ptime = zyear+"year"+zmm+"month";
}또 다른{
ptime = zmm+"Month";
}
}또 다른{
if (Zyear! = 0) {
ptime = Zyear+"Year";
}또 다른{
ptime = null;
}
}
}
if (type == 1) {
반품 Ptime; /연중, 월 및 일로 돌아 가기 (예 : 2 년, 3 개월 및 5 일)
}또 다른{
return string.valueof (countddd);
}
}
/**
* 달 수를 얻으십시오
* @param 년도 차이
* @param의 달 차이
* @반품
*/
공개 정적 int getcmm (정수 연도, 정수의 달) {
int zmm = 0;
if (Month <0) {
zmm = (Month + 12) + (년 -1)*12;
} else if (month == 0) {
zmm = 년*12;
}또 다른{
zmm = 년*12+월;
}
반환 zmm;
}
/**
* 현재 시간을 변경하십시오
*/
공개 정적 날짜 변경 사항 (문자열 유형, int 값) {
Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
if (type.equals ( "Month")) {
calendar.set (calendar.month, calendar.get (calendar.month) + value);
} else if (type.equals ( "date")) {
calendar.set (calendar.date, calendar.get (calendar.date) + value);
}
return calendar.gettime ();
}
/**
* 시간을 변경하십시오
*/
공개 정적 날짜 변경 사항 (날짜 날짜, 문자열 유형, int 값) {
if (date! = null) {
// Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
캘린더 캘린더 = New GregorianCalendar ();
calendar.settime (날짜);
// 캘린더 캘린더 = 캘린더.
if (type.equals ( "Month")) {
calendar.set (calendar.month, calendar.get (calendar.month) + value);
} else if (type.equals ( "date")) {
calendar.set (calendar.date, calendar.get (calendar.date) + value);
} else if (type.endswith ( "Year")) {
calendar.set (calendar.year, calendar.get (calendar.year) + value);
}
return calendar.gettime ();
}
널 리턴;
}
/**
* haoxw 시간 이이 두 시점 사이인지 비교
*
* @param time1
* @param time2
* @반품
*/
public static boolean checktime (String Time1, String Time2) {
캘린더 캘린더 = calendar.getInstance ();
날짜 1 = calendar.gettime ();
날짜 date11 = dateUtil.str2Date (dateUtil.date2str (date1, "yyyy-mm-dd") + "" + time1); // 시작 시간
Calendar C = Calendar.getInstance ();
c.add (calendar.date, 1);
날짜 2 = c.gettime ();
날짜 날짜 22 = dateUtil.str2Date (dateUtil.date2str (date2, "yyyy-mm-dd") + "" + time2); // 종료 시간
Calendar scalendar = calendar.getInstance ();
scalendar.settime (date11); // 시작 시간
Calendar elendar = calendar.getInstance ();
elendar.settime (date22); // 종료 시간
Calendar Calendarnow = Calendar.getInstance ();
if (calendarnow.after (scalendar) && calendarnow.before (평등)) {
진실을 반환하십시오.
} 또 다른 {
거짓을 반환합니다.
}
}
/**
* haoxw 시간 이이 두 시점 사이인지 비교
*
* @param date11
* @param 날짜 22
* @반품
*/
공개 정적 부울 점검 시간 (날짜 날짜 11, 날짜 22) {
Calendar scalendar = calendar.getInstance ();
scalendar.settime (date11); // 시작 시간
Calendar elendar = calendar.getInstance ();
elendar.settime (date22); // 종료 시간
Calendar Calendarnow = Calendar.getInstance ();
if (calendarnow.after (scalendar) && calendarnow.before (평등)) {
진실을 반환하십시오.
} 또 다른 {
거짓을 반환합니다.
}
}
public static boolean checkdate (문자열 날짜 1, 문자열 날짜 2) {
날짜 date11 = dateutil.str2date (date1, "yyyy-mm-dd hh : mm : ss"); // 시작 시간
날짜 날짜 22 = dateUtil.str2Date (date2, "yyyy-mm-dd hh : mm : ss"); // 종료 시간
Calendar scalendar = calendar.getInstance ();
scalendar.settime (date11); // 시작 시간
Calendar elendar = calendar.getInstance ();
elendar.settime (date22); // 종료 시간
Calendar Calendarnow = Calendar.getInstance ();
System.out.println (date11.toString ());
System.out.println (date22.toString ());
System.out.println (scalendar.toString ());
System.out.println (elendar.toString ());
System.out.println (Calendarnow.toString ());
if (calendarnow.after (scalendar) && calendarnow.before (평등)) {
진실을 반환하십시오.
} 또 다른 {
거짓을 반환합니다.
}
}
/**
* 간격 일 전 날짜를 얻으십시오
*
* @param 간격
* @param starttime
* @param 패턴
* @반품
*/
공개 정적 날짜 getInterValdate (문자열 간격, 날짜 시작 시간, 문자열 패턴) {
Calendar Temp = Calendar.getInstance ();
temp.settime (starttime);
temp.add (temp.date, integer.parseint (Interval));
simpledateformat sdf = 새로운 simpledateformat (패턴);
문자열 shijian = sdf.format (temp.gettime ());
Return str2date (Shijian);
}
공용 정적 날짜 형식 (날짜 날짜) {
simpledateformat bartdateformat =
새로운 simpledateformat ( "yyyy-mm-dd");
System.out.println (bartdateformat.format (date));
simpledateformat bartdateformat1 = new simpledateformat ( "yyyy-mm-dd");
노력하다 {
날짜 1 = bartdateformat1.parse (bartdateformat.format (date));
} catch (parseexception e) {
e.printstacktrace ();
}
System.out.println (date.gettime ());
반환 날짜;
}
public static void main (String arf []) {
/*문자열 time1 = "2009-05-07 19:20:00";
문자열 time2 = "2009-05-08 19:30:00";
dateUtil d = 새로운 dateUtil ();
System.out.println (D.CheckDate (Time1, Time2));
system.out.println (d.date2str (new date ());*/
//system.out.println(d.getIntervaldate("-3 ", new Date (), default_pattern);
Calendar Calendar = Calendar.getInstance (timezone.getDefault ());
System.out.println (Calendar.toString ());
System.out.println (dateUtil.str2date ( "20090731", "yyyymmdd"));
System.out.println (dateUtil.getBeforedate (new date (), 2));
System.out.println (dateUtil.DatePattern (new Date ());
simpledateformat bartdateformat =
새로운 simpledateformat ( "yyyy-mm-dd");
날짜 = 새 날짜 ();
System.out.println ( "날짜;"+bartdateformat.format (date));
simpledateformat bartdateformat1 = new simpledateformat ( "yyyy-mm-dd");
노력하다 {
날짜 1 = bartdateformat1.parse (bartdateformat.format (date));
System.out.println ( "날짜 :"+date1);
} catch (parseexception e) {
e.printstacktrace ();
}
}
}