The conversion date for all occasions

I. Introduction                                                                                      

    Date and time of the acquisition, each program will be shown to be involved, here are some records for future reference.

 

Two,  java.util.Date class                         

Copy the code
// current date and time 
a Date now = new new a Date ();
 / * due to the different time zone default program, may show the following:
 * 1. GMT, Tue Oct 28 01:24:14 GMT 2014
 * 2. US Central Standard Time, Tue Oct 27 23:24:14 GST 2014
 */
System.out.println(now);
Copy the code

From the above example you may know that in addition  java.util.Date class  outside when we have to understand zone  java.util.TimeZone  this class date to get the information you need.

 

Three,  java.util.TimeZone class                        

Example 1:

Copy the code
// Get the default time zone (time determined by the most initially the JVM) 
the TimeZone defaultTZ = TimeZone.getDefault ();
 // show sun.util.calendar.ZoneInfo [id = "GMT", offset = 0, dstSavings = 0, useDaylight = to false, = 0 transitions, lastRule = null] 
the System. OUT .println (defaultTZ);
Copy the code

Example 2:

Copy the code
// set the East eight districts for the current time zone 
TimeZone e8 = TimeZone.getTimeZone ( " GMT + 8 " );
TimeZone.setDefault(e8);
// 显示sun.util.calendar.ZoneInfo[id="GMT+08:00",offset=28800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null]
System.out.println(TimeZone.getDefault())
Copy the code

TimeZone.getTimeZone ()  of the reference time zone ID, you can by  () TimeZone.getAvaliableIDs  available available zones ID. 

Well, now we have a date and time source data can be formatted to proceed.

 

Fourth, the date and time formatting Java code                  

1.  java.text.SimpleDateFormat extends java.text.DateFormat 

  Action: by way of example the  format ()  method of the target date (  java.util.Date  ) formatted as text, by  parse ()  method parses the text objects to date (  java.util.Date  ).

  Note: by  SimpleDateFormat sdf = new SimpleDateFormat ()  instantiation, will the current default region as a follow-formatted, time zone parsing operation, even if the subsequent code to reset the default zone does not affect the  SimpleDateFormat instance  time zone value.

Copy the code
// set the date and time when the initialization mode 
the SimpleDateFormat SDF = new new the SimpleDateFormat ( " YYYY / the MM / dd HH: mm: ss.SSS " );

// 修改日期和时间模式
sdf.applyPattern("yyyy/MM/dd HH:mm:ss.SSS")

sdf.format(new Date());
Copy the code

查看源码会发现其实 java.text.SimpleDateFormat 内部使用了 java.util.Calendar 来获取 java.util.Date实例 的时分秒等部分,并进行格式化处理。

 

2.  java.text.DateFormat                             

  作用:内置了多种与特定的 DateFormatSymbols对象 绑定的formatter对象(也就是预设了日期时间模式)。

  好处:方便对日期时间进行本地化。
  缺点:无法直接修改日期时间模式。

  其内部是使用 java.text.SimpleDateFormat 来处理格式化的

 

3.  java.util.Calendar                              

  作用:用于萃取日期时间中的年、月、日、时、分、秒等信息,至于格式化就有我们自己解决了。它有一个好处就是在初始Calendar实例时可以设置使用的TimeZone。

Copy the code
Date date = new Date();
Calendar c1 = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
c1.set(date);
cl.get(Calendar.YEAR);
cl.get(Calendar.MONTH);
cl.get(Calendar.DATE);
Copy the code

 

五、JSTL中的日期时间格式化                      

   <fmt:formatDate/> 示例:

Copy the code
<%
  Date now = new Date();
%>
<%-- 使用自定义日期时间模式来格式化 --%>
<fmt:formatDate value="${now}" timeZone="GMT+8" type="date" pattern="MM-dd"/>

<%-- 采用内置的日期时间模式来格式化 --%>
<fmt:formatDate value="${now}" timeZone="GMT+8" dateStyle="short"/
Copy the code

   <fmt:parseDate/> 示例:

<%-- 结果输出到JspWriter中 --%>
<fmt:parseDate value="2005/12/21" pattern="yyyy/MM/dd"/

 

六、再横向理解                              

  到这里大家应该可通过 SimpleDateFormat实例 来格式化日期时间了,但总觉得 java.text.DateFormat 不好理解。下面介绍的 java.util.Locale 和 java.text.DateFormatSymbols 应该可以帮助大家更好去理解。

  1. java.util.Locale 

     作为标识来代表特定地理位置、政治、文化区域。当某操作需要与特定的地理位置、政治、文化区域关联时,我们称其为locale-sensitive。(如处理货币、日期)<Br/>
`Locale`由“语言码(language code)”和“地区码(country code)”、“补充码(variant 入参)”组成,其中“地区码(country code)”和“补充码(variant 入参)”为可选。

   语言码(language code),由两个小写字母组成,如zh, en。命名规范:http://www.loc.gov/standards/iso639-2/englangn.html 
   地区码(country code),由两个大写字母组成,如CN,US。命名规范:http://www.iso.ch/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html 
 补充码(variant 入参),由于是规范之外的选项,因此没有严格的格式规定,只规定多个值时,通过下划线(_)连接各值即可。
 各类型的码采用下滑线(_)连接构成完整的Locale。
 Locale示例(“语言码(language code)”+“地区码(country code)”): zh_CN 。
 可以自定义Locale对象,也可直接使用`java.util.Locale`内置的Locale对象。、

  2. java.text.DateFormatSymbols 

   封装了根据Locale对象来对日期时间本地化资源的操作。一般不直接使用该类,而是通过 DateFormat.getDateInstance() 等方法来获取内置的formatter对象,该formatter对象已经绑定特定的 DateFormatSymbols对象 

 


 

Published 15 original articles · won praise 3 · views 20000 +

Guess you like

Origin blog.csdn.net/u013587862/article/details/76165598