?? dateutil.java
字號:
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 + -