Commit 28cf2dc7 authored by Quxl's avatar Quxl
parents b76aaa3b c42a5e27
package com.egolm.common; package com.egolm.common;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.GregorianCalendar; import java.util.GregorianCalendar;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.SimpleTimeZone; import java.util.SimpleTimeZone;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class DateUtil { public class DateUtil {
public static final String FMT_HH = "HH"; public static final String FMT_HH = "HH";
public static final String FMT_DATE_HH = "yyyy-MM-dd HH"; public static final String FMT_DATE_HH = "yyyy-MM-dd HH";
public static final String FMT_TIME = "HH:mm:ss"; public static final String FMT_TIME = "HH:mm:ss";
public static final String FMT_DATE = "yyyy-MM-dd"; public static final String FMT_DATE = "yyyy-MM-dd";
public static final String FMTDATE = "yyyyMMdd"; public static final String FMTDATE = "yyyyMMdd";
public static final String FMT_DATE_MINUTE = "yyyy-MM-dd HH:mm"; public static final String FMT_DATE_MINUTE = "yyyy-MM-dd HH:mm";
public static final String FMT_DATE_SECOND = "yyyy-MM-dd HH:mm:ss"; public static final String FMT_DATE_SECOND = "yyyy-MM-dd HH:mm:ss";
public static final String FMT_DATE_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS"; public static final String FMT_DATE_MILLISECOND = "yyyy-MM-dd HH:mm:ss.SSS";
public static final String FMT_A_DATE = "yyyy/MM/dd"; public static final String FMT_A_DATE = "yyyy/MM/dd";
public static final String FMT_A_DATE_MINUTE = "yyyy/MM/dd HH:mm"; public static final String FMT_A_DATE_MINUTE = "yyyy/MM/dd HH:mm";
public static final String FMT_A_DATE_SECOND = "yyyy/MM/dd HH:mm:ss"; public static final String FMT_A_DATE_SECOND = "yyyy/MM/dd HH:mm:ss";
public static final String FMT_A_DATE_MILLISECOND = "yyyy/MM/dd HH:mm:ss.SSS"; public static final String FMT_A_DATE_MILLISECOND = "yyyy/MM/dd HH:mm:ss.SSS";
public static final String FMT_DATE_ISO = "yyyy-MM-ddTHH:mm:ss.SSSZ"; public static final String FMT_DATE_ISO = "yyyy-MM-ddTHH:mm:ss.SSSZ";
public static final String FMT_UTC_GMT = "EEE, dd MMM yyyy HH:mm:ss z"; public static final String FMT_UTC_GMT = "EEE, dd MMM yyyy HH:mm:ss z";
public static final String FMT_YYYYMMddHHMMSS = "yyyyMMddHHmmss"; public static final String FMT_YYYYMMddHHMMSS = "yyyyMMddHHmmss";
public static final String FMT_UTC_ALIYUN="yyyy-MM-dd'T'HH:mm:ss'Z'"; public static final String FMT_UTC_ALIYUN="yyyy-MM-dd'T'HH:mm:ss'Z'";
public static final Long SECOND = 1000L; public static final String FMT_YYYY_MM = "yyyy/MM";
public static final Long MINUTE = 1000L*60;
public static final Long HALF_HOUR = MINUTE*30; public static final Long SECOND = 1000L;
public static final Long HOUR = HALF_HOUR*2; public static final Long MINUTE = 1000L*60;
public static final Long HALF_DAY = HOUR*12; public static final Long HALF_HOUR = MINUTE*30;
public static final Long DAY = HALF_DAY*2; public static final Long HOUR = HALF_HOUR*2;
public static final Long HALF_DAY = HOUR*12;
private static final Map<Pattern, String> PATTERN_DATE = new LinkedHashMap<Pattern, String>(); public static final Long DAY = HALF_DAY*2;
static { private static final Map<Pattern, String> PATTERN_DATE = new LinkedHashMap<Pattern, String>();
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月(([0]?[1-9])|([1-2][0-9])|([3][0-1]))日\\s+\\d{1,2}时\\d{1,2}分\\d{1,2}秒)"), "yyyy年MM月dd日 HH时mm分ss秒");
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月(([0]?[1-9])|([1-2][0-9])|([3][0-1]))日\\s+\\d{1,2}:\\d{1,2}:\\d{1,2})"), "yyyy年MM月dd日 HH:mm:ss"); static {
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月(([0]?[1-9])|([1-2][0-9])|([3][0-1]))日)"), "yyyy年MM月dd日"); PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月(([0]?[1-9])|([1-2][0-9])|([3][0-1]))日\\s+\\d{1,2}时\\d{1,2}分\\d{1,2}秒)"), "yyyy年MM月dd日 HH时mm分ss秒");
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月)"), "yyyy年MM月"); PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月(([0]?[1-9])|([1-2][0-9])|([3][0-1]))日\\s+\\d{1,2}:\\d{1,2}:\\d{1,2})"), "yyyy年MM月dd日 HH:mm:ss");
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年)"), "yyyy年"); PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月(([0]?[1-9])|([1-2][0-9])|([3][0-1]))日)"), "yyyy年MM月dd日");
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年(([0]?[1-9])|([1][0-2]))月)"), "yyyy年MM月");
PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z)"), DateUtil.FMT_DATE_ISO); PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})年)"), "yyyy年");
PATTERN_DATE.put(Pattern.compile("(\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2})"), DateUtil.FMT_A_DATE_SECOND); PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.\\d{3}Z)"), DateUtil.FMT_DATE_ISO);
PATTERN_DATE.put(Pattern.compile("(\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2})"), DateUtil.FMT_A_DATE_MINUTE);
PATTERN_DATE.put(Pattern.compile("(\\d{4}/\\d{1,2}/\\d{1,2})"), DateUtil.FMT_A_DATE); PATTERN_DATE.put(Pattern.compile("(\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2})"), DateUtil.FMT_A_DATE_SECOND);
PATTERN_DATE.put(Pattern.compile("(\\d{4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2})"), DateUtil.FMT_A_DATE_MINUTE);
PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2})"), DateUtil.FMT_DATE_SECOND); PATTERN_DATE.put(Pattern.compile("(\\d{4}/\\d{1,2}/\\d{1,2})"), DateUtil.FMT_A_DATE);
PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2})"), DateUtil.FMT_DATE_MINUTE);
PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2})"), DateUtil.FMT_DATE); PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2})"), DateUtil.FMT_DATE_SECOND);
PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2})"), DateUtil.FMT_DATE_MINUTE);
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1])))"), "yyyyMMdd"); PATTERN_DATE.put(Pattern.compile("(\\d{4}-\\d{1,2}-\\d{1,2})"), DateUtil.FMT_DATE);
PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})(([0][1-9])|([1][0-2])))"), "yyyyMM");
PATTERN_DATE.put(Pattern.compile("([1-2][0-9]{3})"), "yyyy"); PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1])))"), "yyyyMMdd");
} PATTERN_DATE.put(Pattern.compile("(([1-2][0-9]{3})(([0][1-9])|([1][0-2])))"), "yyyyMM");
PATTERN_DATE.put(Pattern.compile("([1-2][0-9]{3})"), "yyyy");
/** }
* 将日期字符串转化成日期类型
* @param date /**
* @param fmt * 将日期字符串转化成日期类型
* @return * @param date
* @throws ParseException * @param fmt
*/ * @return
public static Date parse(String date, String fmt) { * @throws ParseException
try { */
SimpleDateFormat sdf = new SimpleDateFormat(fmt); public static Date parse(String date, String fmt) {
Date newdate = sdf.parse(date); try {
return newdate; SimpleDateFormat sdf = new SimpleDateFormat(fmt);
} catch (Exception e) { Date newdate = sdf.parse(date);
throw new RuntimeException(date + " parse to" + fmt, e); return newdate;
} } catch (Exception e) {
} throw new RuntimeException(date + " parse to" + fmt, e);
}
/** }
* 自动判断日期字符串格式并将其转化为日期类型
* @param str /**
* @return * 自动判断日期字符串格式并将其转化为日期类型
*/ * @param str
public static Date parse(Object obj) { * @return
if(obj instanceof Date) { */
return new Date(((Date)obj).getTime()); public static Date parse(Object obj) {
} else { if(obj instanceof Date) {
String str = String.valueOf(obj).trim(); return new Date(((Date)obj).getTime());
if(StringUtil.isNotEmpty(str)) { } else {
for(Pattern pattern : PATTERN_DATE.keySet()) { String str = String.valueOf(obj).trim();
Matcher matcher = pattern.matcher(str); if(StringUtil.isNotEmpty(str)) {
if(matcher.find()) { for(Pattern pattern : PATTERN_DATE.keySet()) {
String date_str = matcher.group(1); Matcher matcher = pattern.matcher(str);
if(PATTERN_DATE.get(pattern).equals(DateUtil.FMT_DATE_ISO)) { if(matcher.find()) {
return parseISODate(date_str); String date_str = matcher.group(1);
} else { if(PATTERN_DATE.get(pattern).equals(DateUtil.FMT_DATE_ISO)) {
return parse(date_str, PATTERN_DATE.get(pattern)); return parseISODate(date_str);
} } else {
} return parse(date_str, PATTERN_DATE.get(pattern));
} }
} }
return null; }
} }
} return null;
}
public static Date parseISODate(String isoDate) { }
isoDate = isoDate.replace("T", " ").replace("Z", "");
Date date = parse(isoDate); public static Date parseISODate(String isoDate) {
java.util.Calendar cal = java.util.Calendar.getInstance(); isoDate = isoDate.replace("T", " ").replace("Z", "");
cal.setTime(date); Date date = parse(isoDate);
int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET); java.util.Calendar cal = java.util.Calendar.getInstance();
int dstOffset = cal.get(java.util.Calendar.DST_OFFSET); cal.setTime(date);
cal.add(Calendar.MILLISECOND, (zoneOffset + dstOffset)); int zoneOffset = cal.get(java.util.Calendar.ZONE_OFFSET);
return cal.getTime(); int dstOffset = cal.get(java.util.Calendar.DST_OFFSET);
} cal.add(Calendar.MILLISECOND, (zoneOffset + dstOffset));
return cal.getTime();
public static boolean isDate(Object obj) { }
if(obj instanceof Date) {
return true; public static boolean isDate(Object obj) {
} else { if(obj instanceof Date) {
String str = String.valueOf(obj); return true;
if(StringUtil.isNotEmpty(str)) { } else {
String date_str = str.trim(); String str = String.valueOf(obj);
for(Pattern pattern : PATTERN_DATE.keySet()) { if(StringUtil.isNotEmpty(str)) {
if(pattern.matcher(date_str).find()) { String date_str = str.trim();
return true; for(Pattern pattern : PATTERN_DATE.keySet()) {
} if(pattern.matcher(date_str).find()) {
} return true;
} }
return false; }
} }
} return false;
}
/** }
* 格式化日期为字符串
* @param date /**
* @param fmt * 格式化日期为字符串
* @return * @param date
*/ * @param fmt
public static String format(Date date, String fmt) { * @return
if(date == null) { */
return null; public static String format(Date date, String fmt) {
} else { if(date == null) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt); return null;
return sdf.format(date); } else {
} SimpleDateFormat sdf = new SimpleDateFormat(fmt);
} return sdf.format(date);
}
/** }
* 格式化为yyyy-MM-dd格式
* @param date 时间参数 /**
* @return yyyy-MM-dd * 格式化为yyyy-MM-dd格式
*/ * @param date 时间参数
public static String formatDate(Date date) { * @return yyyy-MM-dd
return format(date, DateUtil.FMT_DATE); */
} public static String formatDate(Date date) {
return format(date, DateUtil.FMT_DATE);
/** }
* 格式化日期为yyyy-MM-dd HH:mm:ss格式
* @param date /**
* @return yyyy-MM-dd HH:mm:ss * 格式化日期为yyyy-MM-dd HH:mm:ss格式
*/ * @param date
public static String format(Date date) { * @return yyyy-MM-dd HH:mm:ss
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FMT_DATE_SECOND); */
return sdf.format(date); public static String format(Date date) {
} SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FMT_DATE_SECOND);
return sdf.format(date);
public static String formatUTCDate(Date date) { }
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
dateFormat.setTimeZone(new SimpleTimeZone(0, "UTC")); public static String formatUTCDate(Date date) {
return dateFormat.format(date); SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
} dateFormat.setTimeZone(new SimpleTimeZone(0, "UTC"));
return dateFormat.format(date);
public static String formatGMTDate(Date date) { }
SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT")); public static String formatGMTDate(Date date) {
return dateFormat.format(date); SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
} dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
return dateFormat.format(date);
/** }
* 将毫秒数格式化成默认格式日期字符串
* 默认格式:yyyy-MM-dd HH:mm:ss /**
* @param time * 将毫秒数格式化成默认格式日期字符串
* @return * 默认格式:yyyy-MM-dd HH:mm:ss
*/ * @param time
public static String format(Long time) { * @return
if(time != null) { */
Date date = new Date(time); public static String format(Long time) {
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FMT_DATE_SECOND); if(time != null) {
return sdf.format(date); Date date = new Date(time);
} else { SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FMT_DATE_SECOND);
return null; return sdf.format(date);
} } else {
} return null;
}
/** }
* 将毫秒数格式化为制定格式的日期字符串
* @param time /**
* @param fmt * 将毫秒数格式化为制定格式的日期字符串
* @return * @param time
*/ * @param fmt
public static String format(Long time, String fmt) { * @return
if(time != null) { */
Date date = new Date(time); public static String format(Long time, String fmt) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt); if(time != null) {
return sdf.format(date); Date date = new Date(time);
} else { SimpleDateFormat sdf = new SimpleDateFormat(fmt);
return null; return sdf.format(date);
} } else {
} return null;
}
/** }
* 将毫秒数格式化为时间格式字符串
* 毫秒数必须小于86400000 /**
* @param time * 将毫秒数格式化为时间格式字符串
* @return * 毫秒数必须小于86400000
*/ * @param time
public static String format(Integer time) { * @return
return format(time, DateUtil.FMT_TIME); */
} public static String format(Integer time) {
return format(time, DateUtil.FMT_TIME);
/** }
* 将毫秒数格式化为时间格式字符串
* 毫秒数必须小于86400000 /**
* @param time * 将毫秒数格式化为时间格式字符串
* @param fmt * 毫秒数必须小于86400000
* @return * @param time
*/ * @param fmt
public static String format(Integer time, String fmt) { * @return
if(time != null) { */
if(time < 86400000) { public static String format(Integer time, String fmt) {
Date date = new Date(time - (1000*60*60*8)); if(time != null) {
SimpleDateFormat sdf = new SimpleDateFormat(fmt); if(time < 86400000) {
return sdf.format(date); Date date = new Date(time - (1000*60*60*8));
} else { SimpleDateFormat sdf = new SimpleDateFormat(fmt);
throw new RuntimeException("Can't greater than 86400000"); return sdf.format(date);
} } else {
} else { throw new RuntimeException("Can't greater than 86400000");
return null; }
} } else {
} return null;
}
/** }
* 计算指定时间当天的开始时间
* @param time /**
* @return * 计算指定时间当天的开始时间
* @throws ParseException * @param time
*/ * @return
public static Date noon(Date time) { * @throws ParseException
Long start = start(time == null ? new Date() : time).getTime(); */
return new Date(start + HALF_DAY); public static Date noon(Date time) {
} Long start = start(time == null ? new Date() : time).getTime();
return new Date(start + HALF_DAY);
/** }
* 计算指定时间当天的开始时间
* @param time /**
* @return * 计算指定时间当天的开始时间
* @throws ParseException * @param time
*/ * @return
public static Date start(Date time) { * @throws ParseException
if(time != null) { */
return parse(format(time, DateUtil.FMT_DATE), DateUtil.FMT_DATE); public static Date start(Date time) {
} else { if(time != null) {
return null; return parse(format(time, DateUtil.FMT_DATE), DateUtil.FMT_DATE);
} } else {
} return null;
}
/** }
* 计算指定日期当天的最后时间
* @param time /**
* @return * 计算指定日期当天的最后时间
* @throws ParseException * @param time
*/ * @return
public static Date end(Date time) { * @throws ParseException
if(time != null) { */
return new Date(start(new Date(time.getTime() + (24L*60L*60L*1000L))).getTime()); public static Date end(Date time) {
} else { if(time != null) {
return null; return new Date(start(new Date(time.getTime() + (24L*60L*60L*1000L))).getTime());
} } else {
} return null;
}
/** }
* 取得当前时间向后或向前若干天的时间
* @param time /**
* @param offset * 取得当前时间向后或向前若干天的时间
* @return * @param time
*/ * @param offset
public static Date day(Date time, Integer offset) { * @return
Date date = new Date(time.getTime() + (offset*86400000L)); */
return date; public static Date day(Date time, Integer offset) {
} Date date = new Date(time.getTime() + (offset*86400000L));
return date;
/** }
* 计算指定的日期是星期几,返回数字 周日至周六分别是0-6
* @param date /**
* @return * 计算指定的日期是星期几,返回数字 周日至周六分别是0-6
*/ * @param date
public static Integer week(Date date) { * @return
Calendar calendar = Calendar.getInstance(); */
calendar.setTime(date); public static Integer week(Date date) {
return calendar.get(Calendar.DAY_OF_WEEK) - 1; Calendar calendar = Calendar.getInstance();
} calendar.setTime(date);
return calendar.get(Calendar.DAY_OF_WEEK) - 1;
/** }
* 计算两天之间相隔多少天
* @param start /**
* @param end * 计算两天之间相隔多少天
* @return * @param start
*/ * @param end
public static Integer days(Date start, Date end) { * @return
Date date_start = start(start); */
Date date_end = start(end); public static Integer days(Date start, Date end) {
return (int)((date_end.getTime() - date_start.getTime()) / 86400000L); Date date_start = start(start);
} Date date_end = start(end);
return (int)((date_end.getTime() - date_start.getTime()) / 86400000L);
/** }
* 判断是否是今天
* @param date /**
* @return * 判断是否是今天
*/ * @param date
public static boolean isToday(Date date) { * @return
Date today = new Date(); */
Date start = start(today); public static boolean isToday(Date date) {
Date end = end(today); Date today = new Date();
if(date.getTime() >= start.getTime() && date.getTime() < end.getTime()) { Date start = start(today);
return true; Date end = end(today);
} else { if(date.getTime() >= start.getTime() && date.getTime() < end.getTime()) {
return false; return true;
} } else {
} return false;
}
/** }
* 取得当前日期是多少周
* @param date /**
* @return * 取得当前日期是多少周
*/ * @param date
public static int getWeekOfYear(Date date) { * @return
Calendar c = new GregorianCalendar(); */
c.setFirstDayOfWeek(Calendar.MONDAY); public static int getWeekOfYear(Date date) {
c.setMinimalDaysInFirstWeek(7); Calendar c = new GregorianCalendar();
c.setTime (date); c.setFirstDayOfWeek(Calendar.MONDAY);
return c.get(Calendar.WEEK_OF_YEAR); c.setMinimalDaysInFirstWeek(7);
} c.setTime (date);
return c.get(Calendar.WEEK_OF_YEAR);
/** }
* 得到某一年周的总数
* @param year /**
* @return * 得到某一年周的总数
*/ * @param year
public static int getMaxWeekNumOfYear(int year) { * @return
Calendar c = new GregorianCalendar(); */
c.set(year, Calendar.DECEMBER, 31, 23, 59, 59); public static int getMaxWeekNumOfYear(int year) {
return getWeekOfYear(c.getTime()); Calendar c = new GregorianCalendar();
} c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
return getWeekOfYear(c.getTime());
/** }
* 得到某年某周的第一天
* @param year /**
* @param week * 得到某年某周的第一天
* @return * @param year
*/ * @param week
public static Date getFirstDayOfWeek(int year, int week) { * @return
Calendar c = new GregorianCalendar(); */
c.set(Calendar.YEAR, year); public static Date getFirstDayOfWeek(int year, int week) {
c.set (Calendar.MONTH, Calendar.JANUARY); Calendar c = new GregorianCalendar();
c.set(Calendar.DATE, 1); c.set(Calendar.YEAR, year);
Calendar cal = (GregorianCalendar) c.clone(); c.set (Calendar.MONTH, Calendar.JANUARY);
cal.add(Calendar.DATE, week * 7); c.set(Calendar.DATE, 1);
return getFirstDayOfWeek(cal.getTime ()); Calendar cal = (GregorianCalendar) c.clone();
} cal.add(Calendar.DATE, week * 7);
return getFirstDayOfWeek(cal.getTime ());
/** }
* 得到某年某周的最后一天
* @param year /**
* @param week * 得到某年某周的最后一天
* @return * @param year
*/ * @param week
public static Date getLastDayOfWeek(int year, int week) { * @return
Calendar c = new GregorianCalendar(); */
c.set(Calendar.YEAR, year); public static Date getLastDayOfWeek(int year, int week) {
c.set(Calendar.MONTH, Calendar.JANUARY); Calendar c = new GregorianCalendar();
c.set(Calendar.DATE, 1); c.set(Calendar.YEAR, year);
Calendar cal = (GregorianCalendar) c.clone(); c.set(Calendar.MONTH, Calendar.JANUARY);
cal.add(Calendar.DATE , week * 7); c.set(Calendar.DATE, 1);
return getLastDayOfWeek(cal.getTime()); Calendar cal = (GregorianCalendar) c.clone();
} cal.add(Calendar.DATE , week * 7);
return getLastDayOfWeek(cal.getTime());
/** }
* 取得当前日期所在周的第一天
* @param date /**
* @return * 取得当前日期所在周的第一天
*/ * @param date
public static Date getFirstDayOfWeek(Date date) { * @return
Calendar c = new GregorianCalendar(); */
c.setFirstDayOfWeek(Calendar.MONDAY); public static Date getFirstDayOfWeek(Date date) {
c.setTime(date); Calendar c = new GregorianCalendar();
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday c.setFirstDayOfWeek(Calendar.MONDAY);
return c.getTime (); c.setTime(date);
} c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
return c.getTime ();
/** }
* 取得当前日期所在周的最后一天
* @param date /**
* @return * 取得当前日期所在周的最后一天
*/ * @param date
public static Date getLastDayOfWeek(Date date) { * @return
Calendar c = new GregorianCalendar(); */
c.setFirstDayOfWeek(Calendar.MONDAY); public static Date getLastDayOfWeek(Date date) {
c.setTime(date); Calendar c = new GregorianCalendar();
c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); c.setFirstDayOfWeek(Calendar.MONDAY);
return c.getTime(); c.setTime(date);
} c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6);
return c.getTime();
/** }
* 取得当前日期所在月的第一天
* @param date /**
* @return * 取得当前日期所在月的第一天
*/ * @param date
public static Date getFirstDayOfMonth(Date date){ * @return
Calendar c = Calendar.getInstance(); */
c.add(Calendar.MONTH, 0); public static Date getFirstDayOfMonth(Date date){
c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 Calendar c = Calendar.getInstance();
return c.getTime(); c.add(Calendar.MONTH, 0);
} c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
public static String getTimeStamp() { return c.getTime();
return String.valueOf(System.currentTimeMillis() / 1000); }
} /**
}
 * 获取指定日期下个月的第一天 的日期
* @param dateStr
* @param format
* @return
*/
public static String getFirstDayOfNextMonth(String dateStr,String format){
SimpleDateFormat sdf = new SimpleDateFormat(format);
try {
Date date = sdf.parse(dateStr);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.set(Calendar.DAY_OF_MONTH,1);
calendar.add(Calendar.MONTH, 1);
return sdf.format(calendar.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
public static String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment