I've noticed that some users of my app are experiencing oddly formatted dates, and I am assuming it is due to locale reasons. Can someone suggest a better way to achieve the expected result and/or explain how to properly deal with the (possible) locale issue?
Time time = new Time();
time.setToNow();
String timeString = time.format("%b_%d_%Y_%I-%M-%S-%p");
Expected result:
Feb_12_2014_10-06-09-AM
From my logging I occasionally see results like this:
2?_12_2014_07-35-58-??
sadly I do not capture the locale info so I don't know the exact details.
edit: After trying the accepted solution here are the results for each locale on Android.
en_ca Feb_12_2014_10-49-52-AM
fr_CA févr._12_2014_10-49-52-AM
en_ca Feb_12_2014_10-49-52-AM
zh_CN 2?_12_2014_10-49-52-AM
zh 2?_12_2014_10-49-52-AM
en Feb_12_2014_10-49-52-AM
fr_FR févr._12_2014_10-49-52-AM
fr févr._12_2014_10-49-52-AM
de Feb._12_2014_10-49-52-AM
de_DE Feb._12_2014_10-49-52-AM
it_IT feb_12_2014_10-49-52-AM
ja 2?_12_2014_10-49-52-AM
ko_KR 2?_12_2014_10-49-52-??
ko 2?_12_2014_10-49-52-??
zh_CN 2?_12_2014_10-49-52-AM
<none> 2_12_2014_10-49-52-AM
zh_CN 2?_12_2014_10-49-52-AM
zh_TW 2?_12_2014_10-49-52-??
en_GB Feb_12_2014_10-49-52-AM
en_US Feb_12_2014_10-49-52-AM
Use a SimpleDateFormat to format your datetime strings. It has a constructor that allows you to specify the Locale, for example Locale.US.
Time.format() always works with the current, user-chosen locale which may not be what you want.
Further reading: Be wary of the default locale
Related
I am using NumberFormat.compactCurrency() with HI locale as I want to compact the number in the Indian currency way - into lakhs, crores..etc.
But, if I use HI as the locale in it, it seems to add the lakh/crore string in Hindi. I want to compact the currency in the Indian way but show the lakh/crore in English. Do I need to use a different locale string for that? any other ideas?
The most simple way to get closest to your requirements would be to use en_IN:
NumberFormat.compactCurrency(locale: 'en_IN').format(price)
Output :
Is there an international (or widely used) standard code to specify the names of calendar systems?
I'm working on an application that (among other things) stores and presents historical dates in multiple calendar systems, such as Gregorian, Jewish, Hijri, and others. When storing the date, I need to be able to say what calendar system it is in.
I could invent my own system, but is there something already out there, similar to ISO 639 for languages? I haven't been able to find anything after quite a long search, but maybe I've been using the wrong terms.
Valid question; it is always better not to use arbitrary codes for such things.
You can simply use the values of name attribute which CLDR uses:
<key name="ca" description="Calendar algorithm key" alias="calendar">
<type name="buddhist" description="Thai Buddhist calendar"/>
<type name="chinese" description="Traditional Chinese calendar"/>
<type name="coptic" description="Coptic calendar"/>
...
<type name="islamicc" description="Civil (algorithmic) Arabic calendar" deprecated="true" preferred="islamic-civil" alias="islamic-civil"/>
</key>
But you can also, adapt the method that was used in CLDR data definition which consisted of using an extension to BCP47 ("Tags for Identifying Languages") called RFC6067 for "identifying Unicode locale-based variations using language tags. The 'singleton' identifier for this extension is 'u'". For example, the language Thai is identified by th. Therefore, th-u-ca-buddhist identifies the locale as Thai with support for Buddhist calendar:
th-u-ca-buddhist
| | |
| | +--> Calendar name (values in CLDR bcp47/calendar.xml).
| +-----> 2 char key defining extension to follow (here "ca" for calendar).
+--------> Identifies that extension U values are to follow.
Few more extended usage examples:
fa-u-ca-islamic-rgsa: Farsi with Islamic calendar, Saudi Arabia sighting.
fa-u-ca-persian: Farsi with Persian calendar.
fa_AF-u-ca-persian: Farsi (Afghanistan) with Persian calendar.
Obviously, if your intention is just to store the date and identifier for the calendar it is in, you can use en; i.e.
{ 'year': 1392, 'month': 6 , 'day': 31 , 'calendar': 'en-u-ca-persian' }
I need to echo internationalized time in different format for different languages.
Now i am using locale when creating a date, but it also outputs time:
$date->toString();
29.05.2012 0:00:00
Expected result:
29.05.2012
In documentation i can see inly custom formats (as i understand no internationalization format will be applied if i use them). Is it possible to output only date, without time, like described there (as i understand zf supports international formats: http://en.wikipedia.org/wiki/Date_format_by_country)?
I tried:
echo $date->get(Zend_Date::DATE_SHORT, $locale);
echo $date->toString(Zend_Date::DATE_SHORT); ?>
// produces MMMMMMMM (letters instead of date? Oo)
It didn't work because it was necessary to add
Zend_Date::setOptions(array('format_type' => 'iso'));
in bootstrap instead of
Zend_Date::setOptions(array('format_type' => 'php'));
With this option, it works fine!
I am trying to find the best route to get in some Custom formats I need. For example if I have a phone number 0803456765
In India it may be represented as +91 (080) 3456765
In US it may be 080-345-6765 and so on
I could keep the format in the properties file and based on locale I could pull the format and format the String. I could also have a Util class which does this for me after I identify the Locale.
But I think there might be a better route using NumberFormat. I guess NumberFormat automatically figures out the Locale and applies a certain Pattern to the String. Can I customize this pattern ? In the sense, can I tell GWT to use my Custom pattern for the US Locale
I know we can do this
// Custom format
value = 12345.6789;
formatted = NumberFormat.getFormat("000000.000000").format(value);
// prints 012345.678900 in the default locale
GWT.log("Formatted string is" + formatted, null);
but I don't want to specify my formatting pattern as in 'NumberFormat.getFormat("000000.000000")'. I want to override the default number formats of various Locales in GWT to achieve this. How do I do this ?
Don't roll your own. Google open sourced their library which you can leverage. It supports
Parsing/formatting/validating phone numbers for all countries/regions
of the world.
I live in Hungary and I want to create gwt programs.
timestamp
1324565400
gwt html
<meta name="gwt:property" content="locale=hu_HU">
my code
DateTimeFormat dateFormater = DateTimeFormat.getFormat("H:mm");
...
mikorColumnConfig.setRenderer(new GridCellRenderer<RendelesIdeiglenes>() {
#Override
public Object render(
RendelesIdeiglenes model, String property,
ColumnData config, int rowIndex, int colIndex,
ListStore<RendelesIdeiglenes> store,
Grid<RendelesIdeiglenes> grid)
{
return dateFormater.format((Date) model.get(property));
}
});
and gwt displays
15:50
but the real time is
14:50
Why does it display the wrong time? I belive it could have something to do with the day light saving time, how do I activate the dayLightSaving setting?
Hmm, well, 1324565400000 is 14:50 UTC, and 15:50 UTC+1, and I believe Hungary is in UTC+1, so, living in Hungary, your computer is in UTC+1, and thus expectedly displays 15:50.
Note: the app's locale (as set with the meta name="gwt:property") doesn't change the time-zone that applies. That one is always the one of the computer running the code, i.e. dates are always local dates, and are formatted as such unless you pass an explicit TimeZone to the format method.
BTW, you might want to use DateTimeFormat.getFormat(PredefinedFormat.TIME_SHORT) rather than DateTimeFormat.getFormat("H:mm"). The short time format in the hu locale is H:mm so you won't notice a change, but it'll be easier to internationalize it later on (if you add English support, you'll automatically use the appropriate short time format for the locale, contrary to H:mm which is fixed and locale independent).