Java date相关 格式化(二)

it2022-05-06  5

import java.util.Calendar;

import java.util.Date;import java.util.GregorianCalendar;

public class ZCalendarUtils { /** * 取去年的今天 * @return */ public static String getLastYear() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.YEAR, -1); Date d = cal.getTime(); return DateTimeUtils.formatToDateTime(d); }

/** * 获取某一天是星期几 * @param dt * @return */ public static String getWeekOfDate(Date dt) { String[] weekDays = {"7", "1", "2", "3", "4", "5", "6"}; Calendar cal = Calendar.getInstance(); cal.setTime(dt); int w = cal.get(Calendar.DAY_OF_WEEK) - 1; if (w < 0){ w = 0; } return weekDays[w]; } /** * 取当前是星期几 * @return */ public static String getCurrentWeek() { String day = getWeekOfDate(new Date()); return day; } /** * 取当前月第一天 * @return */ public static String getFirstDayOfCurrentMonth() { Calendar cal = Calendar.getInstance(); cal.set(Calendar.DAY_OF_MONTH, 1); Date d = cal.getTime(); return DateTimeUtils.formatToDate(d); } /** * 取昨天 * @return */ public static String getYesterday() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.DAY_OF_MONTH, -1); Date d = cal.getTime(); return DateTimeUtils.formatToDate(d); } /** * 取当前月最后一天 * @return */ public static String getLastDayOfCurrentMonth() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MONTH, 1); cal.set(Calendar.DAY_OF_MONTH, 1); cal.add(Calendar.DAY_OF_MONTH, -1); Date d = cal.getTime(); return DateTimeUtils.formatToDate(d); } /** * 取上月第一天 * @return */ public static String getFirstDayOfLastMonth() { Calendar cal = Calendar.getInstance(); cal.add(cal.MONTH, -1); cal.set(Calendar.DAY_OF_MONTH, 1); Date d = cal.getTime(); return DateTimeUtils.formatToDate(d); } /** * 取上月最后一天 * @return */ public static String getLastDayOfLastMonth() { Calendar cal = Calendar.getInstance(); //取得系统当前时间所在月第一天时间对象 cal.set(Calendar.DAY_OF_MONTH, 1); //日期减一,取得上月最后一天时间对象 cal.add(Calendar.DAY_OF_MONTH, -1); Date d = cal.getTime(); return DateTimeUtils.formatToDate(d); } /** * 取当前是本月第几周 * @return */ public static int getWeekNumber(){ Calendar calendar = Calendar.getInstance(); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setTime(new Date()); return calendar.get(Calendar.WEEK_OF_MONTH) - 1; } /** * 取给定日期所在月份的第几周 * @param newDate * @param week * @return */ public static Calendar getCalendar(Date newDate, int week) { Calendar caleNew = Calendar.getInstance(); caleNew.setTime(newDate); caleNew.add(Calendar.WEEK_OF_MONTH, week -1 ); return caleNew; } /** * 获上星期的第一天 * * @return */ public static String getFirstDayOfLastWeek() { int currentWeekNo = ZCalendarUtils.getWeekNumber(); Date date = ZCalendarUtils.getFirstDayOfWeek(new Date(), currentWeekNo -1); return DateTimeUtils.formatToDate(date); } /** * 获取当前星期的第一天 * * @return */ public static String getFirstDayOfCurrentWeek() { int currentWeekNo = ZCalendarUtils.getWeekNumber(); Date date = ZCalendarUtils.getFirstDayOfWeek(new Date(), currentWeekNo); return DateTimeUtils.formatToDate(date); } /** * 获取上星期的最后一天 * * @return */ public static String getLastDayOfLastWeek() { int currentWeekNo = ZCalendarUtils.getWeekNumber(); Date date = ZCalendarUtils.getLastDayOfWeek(new Date(), currentWeekNo -1); return DateTimeUtils.formatToDate(date); } /** * 获取当前星期的最后一天 * * @return */ public static String getLastDayOfCurrentWeek() { int currentWeekNo = ZCalendarUtils.getWeekNumber(); Date date = ZCalendarUtils.getLastDayOfWeek(new Date(), currentWeekNo); return DateTimeUtils.formatToDate(date); } /** * 获取给定日期所在月的第n星期的第一天 * * @return */ private static Date getFirstDayOfWeek(Date month, int week) { Calendar myCale = Calendar.getInstance(); try{ GregorianCalendar gc = (GregorianCalendar)getCalendar(month, week ); myCale.setTime(gc.getTime()); myCale.set(Calendar.DATE, gc.get(Calendar.DATE) - gc.get(Calendar.DAY_OF_WEEK) + 2); }catch (Exception e) { e.printStackTrace(); } return myCale.getTime(); }

/** * 获取给定日期所在月的第n星期的最后一天 * * @return */ private static Date getLastDayOfWeek(Date month, int week) { Calendar myCale = Calendar.getInstance(); try{ GregorianCalendar gc = (GregorianCalendar)getCalendar(month, week); myCale.setTime(gc.getTime()); myCale.set(Calendar.DATE, gc.get(Calendar.DATE) + 8 - gc.get(Calendar.DAY_OF_WEEK)); }catch (Exception e) { e.printStackTrace(); } return myCale.getTime(); } public static int getYearOfDate(Date date) { if (date == null) { return 1990; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.YEAR); }

public static int getMonthOfDate(Date date) { if (date == null) { return 1; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.MONTH) + 1; }

public static int getDayOfDate(Date date) { if (date == null) { return 1; } Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.get(Calendar.DAY_OF_MONTH); } public static int getCurrentMonth() { return getMonthOfDate(new Date()); }

public static String getCurrentMonthStr() { return getCurrentMonth() + ""; }

public static String getMonthOfDateStr(Date date) { return getMonthOfDate(date) + ""; } /** * 获取某日期所在月份的最大天数 * * @param date * @return */ public static int getMaxDaysOfMonth(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calendar.getActualMaximum(Calendar.DAY_OF_MONTH); }

}

转载于:https://www.cnblogs.com/Yorkzhu/p/7833721.html


最新回复(0)