📄 dateutil.java
字号:
Date dateAddHour = new Date(n);
return dateAddHour;
}
/**
* 根据年份和周�?获取周数的开始日�?(以当年第�?���?天的�?为第�?���?)
*
* @param year
* 年份
* @param weekCount
* 周数
* @return
*/
public static String getStartByAllWeek(String year, int weekCount) {
Calendar cal = Calendar.getInstance();
cal.set(Integer.parseInt(year), 0, 1);
int week = cal.get(Calendar.DAY_OF_WEEK);
cal.add(Calendar.DAY_OF_YEAR, (9 - week) + (weekCount - 1) * 7);
return new String(cal.get(Calendar.YEAR) + "-"
+ (cal.get(Calendar.MONTH) + 1) + "-"
+ cal.get(Calendar.DAY_OF_MONTH));
}
/**
* 根据年份和周�?获取周数的开始日�?(以当年第一天所在的�?为第�?���? *
* @param year
* 年份
* @param weekCount
* 周数
* @return
*/
public static String getStartByFirstDay(String year, int weekCount) {
return getStartByAllWeek(year, weekCount - 1);
}
/**
* 根据传入的年数获取当前年的前后年的下拉列�? *
* @param yearCount
* 显示年数
* @return
*/
public static String[] getYearlist(int yearCount) {
return getYearlist(yearCount, 0);
}
/**
* 根据传入的年数获取当前年的前后年的下拉列�? *
* @param yearCount
* 显示年数
* @param addCount
* 显示选择的年份是当前年的前后addCount年,如-1为前�?���?为后�?���?为当前年
* @return
*/
public static String[] getYearlist(int yearCount, int addCount) {
String yearList[] = new String[yearCount];
java.util.Date date = new Date();
String select = "";
int year = DateUtil.getYear(date) - yearCount / 2;
for (int i = 0; i < yearCount; i++) {
if (i == yearCount / 2 + addCount)
select = "selected";
yearList[i] = "<option " + select + ">" + year + "</option>";
// System.out.println(yearList[i]);
select = "";
year++;
}
return yearList;
}
public static int getWeekName(java.util.Date date) {
Calendar c = Calendar.getInstance();
c.setTime(date);
int i = c.get(Calendar.DAY_OF_WEEK);
if (i == 1)
return 7;
if (i == 2)
return 1;
if (i == 3)
return 2;
if (i == 4)
return 3;
if (i == 5)
return 4;
if (i == 6)
return 5;
if (i == 7)
return 6;
return 0;
}
/**
* 获取星期�? *
* @return
*/
public static int getWeekName() {
return getWeekName(new java.util.Date());
}
/**
* 根据年月日获取星�? *
* @param year
* @param month
* @return
*/
public static int getDayByYearMonthDay(int year, int month, int day) {
int retVal = 0;
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.set(java.util.Calendar.YEAR, year);
calendar.set(java.util.Calendar.MONTH, month - 1);
calendar.set(java.util.Calendar.DATE, day);
// java.text.DateFormat format=new
// java.text.SimpleDateFormat("yyyy-MM-dd HH:MM:ss");
// System.out.println(format.format(calendar.getTime()));
int dayOfWeek = calendar.get(java.util.Calendar.DAY_OF_WEEK);
switch (dayOfWeek) {
case 1:
retVal = 7;
break;
case 2:
retVal = 1;
break;
case 3:
retVal = 2;
break;
case 4:
retVal = 3;
break;
case 5:
retVal = 4;
break;
case 6:
retVal = 5;
break;
case 7:
retVal = 6;
break;
}
return retVal;
}
/**
* 将字符串的日期转换成yy-MM-dd型的字符�? *
* @param str
* @return
*/
public static String getStrDateByStr(String str) {
if (str == null || str.equals("")) {
return "";
} else {
Date date = DateUtil.convertStrToDate(str);
return DateUtil.getDateString(date);
}
}
/**
* 获取年月日时分秒的字符串
*
* @param time
* @return
*/
public static String getStrTrueDateByStr(String str) {
String date = "";
if (str != null && !str.equals("")) {
date = DateUtil.getDateString((convertStrToDate(str,
"yyyy-MM-dd HH:mm:ss")), "yyyy-MM-dd HH:mm:ss");
}
return date;
}
/**
* 判断日期离现在日期是否超�?�? *
* @param time
* @return
*/
public static boolean isTimeout(String time) {
Date date = DateUtil.convertStrToDate(time);
Calendar c = Calendar.getInstance();
c.setTime(date);
Calendar thisDate = Calendar.getInstance();
thisDate.add(Calendar.DAY_OF_MONTH, -7);
boolean isTimeout = thisDate.after(c);
return isTimeout;
}
/**
* 给定的日期字符串增加或减少几�? *
* @param day
* 增加天数为正数,减少为负�? */
public static String getAddDateStr(String date, int day) {
Calendar c = Calendar.getInstance();
c.setTime(DateUtil.convertStrToDate(date));
c.add(Calendar.DAY_OF_MONTH, day);
return DateUtil.getDateString(c.getTime());
}
/**
* 判断两个日期字符串的大小,返回大的�?��
*/
public static String compareDateStr(String date1, String date2) {
int year1 = DateUtil.getYear(convertStrToDate(date1));
int month1 = DateUtil.getMonth(convertStrToDate(date1));
int day1 = DateUtil.getDay(convertStrToDate(date1));
int year2 = DateUtil.getYear(convertStrToDate(date2));
int month2 = DateUtil.getMonth(convertStrToDate(date2));
int day2 = DateUtil.getDay(convertStrToDate(date2));
if (year1 > year2) {
return date1;
} else {
if (year1 == year2) {
if (month1 > month2) {
return date1;
} else {
if (month1 == month2) {
if (day1 > day2) {
return date1;
} else {
if (day1 == day2) {
return null;
} else {
return date2;
}
}
} else {
return date2;
}
}
} else {
return date2;
}
}
}
/**
* 得到两天日期之间的每天的日期
*
* @param startDate
* @param endDate
* @return
*/
public static String[] dispatchTwoDate(String startDate, String endDate,
String thisDate) {
if (startDate == null && endDate == null && thisDate == null) {
return null;
}
if (thisDate != null && !thisDate.equals("")) {
return new String[] { thisDate };
}
Date sDate = null;
Date eDate = null;
if (startDate != null && !startDate.equals(""))
sDate = DateUtil.convertStrToDate(startDate);
if (endDate != null && !endDate.equals(""))
eDate = DateUtil.convertStrToDate(endDate);
int dispatchDay = (int) ((eDate.getTime() - sDate.getTime()) / (1000 * 60 * 60 * 24));
// System.out.println(dispatchDay);
String[] dateStr = new String[dispatchDay + 1];
Calendar c = Calendar.getInstance();
c.setTime(sDate);
dateStr[0] = DateUtil.getDateString(sDate);
// System.out.println("dateStr[0] "+dateStr[0]);
for (int i = 1; i < dispatchDay + 1; i++) {
c.add(Calendar.DAY_OF_MONTH, i);
dateStr[i] = DateUtil.getDateString(c.getTime());
// System.out.println("dateStr["+i+"] "+dateStr[i]);
c.setTime(sDate);
}
return dateStr;
}
/**
* 判断两个日期中间相差的年�? */
public static int discrepancyYearWithDate(Date date1,Date date2){
long disTime = 0;
System.out.println("date1 time : "+date1.getTime());
System.out.println("date2 time : "+date2.getTime());
if(date1.getTime() - date2.getTime()>0){
disTime = ((long)date1.getTime()-(long)date2.getTime());
}else{
disTime = ((long)date2.getTime()-(long)date1.getTime());
}
return (int)disTime;
}
public static java.sql.Date StringToDate(String param) {
if (param == null) {
return null;
} else {
java.util.Date date = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
date = sdf.parse(param);
return (java.sql.Date) new Date(date.getTime());
} catch (ParseException ex) {
//ex.printStackTrace();
return null;
}
}
}
public static void main(String arg[]){
Calendar c1 = Calendar.getInstance();
c1.set(2005, 12, 11);
Calendar c2 = Calendar.getInstance();
c2.setTime(new Date());
//System.out.println(DateUtil.discrepancyYearWithDate(c1.getTime(), c2.getTime()));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -