Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calendar with Locale

 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The above code takes Locale reference in Calendar. I was expecting the output to be

Thu Feb 26 10:11:16 EST 2009
??? ??, ???


because of the locale is not defined, where as the actual output is

Thu Feb 26 10:11:16 EST 2009
Thu Feb 26 10:11:16 EST 2009


Can anyone explain me the use of Locale reference in the Calendar's getInstance() method.
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look this is not the fault of Calendar class. calling calendar.getTime() returns a Date instance. A date instance is not associated with any locale or time zone. Try displaying the calendar object itself and then see the output ...
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also to add, the java.text.DateFormat class is used to format a date instance according to a locale...
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right Ankit, we have DateFormat class to play with Locales, so my question is when do we need to use Locale reference in Calendar?
 
Byju Joy
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Locale("th", "TH") --> you get a BuddhistCalendar.
and Locale("ja", "JP") --> you get a JapaneseImperialCalendar

All other Locales including Locale("hi", "IN") returns the default GregorianCalendar.
 
khaled Jamal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is stated in k&B that Calendar.getInstance(Locale locale) is used for getting an object that lets you perform date and time calculations in a DIFFERENT locale


here is a sample code :




The output is :

Tue May 26 18:18:44 GMT 2009
26 maggio 2009


the funny about all this is when commenting Line 1 and uncommenting Line 2 the result is the same


Let us please know if Calendar.getInstance(Locale locale) has an effect





 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Byju already explained it.
 
khaled Jamal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not know what does Gregorian Calendar mean so I did not read the post carefully, after googling it I got it

The Gregorian Calendar is the internationally accepted civil calendar

Thanks
 
Himalay Majumdar
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


If I do the above in 1.5 I get the following output

java.util.GregorianCalendar[time=1235681681766,areFieldsSet=true......

Nowhere I find Japanese Calendar

But as of java 6, new Locales have been added

Following code from java2s should work fine.
Am at work..that has java 5 , will try it at home . But I think this is it.




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic