日期:2014-05-20 浏览次数:20840 次
package com.sun.image.codec.jpeg; /** * JPEGImageEncoder Interface * * JPEGImageEncoder compresses images into JPEG data streams and * writes the JPEG stream to an OutputStream ParseException { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = sdf.parse("2009-10-27 00:00:00"); TenDayScope tenDay = TenDayScope.getInstance(); System.out.println(tenDay.contains(date)); } } class TenDayScope { public final static int FIRST_TEN_DAY = 0; public final static int SECOND_TEN_DAY = 1; public final static int THIRD_TEN_DAY = 2; private Calendar start; private Calendar end; private TenDayScope() { init(); } public Calendar getStart() { return (Calendar)start.clone(); } public Calendar getEnd() { return (Calendar)end.clone(); } public static TenDayScope getInstance() { return new TenDayScope(); } /** * 判断 Date 是否在本旬以及下旬之间 * @param date * @return * 2009-10-27 下午01:42:07 */ public boolean contains(Date date) { if(date == null) { return false; } return contains(date.getTime()); } /** * 判断 Calendar 是否在本旬以及下旬之间 * @param date * @return * 2009-10-27 下午01:42:07 */ public boolean contains(Calendar calendar) { if(calendar == null) { return false; } return contains(calendar.getTimeInMillis()); } /** * 判断 Unix 纪元毫秒数是否在本旬以及下旬之间 * @param date * @return * 2009-10-27 下午01:42:07 */ public boolean contains(long millis) { return (millis >= start.getTimeInMillis()) && (millis < end.getTimeInMillis()); } private void init() { { start = Calendar.getInstance(); int t = getTenDay(start.get(Calendar.DATE)); start.set(Calendar.DATE, getStartTenDay(t)); clearHms(start); } { end = Calendar.getInstance(); int t = getTenDay(end.get(Calendar.DATE)); int endDay = getEndTenNextDay(t); if(t > FIRST_TEN_DAY) { end.add(Calendar.MONTH, 1); } end.set(Calendar.DATE, getEndTenNextDay(endDay)); clearHms(end); } } /** * 清空 Calendar 日以下的字段 * @param c * 2009-10-27 下午01:41:39 */ private void clearHms(Calendar c) { c.set(Calendar.HOUR_OF_DAY, 0); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); } /** * 根据当前旬次,获得该旬的第一天日 * * @param currentTenDay * @return * 2009-10-27 下午01:40:56 */ private int getStartTenDay(int currentTenDay) { return currentTenDay * 10 + 1; } /** * 根据当前旬次,获得下旬最后一天的后一天 * @param currentTenDay * @return * 2009-10-27 下午01:41:18 */ private int getEndTenNextDay(int currentTenDay) { return (currentTenDay + 2) % 3 * 10 + 1; } /** * 根据日期获得旬次 * @param day * 2009-10-27 下午01:40:28 */ private int getTenDay(int day) { int t = (day - 1) / 10; if(t > THIRD_TEN_DAY) { t = THIRD_TEN_DAY; } return t; } public String toString() { return String.format("start: %tF %<tT, end: %tF %<tT", start, end); } }