日期时间(二)
// Calendar.class // 如果直接设置月份或者星期时请注意±1,使用SimpleDateFormat则无此问题。 /* * #DAY_OF_WEEK * public final static int SUNDAY = 1; * public final static int MONDAY = 2; * public final static int TUESDAY = 3; * public final static int WEDNESDAY = 4; * public final static int THURSDAY = 5; * public final static int FRIDAY = 6; * public final static int SATURDAY = 7; * * #MONTH public * final static int JANUARY = 0; * public final static int FEBRUARY = 1; * public final static int MARCH = 2; * public final static int APRIL = 3; * public final static int MAY = 4; * public final static int JUNE = 5; * public final static int JULY = 6; * public final static int AUGUST = 7; * public final static int SEPTEMBER = 8; * public final static int OCTOBER = 9; * public final static int NOVEMBER = 10; * public final static int DECEMBER = 11; * public final static int UNDECIMBER = 12; */
Map<String, Integer> java.util.Calendar.getDisplayNames(int field, int style, Locale locale)
field 可以显示的有:几月,星期几
style 可能的样式:缩写,全拼,全部
localse 所有可能的时区
public static void main(String[] args) { Calendar c = Calendar.getInstance(); System.out.println(c.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.UK)); System.out.println(c.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.UK)); System.out.println(c.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.ALL_STYLES, Locale.UK)); System.out.println(c.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.LONG, Locale.UK)); System.out.println(c.getDisplayNames(Calendar.MONTH, Calendar.LONG, Locale.UK)); System.out.println(c.getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.UK)); }
结果:
{Saturday=7, Thursday=5, Monday=2, Tuesday=3, Wednesday=4, Friday=6, Sunday=1} {Thu=5, Wed=4, Sun=1, Sat=7, Fri=6, Tue=3, Mon=2} {Thu=5, Thursday=5, Saturday=7, Monday=2, Wed=4, Sun=1, Tuesday=3, Sat=7, Wednesday=4, Fri=6, Tue=3, Mon=2, Friday=6, Sunday=1} Wednesday {October=9, January=0, April=3, February=1, August=7, June=5, November=10, July=6, May=4, December=11, March=2, September=8} June
常见操作
1) 获取上一个5分整时间段
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String format = sdf.format(new Date()); Date now = sdf.parse(format); Integer curMin = Integer.valueOf(format.split(":")[1]); Date stime = null; Date etime = null; if(curMin%5 == 0) { stime = new Date(now.getTime() - 5 * 60 * 1000); etime = now; }else { int i=0; for(;curMin%5 != 0;curMin--) { i++; } stime = new Date(now.getTime() - (i+5) * 60 * 1000); etime = new Date(now.getTime() - (i+0) * 60 * 1000); } SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(sdf2.format(stime)); System.out.println(sdf2.format(etime));
public static Date[] getThisWeek(boolean tillToday) { int len = 7; if (tillToday) { len = Calendar.getInstance().get(Calendar.DAY_OF_WEEK) - 1; } Date[] result = new Date[len]; for (int i = 0; i < len; i++) { Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); c.add(Calendar.DATE, i); result[i] = c.getTime(); } return result; } public static Date[] getLastWeek() { Date[] result = new Date[7]; for (int i = 0; i < 7; i++) { Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); c.add(Calendar.DATE, i-7); result[i] = c.getTime(); } return result; } public static Date[] getThisMonth(boolean tillToday) { Calendar now = Calendar.getInstance(); int len = now.getActualMaximum(Calendar.DAY_OF_MONTH); if (tillToday) { len = now.get(Calendar.DAY_OF_MONTH); } Date[] result = new Date[len]; for (int i = 0; i < len; i++) { Calendar c = Calendar.getInstance(); c.set(Calendar.DAY_OF_MONTH, 1); c.add(Calendar.DATE, i); result[i] = c.getTime(); } return result; } public static Date[] getLastMonth() { Calendar now = Calendar.getInstance(); now.set(Calendar.MONTH, now.get(Calendar.MONTH) - 1); int len = now.getActualMaximum(Calendar.DAY_OF_MONTH); Date[] result = new Date[len]; for (int i = 0; i < len; i++) { Calendar c = Calendar.getInstance(); c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1); c.set(Calendar.DAY_OF_MONTH, 1); c.add(Calendar.DATE, i); result[i] = c.getTime(); } return result; }
Tips:
// 当前天所在周,周六 c.set(Calendar.DAY_OF_WEEK, c.getActualMaximum(Calendar.DAY_OF_WEEK)); // 当前天所在周,周一 c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); // 当前天所在周,周日 c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY); c.add(Calendar.DATE, 6); // 当前天所在月,第一天 c.set(Calendar.DAY_OF_MONTH,1); // 当前天所在月,月末 c.set(Calendar.DAY_OF_MONTH,c.getActualMaximum(Calendar.DAY_OF_MONTH)); // 当前天所在年,第一天 c.set(Calendar.DAY_OF_YEAR,1); // 当前天所在年,年末 c.set(Calendar.DAY_OF_YEAR,c.getActualMaximum(Calendar.DAY_OF_YEAR)); // 当前天,前一天 c.add(Calendar.DATE, -1); // 当前天,后一天 c.add(Calendar.DATE, 1); // 当前天所在月,上一月 c.set(Calendar.MONTH, c.get(Calendar.MONTH) - 1);