Sunday, October 23, 2011

Parse String to Java Date and Format Date to Localized String

In this post, Let's look into how we can convert a String to Date object in Java and vice versa.

Solid understanding of Date class is essential to understand this post or anything related to java dates. I have written a post explaining the basics of Date class.

I have a string "october 21, 2011 19:08". I would like to convert this to Date object. How can I do this? Also, how can I convert that Date back to localized String?

First, Lets look at the code.
1:  import java.text.ParseException;  
2:  import java.text.SimpleDateFormat;  
3:  import java.util.Date;  
4:  import java.util.TimeZone;  
5:    
6:  public class DateTest {  
7:    
8:       public static void main(String[] args) {  
9:            try {  
10:                 String dateStr = "october 21, 2011 19:08";  
11:                 String pattern = "MMMM dd, yyyy HH:mm";  
12:                 TimeZone timezoneOfDateStr = TimeZone.getTimeZone("Asia/Kolkata");  
13:                   
14:                 SimpleDateFormat sd = new SimpleDateFormat(pattern);  
15:                 sd.setTimeZone(timezoneOfDateStr);  
16:                 Date date = sd.parse(dateStr);  
17:                   
18:                 System.out.println("Converting : ");  
19:                 System.out.println("     Date String: "+dateStr);  
20:                 System.out.println("     pattern: "+pattern);  
21:                 System.out.println("     time zone: Asia/kolkata");  
22:                 System.out.println();  
23:                 System.out.println("Converted.");  
24:                 System.out.println("System Stored the above Date as : "+date.getTime());  
25:                 System.out.println();  
26:                 String patternWithTimezone = "MMMM dd, yyyy HH:mm zzzz";  
27:                 SimpleDateFormat sdWithTZ = new SimpleDateFormat(patternWithTimezone);  
28:                 sdWithTZ.setTimeZone(timezoneOfDateStr);  
29:                 System.out.println("Formatting the date back to human readable format in the same timezone");  
30:                 System.out.println("     here it is: "+sdWithTZ.format(date));  
31:                   
32:                 System.out.println();  
33:                 sdWithTZ.setTimeZone(TimeZone.getTimeZone("GMT"));  
34:                 System.out.println("Formatting the date to human readable format in GMT");  
35:                 System.out.println("     here it is: "+sdWithTZ.format(date));  
36:            } catch (ParseException e) {  
37:                 e.printStackTrace();  
38:            }  
39:       }  
40:    
41:  }  
42:    

Output
Converting : 
 Date String: october 21, 2011 19:08
 pattern: MMMM dd, yyyy HH:mm
 time zone: Asia/kolkata

Converted.
System Stored the above Date as : 1319204280000

Formatting the date back to human readable format
 here it is: October 21, 2011 19:08 India Standard Time

Formatting the date to human readable format in GMT
 here it is: October 21, 2011 13:38 Greenwich Mean Time

Explanation
  1. Line 10: The date that needs to be converted
  2. Line 11: Pattern of the date that is defined in line 10. To understand how to build this pattern, read the table in the java doc of SimpleDateFormat class which explains about the letters and how they are interpreted (example: M is interpreted as month, y as year, d as Date, H as hour, m as min, z as timezone etc)
  3. Line 12: Timezone of the above date. Why timezone? A date by itself does not tell the whole story. (example: Oct 21, 2011 08:00 in newyork is same as Oct 21, 2011 11:00 in Las Angeles). So, just saying Oct 21, 2011 would not tell the whole story, we also need to tell the system what timezone that time belongs to. In this case, we are saying that the date belongs to "Asia/Kolkata timezone" / "India Standard Time".
  4. Lines 14,15,16: Create SimpleDateformat class. Tell it what pattern the string is in, What timezone it belongs to. Parse the date string and get date object.
  5. Lines 18 to 25: Print some details. It also prints the date in milliseconds. Why in milliseconds? Read Java Date Explanation
  6. Lines 26 to 30: Format the date to human readable format in India Standard Time and print.
  7. Lines 32 to 35: Format the date to human readable format in GMT and print.


No comments:

Post a Comment

Followers