热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

工具类:DateUtils类提供日期的处理方法

描述:封装了对一些对日期常见的操作方法。类ID:DateUtils方法:方法名描述getToday()得到当前日期字符串OnEntry:O


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


Ø        类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型日期.


 


Ø        代码:


 



ContractedBlock.gif
ExpandedBlockStart.gif
Code

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();

}




推荐阅读
author-avatar
懒懒加菲猫爱小狐狸
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有