⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dateutil.java

📁 该程序能够准确的记录互联网用户上网所用的流量
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.briup.common.util;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
 * <p>Title: FormatDate.java</p>
 * <p>Description<p>格式化日期类</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: briup</p>
 * @author terryren by 2008-2-20
 * @version 1.0 
 */
public class DateUtil extends Date {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
	* 年
	*/
	public int year;
	/**
	 * 月
	 */
	public int month;
	/**
	 * 日
	 */
	public int day;
	/**
	 * 小时
	 */
	public int hours;
	/**
	 * 分钟
	 */
	public int minutes;
	/**
	 * 秒
	 */
	public int seconds;
	private DateUtil() {
		super();
	}
	
	/**
     * 构造函数
     * @param 字符串类型的日期(必须符合日期的格式)
     */
	@SuppressWarnings("deprecation")
	public DateUtil(String date) {
		super(date);
	}

	/**
	 * 构造函数
	 * @param year
	 * @param month
	 * @param day
	 */
	@SuppressWarnings("deprecation")
	public DateUtil(int year, int month, int day) {
		super(year, month, day);
	}

	/**
	 * 构造函数
	 * @param year
	 * @param month
	 * @param day
	 * @param hours
	 * @param minutes
	 * @param seconds
	 */
	@SuppressWarnings("deprecation")
	public DateUtil(
		int year,
		int month,
		int day,
		int hours,
		int minutes,
		int seconds) {
		super(year, month, day, hours, minutes, seconds);
	}

	/**
	 * 得到今天的时间
	 * @return 今天的时间
	 */
	@SuppressWarnings("deprecation")
	public DateUtil today() {
		int y = super.getYear();
		int m = super.getMonth();
		int d = super.getDate();
		int h = super.getHours();
		int mi = super.getMinutes();
		int s = super.getSeconds();
		DateUtil id = new DateUtil(y, m, d, h, mi, s);
		return id;
	}

	/**
	 * 得到昨天的时间
	 * @return 昨天的时间
	 */
	@SuppressWarnings("deprecation")
	public DateUtil yesterday() {
		int y = super.getYear();
		int m = super.getMonth();
		int d = super.getDate();
		d = d - 1;
		DateUtil id = new DateUtil(y, m, d);
		return id;
	}

	/**
	 * 得到制定格式的时间;(如:yyyy-MM-dd HH:mm:ss)
	 * @return 返回时间 (yyyy-MM-dd HH:mm:ss)
	 */
	public String toString() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		StringBuffer tb = new StringBuffer();
		return sdf.format(this, tb, new FieldPosition(0)).toString();

	}

	/**
	 * 得到制定格式的时间;
	 * @return 返回时间
	 */
	public String toString(String style) {
		SimpleDateFormat sdf = new SimpleDateFormat(style);
		StringBuffer tb = new StringBuffer();
		return sdf.format(this, tb, new FieldPosition(0)).toString();

	}

	/**
	 * 得到制定格式的时间;(如:yyyy-MM-dd HH:mm:ss or yyyy-MM-dd)
	 * @param if(isTime) 时间精确到秒(yyyy-MM-dd HH:mm:ss);
	 * @param else 时间精确到日(yyyy-MM-dd)
	 * @return 返回时间 (yyyy-MM-dd HH:mm:ss) 或 (yyyy-MM-dd)
	 */

	public String toString(boolean isTime) {
		SimpleDateFormat sdf;
		if (isTime) {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		} else {
			sdf = new SimpleDateFormat("yyyy-MM-dd");
		}
		StringBuffer tb = new StringBuffer();
		return sdf.format(this, tb, new FieldPosition(0)).toString();

	}

	/**
	 * 对时间进行格式化,只有长度为10位(到日)或19位(到秒)的才返回值,其余的返回为空;
	 * @param dateString 时间类型.有两种(一:10位2002-12-10;二:19位2002-12-10 12:24:03)
	 * @return 返回格式化后的时间.有两种(一:20021210;二:20021210122403)
	 */
	public static String getFmtString(String dateString) {
		String outDate = "";
		String y;
		String m;
		String d;
		String h;
		String mi;
		String s;
		if (dateString == null)
			return outDate;
		if (dateString.length() == 10 || dateString.length() == 19) {
			y = dateString.substring(0, 4);
			m = dateString.substring(5, 7);
			d = dateString.substring(8, 10);
			outDate = y + m + d;
			if (dateString.length() > 10) {
				h = dateString.substring(11, 13);
				mi = dateString.substring(14, 16);
				s = dateString.substring(17, 19);
				outDate += h + mi + s;
			}
		}
		return outDate;
	}

	/**
	 * 把时间转化为定制的格式(如:2002-11-19,2002-11-20 24:12:30)
	 * @param outputDate 需要进行转化的时间.有两种(一:8位20021119;二:14位20021120241230)
	 * @return 返回定制的时间格式,有两种(一:2002-11-19;二:2002-11-20 24:12:30)
	 */
	public static String getDateOutput(String outputDate) {
		String outDate = "";
		if (outputDate == null)
			return outDate;
		if (outputDate.trim().length() >= 8) {
			String year = outputDate.substring(0, 4);
			String month = outputDate.substring(4, 6);
			String day = outputDate.substring(6, 8);
			outDate = year + "-" + month + "-" + day;
			if (outputDate.trim().length() > 8) {
				String hour = outputDate.substring(8, 10);
				String minute = outputDate.substring(10, 12);
				outDate += " " + hour + ":" + minute;
				if (outputDate.trim().length() > 12) {
					String second = outputDate.substring(12, 14);
					outDate += ":" + second;
				}
			}
		}
		return outDate;
	}

	/**
	 * 得到当前的时间,只到日.
	 * @param outputDate 需要进行转化的时间
	 * @param style 日期中间的样式 如"-","/"
	 * @return 返回定制的时间格式 (如:2003/01/09)
	 */
	public static String getDateOut(String outputDate, String style) {
		String outDate = "";
		if (outputDate == null)
			return outDate;
		if (outputDate.trim().length() >= 8) {
			String year = outputDate.substring(0, 4);
			String month = outputDate.substring(4, 6);
			String day = outputDate.substring(6, 8);
			outDate = year + style + month + style + day;
		}
		return outDate;
	}

	/**
	 * 把时间转化为定制的格式(如:2002年11月19日)
	 * @param outputDate需要进行转化的时间(如:20021119)
	 * @return 返回定制的时间格式(如:2002年11月19日)
	 */
	public static String getDateOutputChn(String outputDate) {
		String outDate = "";
		if (outputDate == null)
			return outDate;
		if (outputDate.trim().length() >= 8) {
			String year = outputDate.substring(0, 4);
			String month = outputDate.substring(4, 6);
			String day = outputDate.substring(6, 8);
			outDate = year + "��" + month + "��" + day + "��";
		}
		return outDate;
	}

	/**
	 * 把时间转化为定制的格式(如:2002-11-19,2002-11-20 24:12:30)
	 * @param outputDate 需要进行转化的时间.有两种(一:8位20021119;二:14位20021120241230)
	 * @return 返回定制的时间格式,有两种(一:2002年11月19日;二:2002年11月24日 24时12分30秒)
	 */
	public static String getDateOutputChnMore(String outputDate) {
		String outDate = "";
		if (outputDate == null)
			return outDate;
		if (outputDate.trim().length() >= 8) {
			String year = outputDate.substring(0, 4);
			String month = outputDate.substring(4, 6);
			String day = outputDate.substring(6, 8);
			outDate = year + "��" + month + "��" + day + "��";
			if (outputDate.trim().length() > 8) {
				String hour = outputDate.substring(8, 10);
				String minute = outputDate.substring(10, 12);
				outDate += " " + hour + "ʱ" + minute + "��";
			}
		}
		return outDate;
	}

	/**
	 * 年份选择下拉框
	 * @param startYear 开始年份
	 * @param endYear 结束年份
	 * @return 下拉框形式的String
	 */
	public static String toHtmlSelect(int startYear, int endYear) {
		String options = "";
		int length = endYear - startYear;
		if (length < 0) {
			length = 0;
		}
		for (int i = 0; i < length; i++) {
			options += "<option ";
			options += " value='" + startYear + "' >";
			startYear++;
			options += "" + startYear;
			options += "</option>\n";
		}
		return options;
	}

	/**
	 * 显示小时
	 * @param hour 传过来的时间参数
	 * @return String
	 */
	public static String toHtmlSelectWithHour(int hour) {
		String options = "";
		for (int i = 0; i <= 23; i++) {
			if (i < 10) {
				options += "<option value='0" + i + "'";
			} else {
				options += "<option value='" + i + "'";
			}
			if (i == hour) {
				options += " selected ";
			}
			options += ">";
			if (i < 10) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -