String.format doesn't give month and weeknames in German locale - android

I'm using following code in my app:
Calendar tmpCalendar = Calendar.getInstance(Locale.getDefault());
String itemTime = String.format(
Locale.getDefault(),
"%1$tA, %1$te. %1$tB %1$tY",
tmpCalendar);
German is the default language on my device, so I'd expect to get something like:
Dienstag, 7. Juni 2011
Instead I'm getting:
3, 7. 6 2011
If I use Locale.US instead of Locale.getDefault() everything works fine. Am I doing something wrong?
Oddly, it works in an emulator running Android 2.2 in German, but not on a HTC Desire, also running 2.2. Why?

It seems to be a bug of some vendors and is issue #9453 in the Android issue tracker.

SimpleDateFormat sdf = new SimpleDateFormat("EE/MM/yyyy");
Date date = new Date();
String sDate= sdf.format(date);
Read more on it here

It does exactly what you ask it to do. If you want to have fine-grain control over the process, you can always use SimpleDateFormat. However, I would rather recommend this method:
Calendar tmpCalendar = Calendar.getInstance(Locale.getDefault());
DateFormat formatter = DateFormat.getDateInstance(DateFormat.LONG, Locale.getDefault());
formatter.setTimeZone(TimeZone.getDefault());
String itemTime = formatter.format(tmpCalendar.getTime());
Also, I would recommend using DateFormat.DEFAULT but you expect long date format, so...

Related

Material Date Picker Difference Format on Difference Device

I need a help.
I'm working with date picker material.io.
And I get different format while installed the APK on difference device.
I figure out the problem is about language device setting.
Maybe someone here has the same problem like me? How to fix this problem?
Here's my date picker code :
private val datePicker = MaterialDatePicker.Builder.datePicker().build()
with(datePicker) {
addOnCancelListener { datePicker.dismiss() }
addOnPositiveButtonClickListener {
val formatter = DateTimeFormatter.ofPattern("MMM d, yyyy")
datePickerValue = LocalDate.parse(datePicker.headerText, formatter)
binding.rowInputSearchDob.edtDob.setText(datePickerValue withFormat getString(R.string.date_format_d_MMM_yyyy))
}
}
Thanks before :)
Your date formatter and parser will be using the format and language settings of the device.. the "default" locale.
To override this you need to specify the locale. Probably by using ".withLocale" see withLocale docs

Calendar returning birmanian numbers

something strange happened. I'm using Calendar to name the audiofiles I'm recording. Everything was working ok until today. The next code is giving me the date in birmanian, I don't know why.
The code is simple:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, 1);
SimpleDateFormat format1 = new SimpleDateFormat("HH;mm;ss[,[dd-MM-yyyy");
String formatted = format1.format(cal.getTime());
(I'm using all the "; , [ and more strange characters to create the file after, it is not the cause of the problem it worked well few days ago)
what I'm getting is:
formatted = ၁၃;၅၃;၂၅[,[၃၁-၀၅-၂၀၁၆
Any help?
Thanks for your time!

Android - Hebrew locale doesn't work in Samsung S1

I'm need to show date in dd/MM/yyyy format (full month name) in Hebrew.
In Samsung s1 - it shows me English locale and in Motorola Atrix 4G it works fine
(shows the month name in Hebrew).
I'm using:
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, getLocale());
// I TRIED ALL THESE 3 LOCALES
private Locale getLocale() {
return new Locale("he");
//return new Locale("iw_IL");
//return new Locale("iw");
}
Note: I need to show Hebrew even if the phone defalt locale is different.
How can I implement for all devices (that has hebrew)?
Per Google, en_US ... is the only locale Java guarantees is always available.
If you know you're going to need Hebrew month names regardless of what's provided by the underlying platform, you should include that data in your app.

Change timezone in android programmatically

I wanted to change timezone in android programmatically like to set timezone as "America/Los_Angeles". How can I do this. How can this be possible by using id's.
In your AndroidManifest.xml
<uses-permission android:name="android.permission.SET_TIME_ZONE"/>
In your source:
AlarmManager am = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
am.setTimeZone("America/Los_Angeles");
Abhishek's code simply defines a Calendar instance with a specific format to be used in the app, so that will not work.
It is not possible to change the phone's timezone programmatically.
You could redirect the user to the appropriate settings, however:
startActivity(new Intent(android.provider.Settings.ACTION_DATE_SETTINGS));
If this is on a simulator or a device that you have root on, you could run
adb shell "su -c 'setprop persist.sys.timezone America/Los_Angeles; stop; sleep 5; start'"
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss z");
sdf.setTimeZone(TimeZone.getTimeZone("America/Los_Angeles"));
System.out.println(sdf.format(calendar.getTime()));
Source
You should have system signatures for setting timezones. if you are not manifacturer for your device may be you can contact them and ask for keystore files. after that you can sign your app as system with keystore file.

Android:Changing time format as per the Device's Current time format

How to get current time format of the android device? i.e. If the android device's current time format is changed from 12hr to 24 hr format and vice versa, then how to check that setting programatically?
I want to change the time shown in my app widget to be of same format as the device's and whenever the time format is changed then my widget should also show that time format. Any pointers will be helpful.
thanks.
You can use TIME_12_24.
int time_format = 0;
try {
time_format = Settings.System.getInt(getContentResolver(), Settings.System.TIME_12_24);
} catch (SettingNotFoundException e) {
e.printStackTrace();
}
Log.i(TAG,"Time format: "+time_format);
time_format will have 12 or 24 based on the settings.
Make sure you have added this permission to manifiest file.
<user-permission android:name="android.permission.WRITE_SETTINGS" />
EDIT :
You can also use DateFormat.is24HourFormat(). It doesn't require an extra permission.
The simple way would be to use to use android native android.text.format.DateFormat class's is24HourFormat(this) static method as below.
if (android.text.format.DateFormat.is24HourFormat(YourActivityName.this)) {
// 24 hour format
}
else
{
// 12 hour format
}
check, http://developer.android.com/reference/android/text/format/DateFormat.html
DateFormat::getDateFormat()
Date date = new Date(location.getTime());
dateFormat.format(date);
Please check this...
http://developer.android.com/reference/java/text/SimpleDateFormat.html
How do you format date and time in Android?
24 hour clock not working in dateformat android

Categories

Resources