コードコピーは次のとおりです。
java.text.parseexceptionをインポートします。
java.text.simpledateformatをインポートします。
java.util.calendarをインポートします。
Import Java.util.date;
java.util.gregoriancalendarをインポートします。
Java.util.timezoneをインポートします。
/**
*時間処理機能
*
* @20080509 15:50
*/
public class dateutil {
private static final string default_pattern = "yyyy-mm-dd hh:mm:ss";
public static final string time_year = "yyyy";
public static final string time_monen = "mm";
public static final string time_day = "dd";
public static string getDate(文字列間隔、日付開始時刻、文字列パターン){
カレンダーTemp = calendar.getInstance(timezone.getDefault());
temp.settime(starttime);
Temp.Add(Temp.month、integer.parseint(間隔));
SimpleDateFormat sdf = new SimpledateFormat(パターン);
return sdf.format(temp.gettime());
}
/**
*文字列タイプを時間型に変換します
*
* @戻る
*/
public static date str2date(string str){
日付d = null;
SimpleDateFormat SDF = new SimpledateFormat(default_pattern);
試す {
d = sdf.parse(str);
} catch(例外e){
e.printstacktrace();
}
d;
}
public static date str2date(string str、string pattern){
日付d = null;
SimpleDateFormat sdf = new SimpledateFormat(パターン);
試す {
d = sdf.parse(str);
} catch(例外e){
e.printstacktrace();
}
d;
}
/**
*時間をフォーマットします
*
* @戻る
*/
public static date datepattern(日付){
SimpleDateFormat SDF = new SimpledateFormat(default_pattern);
試す {
文字列dd = sdf.format(date);
日付= str2date(dd);
} catch(例外e){
e.printstacktrace();
}
返品日;
}
/**
*時間をフォーマットします
*/
public static date datepattern(日付、文字列パターン){
SimpleDateFormat sdf = new SimpledateFormat(パターン);
試す {
文字列dd = sdf.format(date);
date = str2date(dd、pattern);
} catch(例外e){
e.printstacktrace();
}
返品日;
}
public static string date2str(日付){
SimpleDateFormat SDF = new SimpledateFormat(default_pattern);
sdf.format(日付)を返します。
}
public static string date2str(日付、文字列形式){
SimpleDateFormat sdf = new SimpledateFormat(format);
sdf.format(日付)を返します。
}
/**
*昨日入手してください
*
* @param Date
* @戻る
* @Throws例外
*/
public static date getLastDate(日付){
カレンダーカレンダー= calendary.getInstance(timezone.getDefault());
calendar.settime(date);
calendar.add(calendar.date、-1);
return str2date(date2str(calendar.gettime()));
}
/**
*数日前に入手してください
* @param Date
* @戻る
*/
public static date getBeforedate(日付、int日付){
カレンダーカレンダー= calendary.getInstance(timezone.getDefault());
calendar.settime(date);
calendar.add(calendar.date、-dates);
return str2date(date2str(calendar.gettime()));
}
/**
*先週の初日(月曜日)を取得する
*
* @param Date
* @戻る
* @Throws例外
*/
public static date getLastWeekStart(日付){
カレンダーカレンダー= calendary.getInstance(timezone.getDefault());
calendar.settime(date);
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 Date
* @戻る
* @Throws例外
*/
public static date getLastWeekend(日付){
カレンダーカレンダー= calendary.getInstance(timezone.getDefault());
calendar.settime(date);
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年
* @戻る
*/
public static int getday(int num、int year){
if(num == 1 || num == 3 || num == 5 || num == 7 || num == 8 || num == 10 || num == 12){
返品31;
} else if(num == 2){
//跳躍年かどうかを判断します
if(year%400 == 0 ||(年%4 == 0 && year%100!= 0)){
29を返します。
}それ以外{
28を返します。
}
}それ以外{
30を返します。
}
}
/*
*来月の現在の日付に何日かを計算します
*/
public static int getDaymis(日付時刻){
int year = integer.parseint(
new simpledateFormat(time_year).format(time)); // year
int mm = integer.parseint(
new simpledateFormat(time_monen).format(time)); // month
int dd = integer.parseint(
new simpledateFormat(time_day).format(time)); // day
//今年と月の総日数を取得します
int sdd = getDay(mm、year);
sdd-ddを返します。
}
/**
*秒数
* @param DateString
* @戻る
*/
public static long 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 End Date
* @paramタイプタイプは1で、年、月、日(2年、3か月、5日など)を返します。そうしないと、日数を返します
* @戻る
*/
public static string patienage(fistill、date nowtime、integer type){
int fight = integer.parseint(
new simpledateFormat(time_year).format(fistill)); //初年度
int fmm = integer.parseint(
new simpledateFormat(time_monen).format(fistill)); //最初の月
int fdd = integer.parseint(
new simpledateFormat(time_day).format(fistill)); //最初の日付
int nyear = integer.parseint(
new simpledateFormat(time_year).format(nowtime)); //終了年
int nmm = integer.parseint(
new simpledateFormat(time_monen).format(nowtime)); // end month
int ndd = integer.parseint(
new simpledateFormat(time_day).format(nowtime)); //終了日
int cyber = nyear -fyear;
int cmmm = nmm -fmm;
int cdd = ndd -fdd;
int zyear = 0;
int zmm = 0;
int zdd = 0;
int countdddd = 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を返す; /月、1日に戻る(2年、3か月、5日など)
}それ以外{
return string.valueof(countddd); //総日数を返します
}
}
/**
*月数を取得します
* @param年の違い
* @param Monthの違い
* @戻る
*/
public static int getcmm(整数年、整数月){
int zmm = 0;
if(月<0){
zmm =(月 + 12) +(year-1)*12;
} else if(month == 0){
zmm = year*12;
}それ以外{
zmm = year*12+月;
}
zmmを返します。
}
/**
*現在の時間を変更します
*/
public static date chanderate(文字列タイプ、int値){
カレンダーカレンダー= calendary.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();
}
/**
*時間を変更します
*/
public static date chanderate(日付、文字列タイプ、int値){
if(date!= null){
//カレンダーカレンダー= calendar.getInstance(timezone.getDefault());
カレンダーカレンダー= new GregorianCalendar();
calendar.settime(date);
//カレンダーカレンダー=カレンダー。
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();
}
nullを返します。
}
/**
* haoxwこれらの2つの時点の間の時間があるかどうかを比較します
*
* @param time1
* @param time2
* @戻る
*/
public static boolean checktime(string time1、string time2){
カレンダーカレンダー= calendar.getInstance();
日付date1 = calendar.getTime();
日付date11 = dateutil.str2date(dateutil.date2str(date1、 "yyyy-mm-dd") + "" + time1); // start time
カレンダーc = charend.getInstance();
c.add(calendar.date、1);
日付date2 = c.getTime();
date date22 = dateutil.str2date(dateutil.date2str(date2、 "yyyy-mm-dd") + "" + time2); //終端時間
カレンダーscalendar = charend.getInstance();
scalendar.settime(date11); //開始時間
カレンダーelendar = calendar.getInstance();
Elendar.settime(date22); //終了時間
カレンダーカレンダーnow = charend.getInstance();
if(calendarynow.after(scalendar)&& calendarynow.before(equalendar)){
trueを返します。
} それ以外 {
falseを返します。
}
}
/**
* haoxwこれらの2つの時点の間の時間があるかどうかを比較します
*
* @param date11
* @param date22
* @戻る
*/
public static boolean checktime(date date11、date date22){
カレンダーscalendar = charend.getInstance();
scalendar.settime(date11); //開始時間
カレンダーelendar = calendar.getInstance();
Elendar.settime(date22); //終了時間
カレンダーカレンダーnow = charend.getInstance();
if(calendarynow.after(scalendar)&& calendarynow.before(equalendar)){
trueを返します。
} それ以外 {
falseを返します。
}
}
public static boolean checkdate(string date1、string date2){
日付date11 = dateutil.str2date(date1、 "yyyy-mm-dd hh:mm:ss"); // start time
date date22 = dateutil.str2date(date2、 "yyyy-mm-dd hh:mm:ss"); //終端時間
カレンダーscalendar = charend.getInstance();
scalendar.settime(date11); //開始時間
カレンダーelendar = calendar.getInstance();
Elendar.settime(date22); //終了時間
カレンダーカレンダーnow = charend.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(calendarynow.after(scalendar)&& calendarynow.before(equalendar)){
trueを返します。
} それ以外 {
falseを返します。
}
}
/**
*日間隔の前に日付を取得します
*
* @paramインターバル
* @paramの起動時
* @paramパターン
* @戻る
*/
public static date getInterValdate(文字列間隔、日付開始時刻、文字列パターン){
カレンダーtemp = calendar.getInstance();
temp.settime(starttime);
Temp.Add(temp.date、integer.parseint(間隔));
SimpleDateFormat sdf = new SimpledateFormat(パターン);
string shijian = sdf.format(temp.gettime());
return str2date(shijian);
}
public static date formatdate(日付){
SimpleDateFormat bartdateformat =
new simpledateFormat( "yyyy-mm-dd");
system.out.println(bartdateformat.format(date));
SimpleDateFormat bartdateFormat1 = new SimpledateFormat( "yyyy-mm-dd");
試す {
日付date1 = bartdateformat1.parse(bartdateformat.format(date));
} catch(parseexception e){
e.printstacktrace();
}
System.out.println(date.getTime());
返品日;
}
public static void main(string arf []){
/*string time1 = "2009-05-07 19:20:00";
string time2 = "2009-05-08 19:30:00";
dateutil d = new 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));
カレンダーカレンダー= calendary.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 =
new simpledateFormat( "yyyy-mm-dd");
日付date = new date();
system.out.println( "date;"+bartdateformat.format(date));
SimpleDateFormat bartdateFormat1 = new SimpledateFormat( "yyyy-mm-dd");
試す {
日付date1 = bartdateformat1.parse(bartdateformat.format(date));
System.out.println( "date:"+date1);
} catch(parseexception e){
e.printstacktrace();
}
}
}