寻道
探寻程序之道,软件之道,生存之道,生活之道及生命之道。
BlogJava
首页
新文章
新随笔
聚合
管理
posts - 180, comments - 316, trackbacks - 0
日期时间处理实用类
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天
posted on 2008-07-18 10:52
寻道者
阅读(183)
评论(0)
编辑
收藏
所属分类:
Java API
新闻频道
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
成果网帮您增加网站收入
相关链接:
网站导航:
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
相关文章:
JavaMail邮件发送实用类
用正则表达式找出每个属性对应的值
动态生成日历
日期时间处理实用类
java.util.Comparator使用示例
MAIL: junglesong@gmail.com
MSN: junglesong_5@hotmail.com
<
2008年7月
>
日
一
二
三
四
五
六
29
30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(2)
给我留言
查看公开留言
查看私人留言
随笔分类
(201)
Ajax(3)
CSS(9)
Java API(5)
JavaScript(5)
Java基础(39)
JMS(3)
Maven2之旅(13)
My way(3)
Object Orient Programming(33)
SSH(6)
Swing(9)
Web开发(13)
个人作品(13)
开源包使用(1)
算法数据结构(26)
线程Thread(3)
随想录(17)
随笔档案
(107)
2008年9月 (3)
2008年8月 (8)
2008年7月 (10)
2008年6月 (8)
2008年5月 (7)
2008年4月 (9)
2008年3月 (6)
2008年2月 (2)
2008年1月 (5)
2007年12月 (7)
2007年11月 (3)
2007年10月 (3)
2007年9月 (5)
2007年8月 (7)
2007年7月 (4)
2007年6月 (20)
个人软件下载
Sql Anywhere在华军的下载页面
SqlToolBox
文件批量命名器在华军的下载页面
目录文件比较器在华军的下载页面
我的其它博客
我的邻居们
BeanSoft's Java Blog
BlueDavy之技术Blog
CowNew开源团队
DANCE WITH JAVA
David.Turing's blog
fastpace
itkui的博客
Java快速开发平台
Java杂家
Jiangshachina
liaojiyong
Long的博客
Max On Java
mycsdnc#blog
Steady's Java Zone
sterning
zkjbeyond
信拈妙偶
千里冰封
和风细雨
寻道
山风小子的博客
庄周梦蝶
朱远翔 的博客
每日一得
点燃闪电
生活源于奋斗不息
翠湖月色
蛟龍居
谈笑有鸿儒,往来无白丁
铁手剑谱
鱼上游
最新随笔
1. Web页面表单域验证方式在Struts1.3.8中的使用
2. 使用模板方法模式简化控制层类(Action)的设计
3. Spring的ID增长器使用示例(MySql数据库)
4. 某大型招聘会Java职位相关技术名词统计
5. 应用程序的层次浅谈
6. log4j在桌面程序和Web程序中的配置
7. 使用Spring提供的MailSender异步发送文本邮件
8. 从模板方法模式到反射再到Spring的IoC
9. JavaMail邮件发送实用类
10. 面向对象编程中常见类的功能和形态
搜索
积分与排名
积分 - 126851
排名 - 62
最新评论
1. re: "小闹钟定时器"下载[未登录]
a
--ben
2. re: 通用化Web表单验证方式的改进方案
@小数
是还有改进的余地。
--寻道者
3. re: 通用化Web表单验证方式的改进方案
感觉页面上的东西太多了
--小数
4. re: 使用模板方法模式简化控制层类(Action)的设计
@隔叶黄莺
哈哈。
回到实际,楼主文章中说的主题和异常配置也有点远,觉得楼主好像意在说一个设计模式
--zhuxing
5. re: 使用模板方法模式简化控制层类(Action)的设计
@zhuxing
你的用语很职业,只是我们需要切合这里的实际来讨论问题。
--隔叶黄莺
阅读排行榜
1. Dom4j下载及使用Dom4j读写XML简介(3161)
2. 使用commons-fileupload实现单个和多个文件上传(2494)
3. 三种权限设计方案的归纳和比较(2415)
4. JTable常见用法细则(2387)
5. 程序员之路探究(1907)
评论排行榜
1. SqlToolBox 1.60发布,介绍及使用(13)
2. 程序员之路探究(13)
3. 设计构建一个软件程序的基本步骤探讨(10)
4. 三种权限设计方案的归纳和比较(10)
5. 使用CSS实现侧边Tab菜单栏(8)
60天内阅读排行
1. 应用程序的层次浅谈(1165)
2. 使用模板方法模式简化控制层类(Action)的设计(824)
3. 蔓延法判断两个城市的连接状态(806)
4. 用递归和扫描解决称球问题(787)
5. 从模板方法模式到反射再到Spring的IoC(584)
sitinspring(http://www.blogjava.net)原创,转载请注明出处.