春风博客

春天里,百花香...

导航

<2008年7月>
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

统计

公告

MAIL: junglesong@gmail.com
MSN: junglesong_5@hotmail.com

Locations of visitors to this page

常用链接

留言簿(11)

随笔分类(224)

随笔档案(126)

个人软件下载

我的其它博客

我的邻居们

最新随笔

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜

日期时间处理实用类

package com.sitinspring.datetime;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期时间处理实用类
 * 
 * 
@author sitinspring(junglesong@gmail.com)
 * 
@since 2008-7-18 上午10:30:15
 * @vsersion 1.00 创建 sitinspring 2008-7-18 上午10:30:15
 
*/

public final class DateTimeUtil {
    
private DateTimeUtil() {

    }


    
/**
     * 以格式format返回表示日期时间的字符串
     * 
     * 
@param format
     * 
@return
     
*/

    
public static String getDateTimeStr(String format) {
        Date date 
= new Date();
        Format formatter 
= new SimpleDateFormat(format);
        
return formatter.format(date);
    }


    
/**
     * 取得当前日期时间
     * 
     * 
@return
     
*/

    
public static String getCurrDateTime() {
        
return getDateTimeStr("yyyy.MM.dd HH:mm:ss");
    }


    
/**
     * 取得当前日期,不足两位前补零
     * 
     * 
@return
     
*/

    
public static String getCurrDate() {
        
return getDateTimeStr("yyyy.MM.dd");
    }


    
/**
     * 取得当前日期
     * 
     * 
@return
     
*/

    
public static String getSimpleCurrDate() {
        
return getDateTimeStr("yyyy.M.d");
    }


    
/**
     * 取得当前时间
     * 
     * 
@return
     
*/

    
public static String getCurrTime() {
        
return getDateTimeStr("HH:mm:ss");
    }


    
/**
     * 取得当前年月
     * 
     * 
@return
     
*/

    
public static String getCurrYearMonth() {
        
return getDateTimeStr("yyyy.MM");
    }


    
/**
     * 从文本形式日期取得Date日期时间
     * 
     * 
@param strMonth
     * 
@return
     
*/

    
private static Date getDate(String strMonth) {
        SimpleDateFormat myFormatter 
= new SimpleDateFormat("yyyy.MM.dd");

        
try {
            java.util.Date date 
= myFormatter.parse(strMonth);
            
return date;
        }
 catch (Exception ex) {
            
return null;
        }

    }


    
/**
     * 得到两个文本日期之间的天数
     * 
     * 
@param startDate
     * 
@param endDate
     * 
@return
     
*/

    
public static long getDaysBetween(String startDate, String endDate) {
        Date dStart 
= getDate(startDate);
        Date dEnd 
= getDate(endDate);

        
return (dEnd.getTime() - dStart.getTime()) / (24 * 60 * 60 * 1000);
    }


    
/**
     * 取某月的天数,strMonth的格式是"yyyy.MM"
     * 
@param strMonth
     * 
@return
     
*/

    
public static int getDaysInAMonth(String strMonth) {
        String[] arr 
= strMonth.split("[.]");

        
// Create a calendar object of the desired month
        Calendar cal = new GregorianCalendar(Integer.parseInt(arr[0]), Integer
                .parseInt(arr[
1]) - 11);

        
// Get the number of days in that month
        int days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);

        
return days;
    }


    
/**
     * 取某月第一天是周几,strMonth的格式是"yyyy.MM"
     * 
@param strMonth
     * 
@return
     
*/

    
public static int getWeekOfFirstDay(String strMonth) {
        String[] arr 
= strMonth.split("[.]");

        Calendar xmas 
= new GregorianCalendar(Integer.parseInt(arr[0]), Integer
                .parseInt(arr[
1]) - 11);
        
int dayOfWeek = xmas.get(Calendar.DAY_OF_WEEK) - 1;
        
return dayOfWeek;
    }


    
public static void main(String[] args) {
        System.out.println(
"当前日期时间为:" + getCurrDateTime());
        System.out.println(
"当前日期为:" + getCurrDate());
        System.out.println(
"当前日期为:" + getSimpleCurrDate());
        System.out.println(
"当前时间为:" + getCurrTime());
        System.out.println(
"2008.07.05与2008.07.18之间相隔:"
                
+ getDaysBetween("2008.07.05""2008.07.18"+ "");
        System.out.println(
"当前年月为:" + getCurrYearMonth());
        System.out.println(
"本月第一天为周" + getWeekOfFirstDay(getCurrYearMonth()));
        System.out.println(
"本月有" + getDaysInAMonth(getCurrYearMonth()) + "");
    }

}

输出:
当前日期时间为:2008.07.18 10:48:57
当前日期为:
2008.07.18
当前日期为:
2008.7.18
当前时间为:
10:48:57
2008.07.05与2008.07.18之间相隔:13天
当前年月为:
2008.07
本月第一天为周2
本月有31天

posted on 2008-07-18 10:52 sitinspring 阅读(522) 评论(0)  编辑  收藏 所属分类: Java API


只有注册用户登录后才能发表评论。


网站导航:
 
sitinspring(http://www.blogjava.net)原创,转载请注明出处.