Support Wikipedia Follow My Heart: How to solve Date Issues in Java

2011年10月14日星期五

How to solve Date Issues in Java

Java already provides powerful functions in dealing with Date (also Time) problems.
So you can input a String (i.e. “2001.07.04 AD at 12:08:56 PDT”), and get the Date Object by Java Methods.
In contrast, you can also provide a Date Object (or long number, etc.), and print the date as required date format easily.

What you need to done is just define the format String, i.e. “yyyy-mm-dd HH:mm:ss”. 
(Pay attention to the capticals, because they mean different format).
Here is a simple demostration.
Reference Code
The Output Result is :
test1:  Long Number is 1318252354000
test2:  Formatted Date String is 2011-10-10 22:12:34
test3:  Long Number is 481822496000
test4:  Formatted Date String is AM 12:34:56 04/09/85 JST



From Javadoc of JDK, we can easily learn how to write the format String.
Be careful on how to control the length of the items: i.e. the “yy” means here display the year, and two digits will be displayed.
Also, the “/” or “-” (or others) which are not defined in the table, will mean the separator.



sshot-33

没有评论:

发表评论