package
com.chinajavaworld.util;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
publicclass DateTimeUtil
{
privatestaticfinalint MAJOR_VERSION = 1;
privatestaticfinalint MINOR_VERSION = 0;
privatestaticfinalint REVISION_VERSION = 1;
privatestaticfinal String showFormat = "yyyy-MM-dd HH:mm:ss";
privatestaticfinal String storeFormat = "yyyyMMddHHmmssSSS";
privatestaticfinal SimpleDateFormat showFormater = new SimpleDateFormat(showFormat);
privatestaticfinal SimpleDateFormat storeFormater = new SimpleDateFormat(storeFormat);
privatestaticfinal SimpleDateFormat formatter1 = new SimpleDateFormat ("yyyy年MM月dd日 HH时mm分ss秒");
privatestaticfinal SimpleDateFormat formatter2 = new SimpleDateFormat ("yyyy年MM月dd日");
privatestaticfinal SimpleDateFormat formatter3 = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
privatestaticfinal SimpleDateFormat formatter4 = new SimpleDateFormat ("yyyy-MM-dd");
privatestaticfinal SimpleDateFormat formatter5 = new SimpleDateFormat ("yyyy/MM/dd");
privatestaticfinal SimpleDateFormat formatter6 = new SimpleDateFormat ("MM-dd HH:mm");
public DateTimeUtil()
{
}
publicstatic String getNow()
{
return storeFormater.format(new Date());
}
publicstatic String getRelativeDate(int days)
{
Calendar c = Calendar.getInstance();
c.set(5, c.get(5) + days);
StringBuffer sb = new StringBuffer(17);
sb.append(c.get(1));
int tmp[] = {
c.get(2) + 1, c.get(5), c.get(11), c.get(12), c.get(13), c.get(14)
};
for(int i = 0; i < tmp.length - 1; i++)
sb.append(tmp[i] >= 10 ? "" : "0").append(tmp[i]);
if(tmp[tmp.length - 1] < 10)
sb.append("0");
if(tmp[tmp.length - 1] < 100)
sb.append("0");
sb.append(tmp[tmp.length - 1]);
return sb.toString();
}
publicstatic String getNow(String string)
{
return (new SimpleDateFormat(string)).format(new Date());
}
publicstatic String getDisplayTime(long time)
{
return showFormater.format(new Date(time));
}
publicstatic String getDisplayTime(long time, String string)
{
return (new SimpleDateFormat(string)).format(new Date(time));
}
publicstatic String getShowFormat(String time)
{
try
{
if(time == null || time.equals(""))
time = showFormater.format(new Date());
else
time = showFormater.format(storeFormater.parse(time));
}
catch(ParseException e)
{
e.printStackTrace();
}
return time;
}
publicstaticlong compare(String t1, String t2)
{
return Long.valueOf(t1).longValue() - Long.valueOf(t2).longValue();
}
publicstaticint getYear(String time)
{
return Integer.valueOf(time.substring(0, 4)).intValue();
}
publicstaticint getMonth(String time)
{
return Integer.valueOf(time.substring(4, 6)).intValue();
}
publicstaticint getDate(String time)
{
return Integer.valueOf(time.substring(6, 8)).intValue();
}
publicstaticint getHour(String time)
{
return Integer.valueOf(time.substring(8, 10)).intValue();
}
publicstaticint getMinute(String time)
{
return Integer.valueOf(time.substring(10, 12)).intValue();
}
publicstaticint getSecond(String time)
{
return Integer.valueOf(time.substring(12, 14)).intValue();
}
publicstaticint getMilliSencond(String time)
{
return Integer.valueOf(time.substring(14, 17)).intValue();
}
publicstaticlong getTimeStamp(String time)
throws ParseException
{
return storeFormater.parse(time).getTime();
}
publicstatic String formatDate1(Date myDate) {
return formatter1.format(myDate);
}
publicstatic String formatDate2(Date myDate) {
return formatter2.format(myDate);
}
publicstatic String formatDate3(Date myDate) {
return formatter3.format(myDate);
}
publicstatic String formatDate4(Date myDate) {
return formatter4.format(myDate);
}
publicstatic String formatDate5(Date myDate) {
return formatter5.format(myDate);
}
publicstatic String formatDate6(Date myDate) {
return formatter6.format(myDate);
}
publicstaticlong getLongTime(String time){
try{
return showFormater.parse(time).getTime();
}
catch (ParseException ex) {
return 0L;
}
}
publicstatic String getVersion()
{
return"1.0.1";
}
publicstaticint getMajor()
{
return 1;
}
publicstaticint getMinor()
{
return 0;
}
publicstaticint getRevision()
{
return 1;
}
}
posted on 2006-10-11 17:15
无声 阅读(656)
评论(1) 编辑 收藏 所属分类:
java共享代码