java - How to use Calendar.getInstance with specified Locale -


i trying use calendar.getinstance(locale l) specified locale , not working. cannot figure out doing wrong.

the java doc. say:

getinstance public static calendar getinstance(locale alocale) gets calendar using default time zone , specified locale. the calendar returned based on current time in default time zone given locale. parameters: alocale - locale week data returns: calendar.

my code:

 public static void main (string[] args){       locale local = new locale("pt", "br");       calendar c = calendar.getinstance(local); // here using method      system.out.println(c.gettime()); // , here, cannot figure out why not working        dateformat dt = dateformat.getdateinstance(dateformat.long, local);      string s = dt.format(c.gettime());      system.out.println(s); // here example in portuguese brasil  } 

output:

wed apr 29 10:18:16 brt 2015

29 de abril de 2015

should first print must in locale("pt", "br"), in portuguese?

the answer loc correct: call calendar::gettime produces java.util.date object. java.util.date class has no explicit time zone yet tostring method confusingly applies jvm’s current default time zone while generating string.

all confusing names , behavior - of many reasons avoid these poorly designed, confusing, , troublesome old legacy date-time classes. instead should using java.time classes officially supplant old classes.

java.time

get current moment in utc. instant class represents moment on timeline in utc resolution of nanoseconds (up 9 (9) digits of decimal fraction).

instant instant = instant.now(); 

you can create string represent value standard iso 8601 formatting calling tostring.

string output = instant.tostring(); 

2016-09-28t19:38:21z

the code in question ignores issue of time zone. when not specify time zone jvm’s current default time zone implicitly applied. better specify explicitly.

note locale , time zone 2 separate distinct issues.

  • locale determine (a) human language translation of name of day, name of month, , such, , (b) cultural norms deciding issues of abbreviation, capitalization, punctuation, , such.
  • time zone determines wall-clock time used present date-time value.

you can have combination of two. example, time zone of kolkata india french locale, or brazil portuguese locale auckland new zealand time zone.

locale locale = new locale("pt", "br"); zoneid z = zoneid.of( "pacific/auckland" ); 

apply time zone zoneid produce zoneddatetime. conceptually, think of zoneddatetime = ( instant + zoneid ).

specify proper time zone name in format of continent/region. never use 3-4 letter abbreviation such est or ist not true time zones, not standardized, , not unique(!).

zoneddatetime zdt = instant.atzone( z ); 

the locale not affect meaning, on presentation. can let locale object drive automatic localization of when producing string represent date-time value via datetimeformatter class. specify formatstyle determine how long or abbreviated should string be.

datetimeformatter f = datetimeformatter.oflocalizeddatetime( formatstyle.full )                                        .withlocale( locale ); string output = zdt.format( f ); 

dump console. instant , zdt objects seen here represent same moment, same point on timeline. difference view through lens of different region’s wall-clock time.

system.out.println ( "instant.tostring(): " + instant                       + " | zdt: " + zdt                       + " | output: " + output ); 

instant.tostring(): 2016-09-28t20:20:38.242z | zdt: 2016-09-29t09:20:38.242+13:00[pacific/auckland] | output: quinta-feira, 29 de setembro de 2016 09h20min38s nzdt

conversion

avoid old .date , .calendar classes. if must use them old code not yet updated java.time types, can convert. use new methods added old classes. here call java.util.gregoriancalendar.from( zoneddatetime ).

java.util.calendar cal = java.util.gregoriancalendar.from( zdt ) ; 

and, going other direction:

zoneddatetime zdt = mygregoriancalendar.tozoneddatetime() ; 

about java.time

the java.time framework built java 8 , later. these classes supplant troublesome old date-time classes such java.util.date, .calendar, & java.text.simpledateformat.

the joda-time project, in maintenance mode, advises migration java.time.

to learn more, see oracle tutorial. , search stack overflow many examples , explanations.

much of java.time functionality back-ported java 6 & 7 in threeten-backport , further adapted android in threetenabp (see how use…).

the threeten-extra project extends java.time additional classes. project proving ground possible future additions java.time. may find useful classes here such interval, yearweek, yearquarter, , more.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -