关于日期时间开发

日期和时间类开发心得
1       修改历史

Version
Change Time
 Author
Description
Effort Hours
1.0
2006-4-28
LevinSoft
方法、资源等
1
1.1
2007-8-28
LevinSoft
增加了:对SimpleDateFormat和DateUtis的比较。String和date之间转换。
增加:常见问题解答
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)
Wf97程序中, 就是采用这种方式。
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
       try {
           Date date1 = simpleDateFormat. parse (DateUtil.getDateTime( "yyyy-MM-dd HH:mm:ss" , new Date()));
           System. out .println( "simpleDateFormat is: " + date1);
          
           System. out .println( " is : " + DateUtil.convertStringToDate(DateUtil.getDateTime( "yyyy-MM-dd HH:mm:ss" , new Date())));
          
       } catch (ParseException pe){
           pe.printStackTrace();
       }
 
3 .两种方式等价
1 SimpleDateFormat simpleDateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
//            datZxDs.setValidTime(simpleDateFormat.parse(DateUtil.getDateTime("yyyy-MM-dd HH:mm:ss", getFirstDateInNextMonthOf(new Date()))));
              2 datZxDs.setValidTime(DateUtil.convertStringToDate( "yyyy-MM-dd HH:mm:ss" , DateUtil.getDateTime( "yyyy-MM-dd HH:mm:ss" new Date())));
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            不同的转换机制
* <p>To format a date for the current Locale, use one of the
 * static factory methods:
 * <pre>
 *  myString = DateFormat.getDateInstance().format(myDate);
 * </pre>
 * <p>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.
 * <pre>
 * DateFormat df = DateFormat.getDateInstance();
 * for (int i = 0; i < a.length; ++i) {
 *     output.println(df.format(myDate[i]) + "; ");
 * }
 * </pre>
 * <p>To format a date for a different Locale, specify it in the
 * call to getDateInstance().
 * <pre>
 *  DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);
 * </pre>
 * <p>You can use a DateFormat to parse also.
 * <pre>
 * 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.</p>
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.</p>
2、    提供了向millis转换的机制。提供了很多的变量,也包括:每天的表示方式。
10.2       DateFormatUtils(应用最广泛)
介绍:
* <p>Date and time formatting utilities and constants.</p>
 *
 * <p>Formatting is performed using the
 * {@link org.apache.commons.lang.time.FastDateFormat} class.</p>
 
1.    都是静态方法,不用初始化。
2.    把Date格式化为String
3.    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")
4.    pattern {@link java.text.SimpleDateFormat} compatible
10.3       FastDateFormat
1. * <p>FastDateFormat is a fast and thread-safe version of * {@link ava.text.SimpleDateFormat}.</p>
2. FastDateFormat 中的函数parsePattern方法来解析,DateFormatUtils.format(date,pattern)的格式形式。
 
11            JDK时间API
11.1       SimpleDateFormat
* <code>SimpleDateFormat</code> 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);  
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.org.apache.commons.lang.time.StopWatch
3.org.springframework.util.StopWatch
12.4       在jsp页面显示PO中的时间问题
如果用PO做为jsp中的bean,那么可以,在显示时间的地方,使用:
    < tr class = "FORM" >
      < td width = "20%" class = "FORM" > 受理人 : </ td >    
      < td class = "FORM" >< bean:write name = "DatDxmpInfoObj" property = "staffno" /></ td >         
      < td width = "20%" class = "FORM" > 受理时间 : </ td >    
      < td class = "FORM" >< bean:write name = "saportalzj114DatDxmpInfoForm" property = "createDate" /></ td >         
</ tr >   
NOTE 也可以这样的方式: 因为默认生成为 7 位数字,只要修改到 11 位就可以方便的显示。
13            常见问题解答
1.为什么一个凌晨零点的时间,00:00:00在oracle中不显示?
答:这个是oracle的一个机制。例如: 例如: 2007 - 9 - 1 00:00:00 ,在 oracle 中, 00:00:00 不显示,仅显示: 2007 - 9 - 1
2. 
14            参考资源
1.Jdk
2.Apache common lang
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值