GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calendar with Locale Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calendar with Locale" Watch "Calendar with Locale" New topic
Author

Calendar with Locale

Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324



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.

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

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 ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9303
    
  17

Also to add, the java.text.DateFormat class is used to format a date instance according to a locale...
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
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

Joined: Sep 06, 2005
Posts: 84
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

Joined: Feb 16, 2009
Posts: 64
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






SCJP 6, SCBCD 5, SCWCD 5



Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I think Byju already explained it.


All code in my posts, unless a source is explicitly mentioned, is my own.
khaled Jamal
Ranch Hand

Joined: Feb 16, 2009
Posts: 64
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

Joined: Sep 28, 2008
Posts: 324


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.




 
GeeCON Prague 2014
 
subject: Calendar with Locale