[java]代码库
import java.util.*;
import java.text.*;
import java.sql.Time;
public class DateFactory {
public DateFactory() {
}
/*
private static String[][] timeZoneList = null;
private static Object timeZoneLock = new Object();
public static final long SECOND = 1000;
public static final long MINUTE = 60 * SECOND;
public static final long HOUR = 60 * MINUTE;
public static final long DAY = 24 * HOUR;
public static final long WEEK = 7 * DAY;*/
private static Calendar str2Calendar(String date){
DateFormat df = DateFormat.getDateInstance();
try {
df.parse(date);
return df.getCalendar();
}catch (ParseException e) {
return null;
}
}
private static Calendar getCalendar(java.util.Date date){
Calendar cal = getCalendar();
cal.setTime(date);
return cal;
}
private static Calendar getCalendar(){
return Calendar.getInstance();
}
/**
* 字符型日期转化为 java.sql.Date 型
* @param date
* @param format 0:yyyy-MM-dd 1:yyyy-MM-dd HH:mm:ss
* @return
* @throws ParseException
*/
public static java.sql.Date str2date(String date, int format){
if (date == null) {
return null;
}
SimpleDateFormat sdf = null;
if (format == 0) { // 日期格式
sdf = new SimpleDateFormat("yyyy-MM-dd");
}else if(format==1){
sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
}
try {
java.util.Date dt = sdf.parse(date);
return new java.sql.Date(dt.getTime());
} catch (Exception e) {
return new java.sql.Date(str2Calendar(date).getTime().getTime());
}
}
/**
* 日期型转为字符串
* @param date
* @param format
* @return
*/
public static String date2str(java.util.Date date){
return new java.sql.Date(date.getTime()).toString();
}
/**
* 格式化时间
* @param date
* @param format
* @return
*/
public static String FormatDate(java.util.Date date, int format){
String fm = "";
if(format==0){
fm = "yyyy-MM-dd";
}else if(format==1){
fm = "yyyy-MM-dd HH:mm:ss";
}else if(format==2){
fm = "yyyyMMdd";
}else if(format==3){
fm = "yyyyMMddHHmmss";
}
return FormatDate(date, fm);
}
public static String FormatDate(java.util.Date date, String format){
java.text.SimpleDateFormat ft = new SimpleDateFormat(format);
return ft.format(date);
}
/**
* 取周几
* @param strDate 日期
* @return
*/
public static int getDateInWeek(String strDate) {
Calendar c = str2Calendar(strDate);
if(c!=null){
return c.get(c.DAY_OF_WEEK) - c.SUNDAY;
}else{
return -1;
}
}
/**
* 取当前日期
* @return
*/
public static String getDate() {
return FormatDate(getCalendar().getTime(), 0);
}
/**
* 取当前日期的年月日
* @style 表示单位的类型,1-年(YEAR),2-月(MONTH),3-日(DAY)
*/
public static String getDate(String style) {
return getYearMonthDate(getDate(), style)+"";
}
/**
* 取当前时间,排除年月日,只取时间
* @return
*/
public static String getTime() {
java.sql.Time time = new Time(getCalendar().getTime().getTime());
return time.toString();
}
/**
* 取当前时间,包括年月日时分秒
* @return
*/
public static String getSysTime() {
return getSysTime(1);
}
/**
* 取当前时间,格式自定义
* @param format
* @return 格式字符串
*/
public static String getSysTime(String format) {
return FormatDate(getCalendar().getTime(), format);
}
public static String getSysTime(int format) {
return FormatDate(getCalendar().getTime(), format);
}
/**
* 取格林时间 1970-01-01 08:00:00
* @return
*/
public static String getGMTTime(){
return FormatDate(new java.util.Date(0),1);
}
/**
* 取某一年某一月最后那一天日期,只包括年月日
* @param year 年
* @param month 月
* @return
*/
public static String getMaxDate(int year, int month) {
Calendar cal = getCalendar();
cal.set(year, month, getMonthDays(year, month));
return date2str(cal.getTime());
}
/**
* 取年的第一天,只包括年月日
* @param year
* @return
*/
public static String getYearFirstDay(int year){
Calendar cal = getCalendar();
cal.set(year, 0, 1);
return date2str(cal.getTime());
}
/**
* 取年的第一天,只包括年月日
* @param date 当前日期
* @return
*/
public static String getYearFirstDay(java.sql.Date date){
Calendar cal = getCalendar(date);
return getYearFirstDay(cal.get(Calendar.YEAR));
}
/**
* 取年的第一天,只包括年月日
* @param date 当前日期
* @return
*/
public static String getYearFirstDay(String date){
return getYearFirstDay(str2date(date,0));
}
/**
* 取年的最后一天,只包括年月日
* @return
*/
public static String getYearLastDay(){
return getYearLastDay(getDate());
}
public static String getYearLastDay(String date){
return getYearLastDay(str2date(date,0));
}
public static String getYearLastDay(java.sql.Date date){
Calendar cal = getCalendar(date);
int year = cal.get(Calendar.YEAR);
int month = cal.getActualMaximum(Calendar.MONTH);
cal.set(year, month, getMonthDays(year,month+1));
return date2str(cal.getTime());
}
/**
* 取某一年某一月的第一天,只包括年月日
* @param year 年
* @param month 月
* @return
*/
public static String getMonthFisrtDay(int year, int month) {
Calendar cal = getCalendar();
cal.set(year, month-1, 1);
return date2str(cal.getTime());
}
public static String getMonthFisrtDay(java.sql.Date date) {
Calendar cal = getCalendar(date);
cal.set(Calendar.DATE, 1);
return date2str(cal.getTime());
}
public static String getMonthFisrtDay(String date) {
return getMonthFisrtDay(str2date(date,0));
}
/**
* 取某一年某一月的总天数
* @param year 年
* @param month 月
* @return
*/
public static int getMonthDays(int year, int month) {
Calendar cal = getCalendar();
cal.set(year,month-1,1);
return cal.getActualMaximum(Calendar.DATE);
}
public static int getMonthDays(java.sql.Date date) {
Calendar cal = getCalendar(date);
return cal.getActualMaximum(Calendar.DATE);
}
public static int getMonthDays(String date) {
return getMonthDays(str2date(date,0));
}
/**
* 取某一年的天数
* @param date
* @return
*/
public static int getYearDays(String date) {
return getYearDays(str2date(date,0));
}
public static int getYearDays(java.sql.Date date) {
Calendar cal = getCalendar(date);
return cal.getActualMaximum(Calendar.DAY_OF_YEAR);
}
/**
* 取当前季度的开始的日期,只包括年月日
* @return
*/
public static String getThisQuarterBeginTime() {
Calendar cal = getCalendar();
int month = cal.get(Calendar.MONTH) + 1;
cal.set(Calendar.DATE, 1);
if(month>=1 && month<=3){
cal.set(Calendar.MONTH, 0);
return FormatDate(cal.getTime(), 1);
}else if(month>=4 && month <=6){
cal.set(Calendar.MONTH, 3);
return FormatDate(cal.getTime(), 1);
}else if(month>=7 && month <=9){
cal.set(Calendar.MONTH, 6);
return FormatDate(cal.getTime(), 1);
}else{
cal.set(Calendar.MONTH, 9);
return FormatDate(cal.getTime(), 1);
}
}
public static String getThisQuarterBeginTime(String date) {
return getThisQuarterBeginTime(str2date(date,1));
}
public static String getThisQuarterBeginTime(java.sql.Date date) {
Calendar cal = getCalendar(date);
int month = cal.get(Calendar.MONTH) + 1;
cal.set(Calendar.DATE, 1);
if(month>=1 && month<=3){
cal.set(Calendar.MONTH, 0);
return FormatDate(cal.getTime(), 1);
}else if(month>=4 && month <=6){
cal.set(Calendar.MONTH, 3);
return FormatDate(cal.getTime(), 1);
}else if(month>=7 && month <=9){
cal.set(Calendar.MONTH, 6);
return FormatDate(cal.getTime(), 1);
}else{
cal.set(Calendar.MONTH, 9);
return FormatDate(cal.getTime(), 1);
}
}
/**
* 取当前季度的结束的日期,只包括年月日
* @return
*/
public static String getThisQuarterEndTime() {
Calendar cal = getCalendar();
int month = cal.get(Calendar.MONTH) + 1;
cal.set(Calendar.DATE, getMonthDays(Calendar.YEAR, month));
if(month>=1 && month<=3){
cal.set(Calendar.MONTH, 2);
return FormatDate(cal.getTime(), 1);
}else if(month>=4 && month <=6){
cal.set(Calendar.MONTH, 5);
return FormatDate(cal.getTime(), 1);
}else if(month>=7 && month <=9){
cal.set(Calendar.MONTH, 8);
return FormatDate(cal.getTime(), 1);
}else{
cal.set(Calendar.MONTH, 11);
return FormatDate(cal.getTime(), 1);
}
}
public static String getThisQuarterEndTime(String date) {
return getThisQuarterEndTime(str2date(date,1));
}
public static String getThisQuarterEndTime(java.sql.Date date) {
Calendar cal = getCalendar(date);
int month = cal.get(Calendar.MONTH) + 1;
if(month>=1 && month<=3){
cal.set(Calendar.MONTH, 2);
cal.set(Calendar.DATE, getMonthDays(Calendar.YEAR, 3));
return FormatDate(cal.getTime(), 1);
}else if(month>=4 && month <=6){
cal.set(Calendar.MONTH, 5);
cal.set(Calendar.DATE, getMonthDays(Calendar.YEAR, 6));
return FormatDate(cal.getTime(), 1);
}else if(month>=7 && month <=9){
cal.set(Calendar.MONTH, 8);
cal.set(Calendar.DATE, getMonthDays(Calendar.YEAR, 9));
return FormatDate(cal.getTime(), 1);
}else{
cal.set(Calendar.MONTH, 11);
cal.set(Calendar.DATE, getMonthDays(Calendar.YEAR, 12));
return FormatDate(cal.getTime(), 1);
}
}
/**
* 取得时间格式
* @param date 日期
* @param style 类型 1-年(YEAR),2-月(MONTH),3-日(DAY)
* @return
*/
public static int getYearMonthDate(java.sql.Date date, String style) {
Calendar cal = getCalendar(date);
if (style.equalsIgnoreCase("YEAR") || style.equals("1")) {
return cal.get(Calendar.YEAR);
} else if (style.equalsIgnoreCase("MONTH") || style.equals("2")) {
return cal.get(Calendar.MONTH)+1;
} else if (style.equalsIgnoreCase("DAY") || style.equals("3")) {
return cal.get(Calendar.DATE);
} else {
return 0;
}
}
public static int getYearMonthDate(String date, String style) {
return getYearMonthDate(str2date(date,0), style);
}
/**
* 两时间间隔
* @param date1 开始日期
* @param date2 结束日期
* @param style 类型 1-年,2-月,3-日,4-周,5时,6分,7秒
* @return
*/
public static double DateDiff(java.sql.Date date1, java.sql.Date date2, String style) {
double i =0;
double mins = date2.getTime() - date1.getTime();
try {
if (style.equals("1")) {
i = (double)(mins/1000/24/3600/365); //不精确计算间隔年数
} else if (style.equals("2")) {
i = (double)(mins/1000/24/3600/30);//不精确计算间隔月数
} else if (style.equals("3")) {
i = (double)(mins/1000/24/3600);
} else if (style.equals("4")) {
i = (double)(mins/1000/24/3600/7);
} else if (style.equals("5")) {
i = (double)(mins/1000/3600);
} else if (style.equals("6")) {
i = (double)(mins/1000/60);
} else if (style.equals("7")) {
i = (double)(mins/1000);
} else {
i = mins;
}
return Tools.Format(i, 2);
} catch (Exception e) {
return 0;
}
}
public static double DateDiff(String date1, String date2, String style) throws Exception{
return DateDiff(str2date(date1,0),str2date(date2,0),style);
}
/**
* 将日期加上某些天或减去天数,返回字符串
* @param date 日期
* @param addvalue 增加的值
* @param style 类型 1-年,2-月,3-日,4-周,5时,6分,7秒
* @param format
* @return
*/
public static String DateAdd(java.sql.Date date, int addvalue, String style, int format) {
Calendar cal = getCalendar(date);
if (style.equals("1")) {
cal.add(Calendar.YEAR, addvalue);
} else if (style.equals("2")) {
cal.add(Calendar.MONTH, addvalue);
} else if (style.equals("3")) {
cal.add(Calendar.DATE, addvalue);
} else if (style.equals("4")) {
cal.add(Calendar.WEEK_OF_YEAR, addvalue);
} else if (style.equals("5")) {
cal.add(Calendar.HOUR, addvalue);
} else if (style.equals("6")) {
cal.add(Calendar.MINUTE, addvalue);
} else if (style.equals("7")) {
cal.add(Calendar.SECOND, addvalue);
}
return FormatDate(cal.getTime(),format);
}
public static String DateAdd(String date, int addvalue, String style, int format) {
return DateAdd(str2date(date,format), addvalue, style, format);
}
/**
* 字符型时间转化为 java.sql.Timestamp型
* @param datestr
* @return
* @throws ParseException
*/
public static java.sql.Timestamp str2time(String date)
throws ParseException {
return new java.sql.Timestamp(str2date(date,1).getTime());
}
/*
private static String date2time(String datestr) throws ParseException {
if (datestr == null) {
return "";
}
try {
java.sql.Date dt = java.sql.Date.valueOf(datestr);
java.sql.Timestamp time = new java.sql.Timestamp(dt.getTime());
return time.toString();
} catch (Exception e) {
return "";
}
}
private static String time2date(String datestr) throws ParseException {
if (datestr == null) {
return "";
}
try {
java.sql.Timestamp time = java.sql.Timestamp.valueOf(datestr);
java.sql.Date dt = new java.sql.Date(time.getTime());
return dt.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
}
}*/
/**
* 前半年或后半年的第一天,只包括年月日
* @param date 日期
* @return
*/
public static String getHalfYearFirstDay(java.sql.Date date) {
Calendar cal = getCalendar(date);
int month = cal.get(Calendar.MONTH);
cal.set(Calendar.MONTH, month>=6?6:0);
cal.set(Calendar.DATE, 1);
return FormatDate(cal.getTime(), 0);
}
public static String getHalfYearFirstDay(String date) {
return getHalfYearFirstDay(str2date(date,0));
}
/**
* 前半年或后半年的最后一天,只包括年月日
* @param date 日期
* @return
*/
public static String getHalfYearLastDay(java.sql.Date date) {
Calendar cal = getCalendar(date);
int month = cal.get(Calendar.MONTH);
cal.set(Calendar.MONTH, month>=6?11:5);
cal.set(Calendar.DATE, getMonthDays(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1));
return FormatDate(cal.getTime(), 0);
}
public static String getHalfYearLastDay(String date) {
return getHalfYearLastDay(str2date(date,0));
}
/**
* 取月的第一天,只包括年月日
* @param date 日期
* @return
*/
public static String getMonthFirstDay(java.sql.Date date){
Calendar cal = getCalendar(date);
cal.set(Calendar.DATE, 1);
return FormatDate(cal.getTime(), 0);
}
public static String getMonthFirstDay(String date){
return getMonthFirstDay(str2date(date, 0));
}
/**
* 取月的最后一天,只包括年月日
* @param date 日期
* @return
*/
public static String getMonthLastDay(java.sql.Date date) {
Calendar cal = getCalendar(date);
cal.set(Calendar.DATE, getMonthDays(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1));
return FormatDate(cal.getTime(), 0);
}
public static String getMonthLastDay(String date) {
return getMonthLastDay(str2date(date,0));
}
/**
* 是否月的最后一天
* @param date 日期
* @return
*/
public static boolean isMonthLastDay(java.sql.Date date) {
Calendar cal = getCalendar(date);
int dt = cal.get(Calendar.DATE);
int days = getMonthDays(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
return dt==days;
}
public static boolean isMonthLastDay(String date) {
return isMonthLastDay(str2date(date,0));
}
/**
* 是否月的第一天
* @param date 日期
* @return
*/
public static boolean isMonthFisrtDay(java.sql.Date date) {
Calendar cal = getCalendar(date);
int dt = cal.get(Calendar.DATE);
return dt==1;
}
public static boolean isMonthFisrtDay(String date) {
return isMonthFisrtDay(str2date(date,0));
}
/**
* 返回时间戳
* @return
*/
public static java.sql.Timestamp getTimestamp() {
return getTimestamp(getSysTime());
}
public static java.sql.Timestamp getTimestamp(String date) {
return new java.sql.Timestamp(str2date(date,1).getTime());
}
public static void main(String[] args) {
try{
System.out.print(DateFactory.isMonthFisrtDay("2012-2-1 11:11:11"));
}catch(Exception e){}
}
}