ASP.NET日期时间类

日期和时间类开发心得
1 修改历史
Version Change Time Author Description Effort Hours
1.0 2006-4-28 Lanning 方法、资源等 1

2 介绍
这篇文章是对日期和时间进行的相关的内容。主要内容包括:
1.时间的格式标准
2.日期和时间与字符串之间转换方式
3.常用类的使用实例
4.Jsp页面上的调用和显示方式。
5.Apache common lang组件的解释
3 SimpleDateFormat
JDK中的SimpleDateFormat进行了详细的定义,同时 common lang的时间类也都是基于这个类来进行包装和扩展的。它是DateFormat的具体实现子类。
1.Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("ssyyyyHHmmddMM");
String Code=formatter.format(cal.getTime());

2.New SimpleDateFormat(aMask).parse(strDate)

4 DateFormatUtils
(1)DateFormatUtils.format(date, pattern)
private String periodId = DateFormatUtils.format(new Date(), "yyyyMM");
or pattern for yyyy-MM-dd HH:mm:ss
or pattern for yyyy-MM-dd HH:mm:ss or yyyy/MM/dd hh:MM:ss.SSS 其中'S' – MILLISECOND
or pattern for yyyy/MM/dd hh:MM:ss.SSS
pattern 是和{@link java.text.SimpleDateFormat} compatible
(2)SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
theForm.setValidTime(sdf.format(new Date()));
(3)DateFormatUtils.format(datDxmpInfo.getCreateDate(), "yyyy-MM-dd HH:mm:ss")
5 DateFormat
Jdk的类。 SimpleDateFormat是它的具体的实现类,提供了具体的实现类。
public abstract class DateFormat
extends Format 也有很多的方法, 它的子类也提供了更多的方法。
DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date -> text), parsing (text -> date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.

5.1.1 获得类实例
/**
* Gets the date/time formatter with the default formatting style
* for the default locale.
* @return a date/time formatter.
*/
public final static DateFormat getDateTimeInstance()
{
return get(DEFAULT, DEFAULT, 3, Locale.getDefault());
}

5.1.2 不同的转换机制
*

To format a date for the current Locale, use one of the
* static factory methods:
*


* myString = DateFormat.getDateInstance().format(myDate);
*

*

If you are formatting multiple dates, it is
* more efficient to get the format and use it multiple times so that
* the system doesn't have to fetch the information about the local
* language and country conventions multiple times.
*


* DateFormat df = DateFormat.getDateInstance();
* for (int i = 0; i < a.length; ++i) {
* output.println(df.format(myDate) + "; ");
*

}
*


*

To format a date for a different Locale, specify it in the
* call to getDateInstance().
*


* DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
*

*

You can use a DateFormat to parse also.
*


* myDate = df.parse(myString);
6 DateUtils
Apache common lang 组件
1.对日期进行round、truncate运算
2.时间运算格式。
A suite of utilities surrounding the use of the
* {@link java.util.Calendar} and {@link java.util.Date} object.


7 DateUtil
USBOSS专有的一个时间类。
(1) DateUtil.convertStringToDate(String aMask, String strDate)

(2)Date birthDay = DateUtil.convertStringToDate(theForm.getBirthday());

(3) Date submitTime = null;
Date endSubmitTime = null;
logger.debug("\n == test");
try {
submitTime = DateUtil.convertStringToDate("yyyy-MM-dd HH:mm:ss",
theForm.getSubmitTime());
8 Calendar
Calendar cal = Calendar.getInstance();
SimpleDateFormat formatter = new SimpleDateFormat("ssyyyyHHmmddMM");
String Code=formatter.format(cal.getTime());
9 Date
1.data.startDate = new Date(result.getTimestamp("start_time").getTime());
10 Apache Common lang组件
10.1 DateUtils
1. DateUtils
1、 A suite of utilities surrounding the use of the * {@link java.util.Calendar} and {@link java.util.Date} object.


2、 提供了向millis转换的机制。提供了很多的变量,也包括:每天的表示方式。
10.2 DateFormatUtils(应用最广泛)
介绍:
*

Date and time formatting utilities and constants.


*
*

Formatting is performed using the
* {@link https://www.360docs.net/doc/be11176319.html,ng.time.FastDateFormat} class.



1. 都是静态方法,不用初始化。
2. DateFormatUtils.format(date, pattern)
private String periodId = DateFormatUtils.format(new Date(), "yyyyMM");
or pattern for yyyy-MM-dd HH:mm:ss 参见:FastDateFormat中的parsePattern方法。
实例:DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:MM:ss.SSS")
3. pattern {@link java.text.SimpleDateFormat} compatible
10.3 FastDateFormat
1. *

FastDateFormat is a fast and thread-safe version of * {@link ava.text.SimpleDateFormat}.


2. FastDateFormat 中的函数parsePattern方法来解析,DateFormatUtils.format(date,pattern)的格式形式。

11 JDK时间API
11.1 SimpleDateFormat
* SimpleDateFormat is a concrete class for formatting and
* parsing dates in a locale-sensitive manner. It allows for formatting
* (date -> text), parsing (text -> date), and normalization.
11.2 Date类是个非常强大的类,
1、其中,引用了:DateFormat SimpleDateFormat等等. toString 有详细使用的方式。
2、After before compare等函数,使用非常方便。
toLocaleString 是过时,DateFormat.format(Date date)将代替它。
12 工程实例
12.1 获得系统当前时间
不同

方式来获得系统当前时间。
1. Date date = new Date();
System.currentTimeMillis());
2.Calendar calendar = Calendar.getInstance(Locale.CHINESE);
Date commitDate = calendar.getTime(); // Get system current time
logger.debug("the current time is: =====\n" + commitDate);
datCustomerService.setCommitDate(commitDate); 3.
12.2 实现对一个日期的月份、天, 进行增加和减少的方法!
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
int month = calendar.get(Calendar.MONTH);
logger.debug("\n == the month is:" + month + 1); // 0 represents January,
int newMonth = month + 1;
calendar.set(Calendar.MONTH, newMonth);
Date newDate =calendar.getTime();
return newDate;
12.3 时间间隔统计工具类
1.long startTime = System.currentTimeMillis();
long elapsedTime = System.currentTimeMillis() - startTime
2.https://www.360docs.net/doc/be11176319.html,ng.time.StopWatch
3.org.springframework.util.StopWatch
12.4 在jsp页面显示PO中的时间问题
如果用PO做为jsp中的bean,那么可以,在显示时间的地方,使用:

受理人:

受理时间:


NOTE:也可以这样的方式:因为默认生成为7位数字,只要修改到11位就可以方便的显示。
13 参考资源
1.Jdk
2.Apache common lang



Trackback: https://www.360docs.net/doc/be11176319.html,/TrackBack.aspx?PostId=1726637


相关文档
最新文档