• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Calendar with Locale

 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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





 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think Byju already explained it.
 
khaled Jamal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic