一个完整的日期实现类
package com.nyhr.util;
import java.util.GregorianCalendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Calendar;
/**
* <p>Title: 日期时间处理</p>
* <p>Description: 工具类</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>
* @version 1.0
* @author
*/
public class DateUtil
{
/**
* 缺省的DateFormat对象,可以将一个java.util.Date格式化成yyyy-mm-dd输出
*/
private static DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
/**
* 私有构造函数
*/
private DateUtil()
{
}
/**
* <p>返回一个当前的时间,并按格式转换为字符串</p>
* 例:17:27:03
* @return String
*/
public static String getNowTime()
{
GregorianCalendar gcNow = new GregorianCalendar();
java.util.Date dNow = gcNow.getTime();
DateFormat df = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
return df.format(dNow);
}
/**
* <p>返回一个当前日期,并按格式转换为字符串</p>
* 例:2004-4-30
* @return String
*/
public static String getNowDate()
{
GregorianCalendar gcNow = new GregorianCalendar();
java.util.Date dNow = gcNow.getTime();
DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
return df.format(dNow);
}
/**
* <p>返回一个当前日期和时间,并按格式转换为字符串</p>
* 例:2004-4-30 17:27:03
* @return String
*/
public static String getNowDateTime()
{
GregorianCalendar gcNow = new GregorianCalendar();
java.util.Date dNow = gcNow.getTime();
DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.SIMPLIFIED_CHINESE);
return df.format(dNow);
}
/**
* <p>返回当前年</p>
* @return int
*/
public static int getThisYear()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.YEAR);
}
/**
* 返回本月
* @return int
*/
public static int getThisMonth()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.MONTH) + 1;
}
/**
* 返回今天是本月的第几天
* @return int 从1开始
*/
public static int getToDayOfMonth()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.DAY_OF_MONTH);
}
/**
* 返回当前的小时
* @return int
*/
public static int getHour()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.HOUR);
}
/**
* 返回当前的分钟
* @return int 返回当前的分钟
*/
public static int getMinute()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.MINUTE);
}
/**
* 返回当前的秒数
* @return int 第几秒
*/
public static int getSecond()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.SECOND);
}
/**
* 返回今天是本年的第几周
* @return int 从1开始
*/
public static int getToWeekOfYear()
{
GregorianCalendar gcNow = new GregorianCalendar();
return gcNow.get(GregorianCalendar.WEEK_OF_YEAR);
}
/**
* 返回一格式化的日期
* @param time long
* @return String yyyy-mm-dd 格式
*/
public static String formatDate(java.util.Date date)
{
if (date == null) return "";
else return df.format(date);
}
/**
* 返回一格式化的日期
* @param time long
* @return String 2005-6-17 格式
*/
public static String formatSDate(java.util.Date date)
{
if (date == null) return "";
else
{
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
return bartDateFormat.format(date);
}
}
/**
* 返回已添加指定时间间隔的日期
* @param interval 表示要添加的时间间隔("y":年;"d":天;"m":月;如有必要可以自行增加)
* @param number 表示要添加的时间间隔的个数
* @param date java.util.Date()
* @return String 2005-5-12格式的日期字串
*/
public static String dateAdd(String interval, int number,
java.util.Date date)
{
String strTmp = "";
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(date);
//加若干年
if (interval.equals("y"))
{
int currYear = gc.get(Calendar.YEAR);
gc.set(Calendar.YEAR, currYear + number);
}
//加若干月
else if (interval.equals("m"))
{
int currMonth = gc.get(Calendar.MONTH);
gc.set(Calendar.MONTH, currMonth + number);
}
//加若干天
else if (interval.equals("d"))
{
int currDay = gc.get(Calendar.DATE);
gc.set(Calendar.DATE, currDay + number);
}
//加若小时
else if (interval.equals("h"))
{
int currDay = gc.get(Calendar.HOUR);
gc.set(Calendar.HOUR, currDay + number);
}
SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-M-d HH:mm:ss");
strTmp = bartDateFormat.format(gc.getTime());
return strTmp;
}
/**
* <p>返回两个日期之间的单位间隔数</p>
* @param a java.util.Date
* @param b java.util.Date
* @return int 间隔数
*/
public static int dateDiff(java.util.Date a, java.util.Date b)
{
int tempDifference = 0;
int difference = 0;
Calendar earlier = Calendar.getInstance();
Calendar later = Calendar.getInstance();
if (a.compareTo(b) < 0)
{
earlier.setTime(a);
later.setTime(b);
}
else
{
earlier.setTime(b);
later.setTime(a);
}
while (earlier.get(Calendar.YEAR) != later.get(Calendar.YEAR))
{
tempDifference = 365 * (later.get(Calendar.YEAR) - earlier.get(Calendar.YEAR));
difference += tempDifference;
earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
}
if (earlier.get(Calendar.DAY_OF_YEAR) != later.get(Calendar.DAY_OF_YEAR))
{
tempDifference = later.get(Calendar.DAY_OF_YEAR)
- earlier.get(Calendar.DAY_OF_YEAR);
difference += tempDifference;
earlier.add(Calendar.DAY_OF_YEAR, tempDifference);
}
return difference;
}
/**
* <p>该方法是获得到每月1号星期一的数据</p>
* @return -返回一个数字
*/
/**
* <p>该方法是获得到每月1号星期一的数据</p>
* @return -返回一个数字
*/
public static int getDate(int curYear,int curMonth ,int curDate)
{
int day1 = 0;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(curYear,curMonth-1,curDate);
int dayOfWeek = cal.get(cal.DAY_OF_WEEK);
System.out.println("curDate="+curDate +" dayOfWeek "+dayOfWeek);
switch(dayOfWeek)
{
case 1: //星期天
day1=0;
break;
case 2: //星期一
day1=1;
break;
case 3: //星期二
day1=2;
break;
case 4: //星期三
day1=3;
break;
case 5: //星期四
day1=4;
break;
case 6: //星期五
day1=5;
break;
case 7: //星期六
day1=6;
break;
}
return day1;
}
public static String checkTime(int id)
{
String bol = "";
Calendar tt = Calendar.getInstance();
String currDate=getNowDate();
System.out.println("currDate="+currDate);
int result = tt.get(Calendar.DAY_OF_WEEK);
int shour = tt.get(Calendar.HOUR_OF_DAY);
if (id == 3)
{
switch (result)
{
case 1:
break;
case 7:
if ((shour >= 8) && (shour < 12))
{
bol = "disabled";
break;
}
default:
if ((shour >= 8) && (shour < 12))
{
bol = "disabled";
break;
}
else if ((shour >= 14) && (shour < 17))
{
bol = "disabled";
break;
}
}
}
return bol;
}
/**
* <p>该方法是将字符型转变成日期型</p>
* @param strX -传入字符类型
* @return -返回日期类型
*/
public static Date getStrDate(String strX)
{
Date date1=new Date();
if (!strX.equals(""))
{
try
{
date1=(DateFormat.getDateInstance()).parse(strX);
}
catch(Exception ex)
{
Debug.log("hhh","getStrDate()",ex.toString());
//System.out.println(ex.toString());
}
}
else
{
GregorianCalendar gcNow = new GregorianCalendar();
date1 = gcNow.getTime();
}
return date1;
}
/**
* <p>比较两日期字符串的大小</p>
* @param d1
* @param d2
* @return (d1>d2)?2:(d1=d2)?1:0
*/
public static int compareDate(String d1, String d2)
{
short vl = 1;
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(getStrDate(d1));
int year = gc.get(GregorianCalendar.YEAR);
int month = gc.get(GregorianCalendar.MONTH);
int day = gc.get(GregorianCalendar.DAY_OF_MONTH);
gc.setTime(getStrDate(d2));
int tempYear = gc.get(GregorianCalendar.YEAR);
int tempMonth = gc.get(GregorianCalendar.MONTH);
int tempDay = gc.get(GregorianCalendar.DAY_OF_MONTH);
if(year != tempYear)
{
if (year>tempYear)
vl = 2;
else
vl = 0;
}
else
{
if (month != tempMonth)
{
if(month>tempMonth)
vl = 2;
else
vl = 0;
}
else
{
if (day != tempDay)
{
if (day > tempDay)
vl = 2;
else
vl = 0;
}
}
}
return vl;
}
}