日期时间处理实用类(转贴)

工具类:

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]) - 1, 1);

// 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]) - 1, 1);
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天
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值