Ø 描述: 封装了对一些对日期常见的操作方法。
Ø 类ID: DateUtils
Ø 方法:
方法名 | 描述 |
|
|
getToday( ) | 得到当前日期字符串 |
On Entry: |
|
On Exit: | return 当前日期 按"yyyy-MM-dd"格式获取 |
format(Date) | 将日期型转换为字符型.转换后的日期格式为yyyy-MM-dd |
On Entry: | Date: 如: Tue Jan 01 00:09:00 CST 2002
|
On Exit: | return String 返回字符型日期 |
getTime() | 获得当前时间.显示格式如:2008-07-18 10:21:41 |
On Entry: |
|
On Exit: | return String返回当前时间字符串 |
getTimeNoSeparate( ) | 获得当前时间.显示格式如:20080826102109一般用于作为新建文件夹或者文件名的一部分 |
On Entry: |
|
On Exit: | return String返回当前时间字符串 |
getNow() | 得到当前日期,格式如:20080825 |
On Entry: |
|
On Exit: | return String返回当前日期字符串 |
format(String) | 将字符串转换成Date型如:2008-08-25 to Wed Aug 24 00:00:00 CST 2005 |
On Entry: |
|
On Exit: | return Date返回Date型日期. |
Ø 代码:
public class DateUtils
{
public DateUtils()
{
}
public static DateUtils getInstance()
{
return dateUtils;
}
public String format(Date date)
{
String format = "yyyy-MM-dd";
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(date);
}
private String format(Date date, String format)
{
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.format(date);
}
private Date parse(String date, String format)
throws ParseException
{
SimpleDateFormat fmt = new SimpleDateFormat(format);
return fmt.parse(date);
}
public String getToday()
{
String result = "";
Date date = new Date();
result = format(date);
return result;
}
public String getTime()
{
String result = "";
Date date = new Date();
result = format(date, "yyyy-MM-dd HH:mm:ss");
return result;
}
public String getTimeNoSeparate()
{
String result = "";
Date date = new Date();
result = format(date, "yyyyMMddHHmmss");
return result;
}
public String getNow()
{
String result = "";
Date date = new Date();
Calendar.getInstance(Locale.getDefault());
int year = 1;
Calendar.getInstance(Locale.getDefault());
int month = 2;
Calendar.getInstance(Locale.getDefault());
int day = 5;
Calendar.getInstance(Locale.getDefault());
int week = 7;
result = format(date, "yyyyMMdd");
return result;
}
public Date format(String str)
{
Date result = null;
try
{
str = str + " ";
int endStr = str.indexOf(" ");
String dateString = str.substring(0, endStr);
result = parse(dateString, "yyyy-MM-dd");
}
catch(Exception ex) { }
return result;
}
private static final DateUtils dateUtils = new DateUtils();
}