📄 dateutil.java
字号:
* @param pattern
* 日期模式
* @return 给定字符串描述的日期对象。
*/
public static Date getDateFromString(String dateStr, String pattern) {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
Date resDate = null;
try {
resDate = sdf.parse(dateStr);
} catch (Exception e) {
e.printStackTrace();
}
return resDate;
}
/**
* 取得当前Date对象.
*
* @return Date 当前Date对象.
*/
public static Date getDateObj() {
Calendar c = new GregorianCalendar();
return c.getTime();
}
/**
*
* @return 当前月份有多少天;
*/
public static int getDaysOfCurMonth() {
int curyear = new Integer(getCurrentYear()).intValue(); // 当前年份
int curMonth = new Integer(getCurrentMonth()).intValue();// 当前月份
int mArray[] = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
// 判断闰年的情况 ,2月份有29天;
if ((curyear % 400 == 0)
|| ((curyear % 100 != 0) && (curyear % 4 == 0))) {
mArray[1] = 29;
}
return mArray[curMonth - 1];
// 如果要返回下个月的天数,注意处理月份12的情况,防止数组越界;
// 如果要返回上个月的天数,注意处理月份1的情况,防止数组越界;
}
/**
* 根据指定的年月 返回指定月份(yyyy-MM)有多少天。
*
* @param time yyyy-MM
* @return 天数,指定月份的天数。
*/
public static int getDaysOfCurMonth(final String time) {
String[] timeArray = time.split("-");
int curyear = new Integer(timeArray[0]).intValue(); // 当前年份
int curMonth = new Integer(timeArray[1]).intValue();// 当前月份
int mArray[] = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31 };
// 判断闰年的情况 ,2月份有29天;
if ((curyear % 400 == 0)
|| ((curyear % 100 != 0) && (curyear % 4 == 0))) {
mArray[1] = 29;
}
if (curMonth == 12) {
return mArray[0];
}
return mArray[curMonth - 1];
// 如果要返回下个月的天数,注意处理月份12的情况,防止数组越界;
// 如果要返回上个月的天数,注意处理月份1的情况,防止数组越界;
}
/**
* 返回指定为年度为year月度为month的月份内,第weekOfMonth个星期的第dayOfWeek天。<br>
* 00 00 00 01 02 03 04 <br>
* 05 06 07 08 09 10 11<br>
* 12 13 14 15 16 17 18<br>
* 19 20 21 22 23 24 25<br>
* 26 27 28 29 30 31 <br>
* 2006年的第一个周的1到7天为:05 06 07 01 02 03 04 <br>
* 2006年的第二个周的1到7天为:12 13 14 08 09 10 11 <br>
* 2006年的第三个周的1到7天为:19 20 21 15 16 17 18 <br>
* 2006年的第四个周的1到7天为:26 27 28 22 23 24 25 <br>
* 2006年的第五个周的1到7天为:02 03 04 29 30 31 01 。本月没有就自动转到下个月了。
*
* @param year
* 形式为yyyy <br>
* @param month
* 形式为MM,参数值在[1-12]。<br>
* @param weekOfMonth
* 在[1-6],因为一个月最多有6个周。<br>
* @param dayOfWeek
* 数字在1到7之间,包括1和7。1表示星期天,7表示星期六<br>
* -6为星期日-1为星期五,0为星期六 <br>
* @return <type>int</type>
*/
public static int getDayofWeekInMonth(String year, String month,
String weekOfMonth, String dayOfWeek) {
Calendar cal = new GregorianCalendar();
// 在具有默认语言环境的默认时区内使用当前时间构造一个默认的 GregorianCalendar。
int y = new Integer(year).intValue();
int m = new Integer(month).intValue();
cal.clear();// 不保留以前的设置
cal.set(y, m - 1, 1);// 将日期设置为本月的第一天。
cal.set(Calendar.DAY_OF_WEEK_IN_MONTH, new Integer(weekOfMonth)
.intValue());
cal.set(Calendar.DAY_OF_WEEK, new Integer(dayOfWeek).intValue());
// System.out.print(cal.get(Calendar.MONTH)+" ");
// System.out.print("当"+cal.get(Calendar.WEEK_OF_MONTH)+"\t");
// WEEK_OF_MONTH表示当天在本月的第几个周。不管1号是星期几,都表示在本月的第一个周
return cal.get(Calendar.DAY_OF_MONTH);
}
/**
* 根据指定的年月日小时分秒,返回一个java.Util.Date对象。
*
* @param year 年
* @param month 月 0-11
* @param date 日
* @param hourOfDay 小时 0-23
* @param minute 分 0-59
* @param second 秒 0-59
* @return 一个Date对象。
*/
public static Date getDate(int year, int month, int date, int hourOfDay,
int minute, int second) {
Calendar cal = new GregorianCalendar();
cal.set(year, month, date, hourOfDay, minute, second);
return cal.getTime();
}
/**
* 根据指定的年、月、日返回当前是星期几。1表示星期天、2表示星期一、7表示星期六。
*
* @param year
* @param month
* month是从1开始的12结束
* @param day
* @return 返回一个代表当期日期是星期几的数字。1表示星期天、2表示星期一、7表示星期六。
*/
public static int getDayOfWeek(String year, String month, String day) {
Calendar cal = new GregorianCalendar(new Integer(year).intValue(),
new Integer(month).intValue() - 1, new Integer(day).intValue());
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* 根据指定的年、月、日返回当前是星期几。1表示星期天、2表示星期一、7表示星期六。
*
* @param date
* "yyyy/MM/dd",或者"yyyy-MM-dd"
* @return 返回一个代表当期日期是星期几的数字。1表示星期天、2表示星期一、7表示星期六。
*/
public static int getDayOfWeek(String date) {
String[] temp = null;
if (date.indexOf("/") > 0) {
temp = date.split("/");
}
if (date.indexOf("-") > 0) {
temp = date.split("-");
}
return getDayOfWeek(temp[0], temp[1], temp[2]);
}
/**
* 返回当前日期是星期几。例如:星期日、星期一、星期六等等。
* @param date 格式为 yyyy/MM/dd 或者 yyyy-MM-dd
* @return 返回当前日期是星期几
*/
public static String getChinaDayOfWeek(String date){
String[] weeks = new String[]{"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};
int week = getDayOfWeek(date);
return weeks[week-1];
}
/**
* 根据指定的年、月、日返回当前是星期几。1表示星期天、2表示星期一、7表示星期六。
*
* @param date
*
* @return 返回一个代表当期日期是星期几的数字。1表示星期天、2表示星期一、7表示星期六。
*/
public static int getDayOfWeek(Date date) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
return cal.get(Calendar.DAY_OF_WEEK);
}
/**
* 返回制定日期所在的周是一年中的第几个周。<br>
* created by wangmj at 20060324.<br>
*
* @param year
* @param month
* 范围1-12<br>
* @param day
* @return int
*/
public static int getWeekOfYear(String year, String month, String day) {
Calendar cal = new GregorianCalendar();
cal.clear();
cal.set(new Integer(year).intValue(),
new Integer(month).intValue() - 1, new Integer(day).intValue());
return cal.get(Calendar.WEEK_OF_YEAR);
}
/**
* 取得给定日期加上一定天数后的日期对象.
*
* @param date
* 给定的日期对象
* @param amount
* 需要添加的天数,如果是向前的天数,使用负数就可以.
* @return Date 加上一定天数以后的Date对象.
*/
public static Date getDateAdd(Date date, int amount) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(GregorianCalendar.DATE, amount);
return cal.getTime();
}
/**
* 取得给定日期加上一定天数后的日期对象.
*
* @param date
* 给定的日期对象
* @param amount
* 需要添加的天数,如果是向前的天数,使用负数就可以.
* @param format
* 输出格式.
* @return Date 加上一定天数以后的Date对象.
*/
public static String getFormatDateAdd(Date date, int amount, String format) {
Calendar cal = new GregorianCalendar();
cal.setTime(date);
cal.add(GregorianCalendar.DATE, amount);
return getFormatDateTime(cal.getTime(), format);
}
/**
* 获得当前日期固定间隔天数的日期,如前60天dateAdd(-60)
*
* @param amount
* 距今天的间隔日期长度,向前为负,向后为正
* @param format
* 输出日期的格式.
* @return java.lang.String 按照格式输出的间隔的日期字符串.
*/
public static String getFormatCurrentAdd(int amount, String format) {
Date d = getDateAdd(new Date(), amount);
return getFormatDateTime(d, format);
}
/**
* 取得给定格式的昨天的日期输出
*
* @param format
* 日期输出的格式
* @return String 给定格式的日期字符串.
*/
public static String getFormatYestoday(String format) {
return getFormatCurrentAdd(-1, format);
}
/**
* 返回指定日期的前一天。<br>
* @param sourceDate
* @param format yyyy MM dd hh mm ss
* @return 返回日期字符串,形式和formcat一致。
*/
public static String getYestoday(String sourceDate, String format) {
return getFormatDateAdd(getDateFromString(sourceDate, format), -1,
format);
}
/**
* 返回明天的日期,<br>
* @param format
* @return 返回日期字符串,形式和formcat一致。
*/
public static String getFormatTomorrow(String format) {
return getFormatCurrentAdd(1, format);
}
/**
* 返回指定日期的后一天。<br>
* @param sourceDate
* @param format
* @return 返回日期字符串,形式和formcat一致。
*/
public static String getFormatDateTommorrow(String sourceDate, String format) {
return getFormatDateAdd(getDateFromString(sourceDate, format), 1,
format);
}
/**
* 根据主机的默认 TimeZone,来获得指定形式的时间字符串。
* @param dateFormat
* @return 返回日期字符串,形式和formcat一致。
*/
public static String getCurrentDateString(String dateFormat) {
Calendar cal = Calendar.getInstance(TimeZone.getDefault());
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
sdf.setTimeZone(TimeZone.getDefault());
return sdf.format(cal.getTime());
}
/**
* @deprecated 不鼓励使用。
* 返回当前时间串 格式:yyMMddhhmmss,在上传附件时使用
*
* @return String
*/
public static String getCurDate() {
GregorianCalendar gcDate = new GregorianCalendar();
int year = gcDate.get(GregorianCalendar.YEAR);
int month = gcDate.get(GregorianCalendar.MONTH) + 1;
int day = gcDate.get(GregorianCalendar.DAY_OF_MONTH);
int hour = gcDate.get(GregorianCalendar.HOUR_OF_DAY);
int minute = gcDate.get(GregorianCalendar.MINUTE);
int sen = gcDate.get(GregorianCalendar.SECOND);
String y;
String m;
String d;
String h;
String n;
String s;
y = new Integer(year).toString();
if (month < 10) {
m = "0" + new Integer(month).toString();
} else {
m = new Integer(month).toString();
}
if (day < 10) {
d = "0" + new Integer(day).toString();
} else {
d = new Integer(day).toString();
}
if (hour < 10) {
h = "0" + new Integer(hour).toString();
} else {
h = new Integer(hour).toString();
}
if (minute < 10) {
n = "0" + new Integer(minute).toString();
} else {
n = new Integer(minute).toString();
}
if (sen < 10) {
s = "0" + new Integer(sen).toString();
} else {
s = new Integer(sen).toString();
}
return "" + y + m + d + h + n + s;
}
/**
* 根据给定的格式,返回时间字符串。 和getFormatDate(String format)相似。
*
* @param format yyyy MM dd hh mm ss
* @return 返回一个时间字符串
*/
public static String getCurTimeByFormat(String format) {
Date newdate = new Date(System.currentTimeMillis());
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(newdate);
}
/**
* 获取两个时间串时间的差值,单位为秒
*
* @param startTime
* 开始时间 yyyy-MM-dd HH:mm:ss
* @param endTime
* 结束时间 yyyy-MM-dd HH:mm:ss
* @return 两个时间的差值(秒)
*/
public static long getDiff(String startTime, String endTime) {
long diff = 0;
SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date startDate = ft.parse(startTime);
Date endDate = ft.parse(endTime);
diff = startDate.getTime() - endDate.getTime();
diff = diff / 1000;
} catch (ParseException e) {
e.printStackTrace();
}
return diff;
}
/**
* 获取小时/分钟/秒
*
* @param second
* 秒
* @return 包含小时、分钟、秒的时间字符串,例如3小时23分钟13秒。
*/
public static String getHour(long second) {
long hour = second / 60 / 60;
long minute = (second - hour * 60 * 60) / 60;
long sec = (second - hour * 60 * 60) - minute * 60;
return hour + "小时" + minute + "分钟" + sec + "秒";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -