Date with format and locale - android

I am trying to get the date with this pattern - "dd/MM/yyyy".
So far i've used SimpleDateFormat to achieve it, but now i need to also support Chinese in my app and it's not giving me the result i need.
I'm trying to make it look like the pattern with attention to the locale:
English: 16/05/2016
Chinese: 2016年05月16日
I tried different options -
android.text.format.DateUtils.formatDateTime
android.text.format.DateFormat.getDateFormat
but couldn't get the result i wanted.
Thanks

If you want specific patterns, you have to test the locale and apply the format you want.
For your english and chinese formats :
CharSequence englishDate = DateFormat.format("dd/MM/yyyy", date);
CharSequence chineseDate = DateFormat.format("yyyy年MM月dd日", date);
Results are :
25/05/2016 and 2016年05月25日

Related

Android format date using X country translation

For date parsing, I'm using SimpleDateFormat.
I know that it's possible to pass Locale object to translate month names.
I tried to use the Estonian language: Locale("ee", "Estonia"), but it did not work.
Am I using the wrong language code or Estonian language is not supported thus I've to use a different approach?
I was able to use Estonian by using:
Locale("et", "ee")
You can get the correct Locale for any country from Translations Editor in Android studio.
For Estonian country, Locale should be Locale("et", "ee")

How to prevent default language to be forced when using string format

So I Have a TextView in my calculator app that I eventually display the results in it ...
It works ... but recently during the testing I found out that if The Default Language of That device is set to a non-english language (for example arabic or farsi) numbers in TextView get to be shown in that specific language (not english) and in a completely different format !!
I used this code to generate the result
result = String.format("%.4f", mResultBeforeFormatting);
resultTextView.settext(result);
also to note is that if I set the TextView with a hard coded string the issue doesn't happen
resultTextView.settext("343");
The formats used by the String.format method will by default be the formats specified by the device's default locale.
If you want to force the use of a specific locale, use the String.format method that accepts a locale parameter.
For example:
result = String.format(Locale.ENGLISH, "%.4f", mResultBeforeFormatting);

Express a number in a special format in Android

I have a question, I am doing an App that is a kind of bill however I don't know how to express in a textview for example:
1120 as $1.120 or something like this.
Thanks in advance
numberformat is your solution :
Test code :
int = 1236;
NumberFormat nf = NumberFormat.getInstance(Locale.US);
Log.i("test", nf.format(nb));
show output :
1,236
There are a number of solutions to format numbers in Android which of course depend on the nature of your app and your requirements.
Because you are using a money value in your example, I would look into Big Decimal at has numerous precision and rounding methods.
Here's a little example for you, note that it is not localised!
BigDecimal theAmount = new BigDecimal(int);
DecimalFormat decimalFormat = new DecimalFormat("$#,##0.00;-$#,##0.00");
// ^ This is set manually, you could use a localised format to have Android set the
// values based on locale like this: DecimalFormat.getInstance();
decimalFormat.setDecimalSeparatorAlwaysShown(true);
decimalFormat.setMinimumFractionDigits(3);
decimalFormat.setMaximumFractionDigits(3);
// ^ There are a lot of methods to check out here!
TextView yourTextView.setText(decimalFormat.format(theAmount.divide(new BigDecimal(1000))));
Good luck!

How to parse an XML date with Zulu time in Android?

How can I parse a standard XML date in Android that is formatted according to the ISO standard? Example:
2012-12-13T12:34:56.678Z
Note how the time zone is given as "Z" (Zulu time).
SimpleDateFormat does not recognize the Z, and when I try to use the XML packages, I get an exception that they are not included.
DatatypeConfigurationException: Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl not found
Am I missing something? I'm assuming it should be pretty simple and straightforward to parse and format an ISO date.
In JavaScript, we would write:
var isoDateString = new Date().toISOString(); //2012-12-13T12:34:56.678Z
var isoDate = new Date(isoDateString);
See Android reference:
http://developer.android.com/reference/java/text/SimpleDateFormat.html
One could also always use java regular expression parsing to grab the components of the DTG, but that is not recommended if the Android API provides this functionality.

String.format uses comma instead of point

My app is working on many devices without problems so far. But now I got my new Galaxy Tab with Android 3.2 where it crashes all the time. I found out that the problem was a float in an EditText.
I am using myEditText.setText(String.format("%.1f", fMyFloat)); to put the float in the EditText. But somehow the float on my 3.2 Galaxy Tab is generated with a comma instead of a point. When I read the EditText back the app crashes of course, telling me that this is no valid float because of the comma...
What is going wrong here?
Convert float to string..
From the documentation of String.format:
String.format(String format, Object... args)
Returns a localized formatted string, using the supplied format and arguments, using the user's default locale.
The quoted text above means that the output of String.format will match the default locale the user uses.
As an example a comma would be used as the decimal-point-delimiter if it's a user using Swedish locale, but a dot if it's using an American.
If you'd like to force what locale is going to be used, use the overload of String.format that accepts three parameters:
String.format (Locale locale, String format, Object... args)
Convert string to float..
Parsing an arbitrary string into a float using the default locale is quite easy, all you need to do is to use DecimalFormat.parse.
Then use .parse to get a Number and call floatValue on this returned object.
Your format call on your Galaxy Tab uses some default Locale which in turn uses , for floats. You could use String.format(Locale,String,...) version with specific locale to make things work.
Or you should've used same locale both for parsing and formatting the number. So you should probably go with NumberFormat to format and parse your floats.
String.format uses the locale you are in. You should do something like this if you want a dot:
NumberFormat formatter = NumberFormat.getInstance(Locale.US);
myEditText.setText(formatter.format(fMyFloat);
Have a look into NumberFormat for more formatting options
Use below code it's works for me:
NumberFormat nf = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat df = (DecimalFormat)nf;
df.applyPattern(pattern);
String output = df.format(value);
System.out.println(pattern + " " + output + " " + loc.toString());
Summing up previous answers, an easy way to have the dot instead of the comma in all country, is this:
myEditText.setText(Locale.CANADA, String.format("%.1f", fMyFloat));
And you will have your String formatted with the dot

Categories

Resources