DateUtils类提供日期的处理方法

Ø        描述:           封装了对一些对日期常见的操作方法。

Ø        类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返回当前日期字符串

formatString

将字符串转换成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();

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值