You are on page 1of 9

DateFormat and SimpleDateFormat Examples

Version 1.1 of Java introduced the j a v a . t e x tpackage, which included utility classes for parsing and formatting numbers and dates, along with utility classes for building other kinds of parsers.

Query builder components


Ad-hoc user-friendly query builder. WinForms, ASP.NET, WPF, Silverlight
devtools.korzh.com

Default date formats The j a v a . t e x t . D a t e F o r m a tclass, and its concrete subclass j a v a . t e x t . S i m p l e D a t e F o r m a t , provide a convenient way to convert strings with date and/or time info to and from j a v a . u t i l . D a t eobjects. Figure 1 shows an example of using default D a t e F o r m a tobjects to format a date in a variety of ways:

i m p o r tj a v a . t e x t . D a t e F o r m a t ; i m p o r tj a v a . u t i l . D a t e ; p u b l i cc l a s sD a t e F o r m a t E x a m p l e 1{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ / /M a k ean e wD a t eo b j e c t .I tw i l lb ei n i t i a l i z e dt ot h ec u r r e n tt i m e . D a t en o w=n e wD a t e ( ) ; / /S e ew h a tt o S t r i n g ( )r e t u r n s S y s t e m . o u t . p r i n t l n ( "1 ."+n o w . t o S t r i n g ( ) ) ; / /N e x t ,t r yt h ed e f a u l tD a t e F o r m a t S y s t e m . o u t . p r i n t l n ( "2 ."+D a t e F o r m a t . g e t I n s t a n c e ( ) . f o r m a t ( n o w ) ) ; / /A n dt h ed e f a u l tt i m ea n dd a t e t i m eD a t e F o r m a t s S y s t e m . o u t . p r i n t l n ( "3 ."+D a t e F o r m a t . g e t T i m e I n s t a n c e ( ) . f o r m a t ( n o w ) ) ; S y s t e m . o u t . p r i n t l n ( "4 ."+ D a t e F o r m a t . g e t D a t e T i m e I n s t a n c e ( ) . f o r m a t ( n o w ) ) ; / /N e x t ,t r yt h es h o r t ,m e d i u ma n dl o n gv a r i a n t so ft h e / /d e f a u l tt i m ef o r m a t S y s t e m . o u t . p r i n t l n ( "5 ."+ D a t e F o r m a t . g e t T i m e I n s t a n c e ( D a t e F o r m a t . S H O R T ) . f o r m a t ( n o w ) ) ; S y s t e m . o u t . p r i n t l n ( "6 ."+ D a t e F o r m a t . g e t T i m e I n s t a n c e ( D a t e F o r m a t . M E D I U M ) . f o r m a t ( n o w ) ) ; S y s t e m . o u t . p r i n t l n ( "7 ."+ D a t e F o r m a t . g e t T i m e I n s t a n c e ( D a t e F o r m a t . L O N G ) . f o r m a t ( n o w ) ) ; / /F o rt h ed e f a u l td a t e t i m ef o r m a t ,t h el e n g t ho fb o t ht h e / /d a t ea n dt i m ee l e m e n t sc a nb es p e c i f i e d .H e r ea r es o m ee x a m p l e s : S y s t e m . o u t . p r i n t l n ( "8 ."+D a t e F o r m a t . g e t D a t e T i m e I n s t a n c e (
javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 1/9

D a t e F o r m a t . S H O R T ,D a t e F o r m a t . S H O R T ) . f o r m a t ( n o w ) ) ; S y s t e m . o u t . p r i n t l n ( "9 ."+D a t e F o r m a t . g e t D a t e T i m e I n s t a n c e ( D a t e F o r m a t . M E D I U M ,D a t e F o r m a t . S H O R T ) . f o r m a t ( n o w ) ) ; S y s t e m . o u t . p r i n t l n ( " 1 0 ."+D a t e F o r m a t . g e t D a t e T i m e I n s t a n c e ( D a t e F o r m a t . L O N G ,D a t e F o r m a t . L O N G ) . f o r m a t ( n o w ) ) ; } }

Figure 1. Using default D a t e F o r m a tobjects to format a D a t eobject. When you run this class, you will see output that looks something like that shown in Figure 2.

>j a v aD a t e F o r m a t E x a m p l e 1 1 .T u eN o v0 42 0 : 1 4 : 1 1E S T2 0 0 3 2 .1 1 / 4 / 0 38 : 1 4P M 3 .8 : 1 4 : 1 1P M 4 .N o v4 ,2 0 0 38 : 1 4 : 1 1P M 5 .8 : 1 4P M 6 .8 : 1 4 : 1 1P M 7 .8 : 1 4 : 1 1P ME S T 8 .1 1 / 4 / 0 38 : 1 4P M 9 .N o v4 ,2 0 0 38 : 1 4P M 1 0 .N o v e m b e r4 ,2 0 0 38 : 1 4 : 1 1P ME S T

Figure 2. Output from example in Figure 1 Default D a t e F o r m a tobjects retrieved from the static g e t I n s t a n c e ( ) ,g e t T i m e I n s t a n c e ( ) , and g e t D a t e T i m e I n s t a n c e ( )methods can also be used for parsing S t r i n gobjects to produce D a t eobjects. Figure 3 shows a simple example of this.

i m p o r tj a v a . t e x t . D a t e F o r m a t ; i m p o r tj a v a . t e x t . P a r s e E x c e p t i o n ; i m p o r tj a v a . u t i l . D a t e ; p u b l i cc l a s sD a t e F o r m a t E x a m p l e 2{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ / /M a k eaS t r i n gt h a th a sad a t ei ni t ,w i t hM E D I U Md a t ef o r m a t / /a n dS H O R Tt i m ef o r m a t . S t r i n gd a t e S t r i n g=" N o v4 ,2 0 0 38 : 1 4P M " ; / /G e tt h ed e f a u l tM E D I U M / S H O R TD a t e F o r m a t D a t e F o r m a tf o r m a t= D a t e F o r m a t . g e t D a t e T i m e I n s t a n c e ( D a t e F o r m a t . M E D I U M ,D a t e F o r m a t . S H O R T ) ; / /P a r s et h ed a t e t r y{ D a t ed a t e=f o r m a t . p a r s e ( d a t e S t r i n g ) ; S y s t e m . o u t . p r i n t l n ( " O r i g i n a ls t r i n g :"+d a t e S t r i n g ) ; S y s t e m . o u t . p r i n t l n ( " P a r s e dd a t e :"+ d a t e . t o S t r i n g ( ) ) ; } c a t c h ( P a r s e E x c e p t i o np e ){


javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 2/9

S y s t e m . o u t . p r i n t l n ( " E R R O R :c o u l dn o tp a r s ed a t ei ns t r i n g\ " "+ d a t e S t r i n g+" \ " " ) ; } } }

Figure 3. Using default D a t e F o r m a tobjects to parse a S t r i n g The result is shown in Figure 4. Note that since the string version of the date did not contain timezone or seconds, the timezone is set to the default timezone (EST, in this case) and the seconds are set to zero.

>j a v aD a t e F o r m a t E x a m p l e 2 O r i g i n a ls t r i n g :N o v4 ,2 0 0 38 : 1 4P M P a r s e dd a t e :T u eN o v0 42 0 : 1 4 : 0 0E S T2 0 0 3

Figure 4. Parsing a date string The p a r s emethod throws an exception if a date matching the format cannot be parsed. In the code shown in Figure 3, the string matches the format exactly. To see what happens when a bad string is encountered, the class in Figure 5 reads and attempts to parse input until a blank line (or a Control-D) is entered.

i m p o r tj a v a . t e x t . D a t e F o r m a t ; i m p o r tj a v a . t e x t . P a r s e E x c e p t i o n ; i m p o r tj a v a . u t i l . D a t e ; i m p o r tj a v a . i o . I O E x c e p t i o n ; i m p o r tj a v a . i o . B u f f e r e d R e a d e r ; i m p o r tj a v a . i o . I n p u t S t r e a m R e a d e r ; p u b l i cc l a s sD a t e F o r m a t E x a m p l e 3{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ / /G e tt h ed e f a u l tM E D I U M / S H O R TD a t e F o r m a t D a t e F o r m a tf o r m a t= D a t e F o r m a t . g e t D a t e T i m e I n s t a n c e ( D a t e F o r m a t . M E D I U M , D a t e F o r m a t . S H O R T ) ; / /R e a da n dp a r s ei n p u t ,s t o p p i n go nab l a n ki n p u tl i n e B u f f e r e d R e a d e rr e a d e r= n e wB u f f e r e d R e a d e r ( n e wI n p u t S t r e a m R e a d e r ( S y s t e m . i n ) ) ; t r y{ S y s t e m . o u t . p r i n t ( " E N T E RD A T ES T R I N G :" ) ; S t r i n gd a t e S t r i n g=r e a d e r . r e a d L i n e ( ) ; w h i l e( ( d a t e S t r i n g! =n u l l )& &( d a t e S t r i n g . l e n g t h ( )>0 ) ){ / /P a r s et h ed a t e t r y{ D a t ed a t e=f o r m a t . p a r s e ( d a t e S t r i n g ) ; S y s t e m . o u t . p r i n t l n ( " O r i g i n a ls t r i n g :"+d a t e S t r i n g ) ; S y s t e m . o u t . p r i n t l n ( " P a r s e dd a t e :"+ d a t e . t o S t r i n g ( ) ) ; S y s t e m . o u t . p r i n t l n ( ) ;/ /S k i pal i n e } c a t c h ( P a r s e E x c e p t i o np e ){ S y s t e m . o u t . p r i n t l n (
javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 3/9

" E R R O R :c o u l dn o tp a r s ed a t ei ns t r i n g\ " "+ d a t e S t r i n g+" \ " " ) ; } / /R e a da n o t h e rs t r i n g S y s t e m . o u t . p r i n t ( " E N T E RD A T ES T R I N G :" ) ; d a t e S t r i n g=r e a d e r . r e a d L i n e ( ) ; } } c a t c h ( I O E x c e p t i o ni o e ){ S y s t e m . o u t . p r i n t l n ( " I / OE x c e p t i o n :"+i o e ) ; } } }

Figure 5. Example of parsing dates entered on the command line Figure 6 shows an example of running this class and entering several date strings. (Text entered is shown in italics.)

>j a v aD a t e F o r m a t E x a m p l e 3 E N T E RD A T ES T R I N G :N o v4 ,2 0 0 38 : 1 4P M O r i g i n a ls t r i n g :N o v4 ,2 0 0 38 : 1 4P M P a r s e dd a t e :T u eN o v0 42 0 : 1 4 : 0 0E S T2 0 0 3 E N T E RD A T ES T R I N G :N o v4 ,2 0 0 38 : 1 4 E R R O R :c o u l dn o tp a r s ed a t ei ns t r i n g" N o v4 ,2 0 0 38 : 1 4" E N T E RD A T ES T R I N G :N o v4 ,2 0 0 38 : 1 4A M O r i g i n a ls t r i n g :N o v4 ,2 0 0 38 : 1 4A M P a r s e dd a t e :T u eN o v0 40 8 : 1 4 : 0 0E S T2 0 0 3 E N T E RD A T ES T R I N G :N o v e m b e r4 ,2 0 0 38 : 1 4A M O r i g i n a ls t r i n g :N o v e m b e r4 ,2 0 0 38 : 1 4A M P a r s e dd a t e :T u eN o v0 40 8 : 1 4 : 0 0E S T2 0 0 3 E N T E RD A T ES T R I N G :N o v4 ,2 0 0 32 0 : 1 4P M O r i g i n a ls t r i n g :N o v4 ,2 0 0 32 0 : 1 4P M P a r s e dd a t e :W e dN o v0 50 8 : 1 4 : 0 0E S T2 0 0 3 E N T E RD A T ES T R I N G :N o v4 ,2 0 0 32 0 : 1 4 E R R O R :c o u l dn o tp a r s ed a t ei ns t r i n g" N o v4 ,2 0 0 32 0 : 1 4 " E N T E RD A T ES T R I N G :

Figure 6. Parsing a variety of date strings Note that the default parser is somewhat flexible. It recognizes a long version of the month name (November instead of Nov), but does not recognize the two dates shown in red, both of which are missing AM or PM. Furthermore, it produces possibly unexpected results when the time 20:14 PM is entered: The parsed date is 8:14 the next morning.
D a t e F o r m a tactually gives you a small amount of control over leniency in parsing.

The default D a t e F o r m a tinstances are lenient by default, but invoking f o r m a t . s e t L e n i e n t ( f a l s e ) ;in the example in Figure 5 would cause the 20:14 PM example (in Figure 6) to fail, though it will still accept November or Nov. Using SimpleDateFormat for custom date formatting and parsing
javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 4/9

The default D a t e F o r m a tinstances returned by the static methods in the D a t e F o r m a tclass may be sufficient for many purposes, but clearly do not cover all possible valid or useful formats for dates. For example, notice that in Figure 2, none of the D a t e F o r m a t -generated strings (numbers 2 9) match the format of the output of the D a t eclasss t o S t r i n g ( )method. This means that you cannot use the default D a t e F o r m a tinstances to parse the output of t o S t r i n g ( ) , something that might be useful for things like parsing log data. The S i m p l e D a t e F o r m a tlets you build custom formats. Dates are constructed with a string that specifies a pattern for the dates to be formatted and/or parsed. From the S i m p l e D a t e F o r m a tJavaDocs, the characters in Figure 7 can be used in date formats. Where appropriate, 4 or more of the character will be interpreted to mean that the long format of the element should be used, while fewer than 4 mean that a short format should be used.

Symbol G y

Meaning Era Year

Type Text Number

Month

Text or Number

d h H k K m s S E D F w W a

Day in month Hour (1-12, AM/PM) Hour (0-23) Hour (1-24) Hour (0-11 AM/PM) Minute Second Millisecond (0-999) Day in week Day in year (1-365 or 1-364) Day of week in month (1-5) Week in year (1-53) Week in month (1-5) AM/PM

Number Number Number Number Number Number Number Number Text Number Number Number Number Text

Example GG -> AD yy -> 03 yyyy -> 2003 M -> 7 M -> 12 MM -> 07 MMM -> Jul MMMM -> December d -> 3 dd -> 03 h -> 3 hh -> 03 H -> 15 HH -> 15 k -> 3 kk -> 03 K -> 15 KK -> 15 m -> 7 m -> 15 mm -> 15 s -> 15 ss -> 15 SSS -> 007 EEE -> Tue EEEE -> Tuesday D -> 65 DDD -> 065 F -> 1 w -> 7 W -> 3 a -> AM aa -> AM z -> EST

javatechniques.com/blog/dateformat-and-simpledateformat-examples/

5/9

Time zone Excape for text Single quote

Text Delimiter Literal

zzz -> EST zzzz -> Eastern Standard Time hour h -> hour 9 ssSSS -> 45876

Figure 7. Syntax elements for S i m p l e D a t e F o r m a t Note that you will generally never want to use single-digit minutes, seconds, or milliseconds, even though these are supported by S i m p l e D a t e F o r m a t(m, s, S). Using the syntax from Figure 7, we can now make a S i m p l e D a t e F o r m a tthat can read the output of D a t e . t o S t r i n g ( ) . Figure 8 shows an example this:

i m p o r tj a v a . t e x t . D a t e F o r m a t ; i m p o r tj a v a . t e x t . S i m p l e D a t e F o r m a t ; i m p o r tj a v a . t e x t . P a r s e E x c e p t i o n ; i m p o r tj a v a . u t i l . D a t e ; p u b l i cc l a s sD a t e F o r m a t E x a m p l e 4{ p u b l i cs t a t i cv o i dm a i n ( S t r i n g [ ]a r g s ){ / /M a k ean e wD a t eo b j e c t .I tw i l lb ei n i t i a l i z e dt ot h e / /c u r r e n tt i m e . D a t en o w=n e wD a t e ( ) ; / /P r i n tt h er e s u l to ft o S t r i n g ( ) S t r i n gd a t e S t r i n g=n o w . t o S t r i n g ( ) ; S y s t e m . o u t . p r i n t l n ( "1 ."+d a t e S t r i n g ) ; / /M a k eaS i m p l e D a t e F o r m a tf o rt o S t r i n g ( ) ' so u t p u t .T h i s / /h a ss h o r t( t e x t )d a t e ,as p a c e ,s h o r t( t e x t )m o n t h ,as p a c e , / /2 d i g i td a t e ,as p a c e ,h o u r( 0 2 3 ) ,m i n u t e ,s e c o n d ,as p a c e , / /s h o r tt i m e z o n e ,af i n a ls p a c e ,a n dal o n gy e a r . S i m p l e D a t e F o r m a tf o r m a t= n e wS i m p l e D a t e F o r m a t ( " E E EM M Md dH H : m m : s sz z zy y y y " ) ; / /S e ei fw ec a np a r s et h eo u t p u to fD a t e . t o S t r i n g ( ) t r y{ D a t ep a r s e d=f o r m a t . p a r s e ( d a t e S t r i n g ) ; S y s t e m . o u t . p r i n t l n ( "2 ."+p a r s e d . t o S t r i n g ( ) ) ; } c a t c h ( P a r s e E x c e p t i o np e ){ S y s t e m . o u t . p r i n t l n ( " E R R O R :C a n n o tp a r s e\ " "+d a t e S t r i n g+" \ " " ) ; } / /P r i n tt h er e s u l to ff o r m a t t i n gt h en o wD a t et os e ei ft h er e s u l t / /i st h es a m ea st h eo u t p u to ft o S t r i n g ( ) S y s t e m . o u t . p r i n t l n ( "3 ."+f o r m a t . f o r m a t ( n o w ) ) ; } }

The output shows three identical strings:


>j a v aD a t e F o r m a t E x a m p l e 4 1 .T u eN o v0 42 1 : 5 3 : 4 3E S T2 0 0 3
javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 6/9

2 .T u eN o v0 42 1 : 5 3 : 4 3E S T2 0 0 3 3 .T u eN o v0 42 1 : 5 3 : 4 3E S T2 0 0 3

Figure 8. A parser for D a t e . t o S t r i n g ( )output Copyright 2003-2007 - Philip Isenhour

Categories
Basics Bug fixes General Development GUI Hosting Lucene Optimization Uncategorized

Java Exam Simulator


SCJA SCJP SCJD SCEA SCBCD SCWCD SCDJWS OCE JEE 6 EJB JPA. Buy Now !
www.epractizelabs.com

Ajax RIAs in 100% Java


The Most Advanced GWT Framework Live Demos & Free 60day Trial
www.SmartClient.com/

Cupn Google Adwords


Coloca Tu Empresa en Google. $650 gratis. Solicta tu cupn aqu
google.com/$650-Gratis

PDF Libraries for Java


Generate, Merge, Append, Barcodes And So Much More, Free Evaluation!
www.DynamicPDF.com

javatechniques.com/blog/dateformat-and-simpledateformat-examples/

7/9

I Recommend...
Hack e rs and
P aul G raham (H

Software as C
H oward Baetjer

Paralle ls De s
N ova D evelopm

The Long Tail:


C hris A nders on

Privacy

Pages
About this Site Compressing Data Sent Over a Socket DateFormat and SimpleDateFormat Examples Deploying Java Apps on a Virtual Dedicated Server Faster Deep Copies of Java Objects Faster JTextPane Text Insertion (Part I) Faster JTextPane Text Insertion (Part II) Fixing Disappearing Text Selections when a Menu is Opened GridBagLayout Example: A Simple Form Layout Java Font Problems under Panther (Mac OSX 10.3) JDB Example: Generating a Thread Dump Launching Java WebStart from the Command Line Linux X11 Libraries for Headless Mode Low-Memory Deep Copy Technique for Java Objects Lucene In-Memory Text Search Example Profiling WebStart Apps with JProfiler on OSX Setting JTextPane Font and Color Squid Corrupts Jar Files?
javatechniques.com/blog/dateformat-and-simpledateformat-examples/ 8/9

String Equality and Interning

Meta
Log in

javatechniques.com/blog/dateformat-and-simpledateformat-examples/

9/9