首页>代码>java面向对象思想算出当前阳历日期的下一秒>/nextsecond/src/lyj/next/second/NextSecond.java
package lyj.next.second;

import java.io.Serializable;
import java.util.Calendar;

public class NextSecond {
	private static final int BIGMONTH = 0X00001F;
	private static final int SMALLMONTH = 0X00001E;
	private static final int BIGFEBRUAY = 0X00001D;
	private static final int SMALLFEBRUAY = 0X00001C;

	/**
	 * 获取指定日期的下一秒
	 * 
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static String nextSeconds(Date date) throws Exception {
		if (!Date.checkUseFul(date)) {
			throw new Exception("日期不合法");

		}

		return Date.nextDateInOneSecond(date);
	}

	// 主程序入口
	public static void main(String[] args) throws Exception {
		Date dt = new Date(2012, 10, 31, 23, 59, 59);
		System.out.println(nextSeconds(dt));
	}

	// 日期内

	static class Date implements Serializable {

		private static final long serialVersionUID = -8876583691388432561L;
		int year, month, day, hour, minute, second;
		private static boolean needAddMinute = Boolean.FALSE;// 是否需要累加月
		private static boolean needAddHour = Boolean.FALSE;// 是否需要累加小时
		private static boolean needAddDay = Boolean.FALSE;// 是否需要累加天数
		private static boolean needAddMonth = Boolean.FALSE;// 是否需要累加月数
		private static boolean needAddYear = Boolean.FALSE;// 是否需要累加年份

		private Date(int year, int month, int day, int hour, int minute,
				int second) {
			this.year = year;
			this.month = month;
			this.day = day;
			this.hour = hour;
			this.minute = minute;
			this.second = second;
		}

		private Date(Calendar cand) {

			this.year = cand.get(Calendar.YEAR);
			this.month = cand.get(Calendar.MONTH);
			this.day = cand.get(Calendar.DATE);
			this.hour = cand.get(Calendar.HOUR);
			this.minute = cand.get(Calendar.MINUTE);
			this.second = cand.get(Calendar.SECOND);
		}

		private int getMonth() {
			return month;
		}

		private int getYear() {
			return year;
		}

		private int getDay() {
			return day;
		}

		private int getHour() {
			return hour;
		}

		private int getMinute() {
			return minute;
		}

		private int getSecond() {
			return second;
		}

		private void setYear(int year) {
			this.year = year;
		}

		private void setMonth(int month) {
			this.month = month;
		}

		private void setDay(int day) {
			this.day = day;
		}

		private void setHour(int hour) {
			this.hour = hour;
		}

		private void setMinute(int minute) {
			this.minute = minute;
		}

		private void setSecond(int second) {
			this.second = second;
		}

		/**
		 * 检测日期是否为有效日期 默认有效返回yes无效返回no
		 * 
		 * @param date
		 * @return
		 */
		private static boolean checkUseFul(Date date) {
			boolean[] useful = new boolean[6];
			useful[0] = date.year >= 1;// 年份必须从整数1开始
			useful[1] = date.month >= 1 && date.month <= 12;// 月份控制在1到12月之间
			useful[2] = date.day >= 1 && date.day <= days(date);// 天数必须<=这个月的最大日期
			useful[3] = date.hour >= 1 && date.hour <= 23;// 小时为24小时制
			useful[4] = date.minute >= 1 && date.minute <= 59;
			useful[5] = date.second >= 0 && date.second <= 59;
			for (boolean b : useful) {
				if (!b)
					return false;
			}
			return true;
		}

		/**
		 * 判断指定日期的年份是否属于润年
		 * 
		 * @param date
		 * @return
		 */
		private static boolean isLeapYear(final Date date) {
			final int year = date.getMonth();
			return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));

		}

		/**
		 * 
		 * @param date
		 * @return 指定日期的这个月有多少天
		 */
		private static int days(final Date date) {
			int daycount;
			final int month = date.getMonth();
			switch (month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				return BIGMONTH;
			case 2:
				daycount = (isLeapYear(date)) ? BIGFEBRUAY : SMALLFEBRUAY;
				return daycount;

			default:
				return SMALLMONTH;

			}
		}

		/**
		 * 判断当前秒是否大于59如果是则分要加1
		 * 
		 * @return
		 */
		private static void nextSecond(final Date date) {
			int second = date.getSecond();
			date.setSecond((second == 59) ? 00 : second + 1);
			needAddMinute = (second == 59);
		}

		/**
		 * 判断当前分是否为59如果是则小时要加1
		 * 
		 * @return
		 */
		private static void nextMinute(final Date date) {
			int minute = date.getMinute();
			minute = needAddMinute ? minute + 1 : minute;
			date.setMinute(minute == 60 ? 00 : minute);
			needAddHour = (minute == 60);

		}

		/**
		 * 判断当前小时是否到了24如果是则天数要加1
		 * 
		 * @return
		 */
		private static void nextHour(final Date date) {
			int hour = date.getHour();
			hour = needAddHour ? hour + 1 : hour;
			date.setHour(hour == 24 ? 00 : hour);
			needAddDay = (hour == 24);
		}

		/**
		 * 判断当前天数是否到了当月的最后一天如果是则月数要加1
		 * 
		 * @return
		 */
		private static void nextDays(final Date date) {
			final int daycount = days(date);
			int day = date.getDay();
			day = needAddDay ? day + 1 : day;
			date.setDay(day == daycount + 1 ? 1 : day);
			needAddMonth = (day == daycount + 1);
		}

		/**
		 * 判断当前月份是否到了12月如果是则年份要加1
		 * 
		 * @return
		 */
		private static void nextMonth(final Date date) {
			int month = date.getMonth();
			month = needAddMonth ? month + 1 : month;
			date.setMonth(month == 13 ? 1 : month);
			needAddYear = (month == 13);
		}

		private static void nextYear(final Date date) {
			int year = date.getYear();
			year = needAddYear ? year + 1 : year;
			date.setYear(year);

		}

		static String toString(final Date date) {
			String string = date.getYear() + "年" + date.getMonth() + "月"
					+ date.getDay();
			string += "日 " + date.getHour() + "时" + date.getMinute() + "分"
					+ date.getSecond() + "秒";
			return string;
		}

		static String nextDateInOneSecond(final Date date) {
			String currDate = "当前日期为:" + toString(date) + "\n";
			nextSecond(date);
			nextMinute(date);
			nextHour(date);
			nextDays(date);
			nextMonth(date);
			nextYear(date);
			return currDate + "下一秒为:" + toString(date);

		}

	}

}
最近下载更多
836776364  LV2 2015年10月31日
Silence  LV5 2012年11月11日
最代码官方  LV167 2012年11月11日
最近浏览更多
阿道夫进口海尔 2023年5月3日
暂无贡献等级
刘孟彪  LV9 2020年12月30日
moomin709  LV24 2020年6月24日
wei112233  LV15 2020年4月21日
flygrass  LV12 2018年12月5日
shanhudao 2018年2月12日
暂无贡献等级
honorlying  LV3 2017年10月26日
happylife135 2017年5月24日
暂无贡献等级
ygsummary  LV7 2016年3月12日
zhangV5  LV3 2015年12月25日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友