import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; /*** * Date tool class * */ public class DateUtil { public static final String ENG_DATE_FROMAT = "EEE, d MMM yyyy HH:mm:ss z"; public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"; public static final String YYYY_MM_DD = "yyyy-MM-dd"; public static final String YYYY = "yyyy"; public static final String MM = "MM"; public static final String DD = "dd"; /** * Convert time object to string * @param date * @param formatStr * @return */ public static String dateToString(Date date, String formatStr) { String strDate = ""; SimpleDateFormat sdf = new SimpleDateFormat(formatStr); strDate = sdf.format(date); return strDate; } /** * Get the current time of the system * @return */ public static Date getCurrentDate(){ return new Date(); } /** * Compare the difference in days between two dates * @param startDate start date * @param endDate end date * @return */ public static int daysOfDate(Date startDate, Date endDate) { if (startDate == null) throw new RuntimeException( "input parametars beginDate can't be null!"); if (endDate == null) throw new RuntimeException( "input parametars endDate can't be null!"); Calendar calBegin = Calendar.getInstance(); Calendar calEnd = Calendar.getInstance(); Calendar cal = Calendar.getInstance(); cal.setTime(startDate); calBegin.clear(); calBegin.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); cal.setTime(endDate); calEnd.clear(); calEnd.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); long times = calEnd.getTimeInMillis() - calBegin.getTimeInMillis(); Long days = new Long(times / (24 * 60 * 60 * 1000)); return days.intValue(); } /** * Get the start date of the week of a year * @param year * @param week * @return */ public static Date getFirstDayOfWeek(int year, int week) { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getFirstDayOfWeek(cal.getTime()); } /** * Get the end date of the week of a year * @param year * @param week * @return */ public static Date getLastDayOfWeek(int year, int week) { Calendar c = new GregorianCalendar(); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, Calendar.JANUARY); c.set(Calendar.DATE, 1); Calendar cal = (GregorianCalendar) c.clone(); cal.add(Calendar.DATE, week * 7); return getLastDayOfWeek(cal.getTime()); } /** * Get the start date of the week at the current time * @param date * @return */ public static Date getFirstDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday return c.getTime(); } /** * Get the end date of the week at the current time * @param date * @return */ public static Date getLastDayOfWeek(Date date) { Calendar c = new GregorianCalendar(); c.setFirstDayOfWeek(Calendar.MONDAY); c.setTime(date); c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday return c.getTime(); } }
date tool
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326141169&siteId=291194637
Recommended
Ranking