Display timezone name in locale Java - java

I have this piece of code in Java
public static void main(String[] args) {
TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles");
System.out.println(tz.getDisplayName(new Locale("ja-JP")));
}
It displays the output "Pacific Standard Time". I would think, it should display this timezone in Japanese? Is this not how this library is supposed to behave?

Use Java's japanese Locale, not your own
System.out.println(tz.getDisplayName(Locale.forLanguageTag("ja-JP")));
or better yet
System.out.println(tz.getDisplayName(Locale.JAPAN));
See here for supported locales in Java 8.

Related

In Java-8 Locale Object convert to old ISO in some Language code. How can i fix this issue without update Java version?

Like when i take input (he-IL) then output comes like "iw_IL". But i do not want this convert to old iso.
public static void main(String[] args) {
Locale locale = getLocaleIn("he-IL");
System.out.println(locale.toString());
}
private static Locale getLocaleIn(String langCode) {
LocaleCode code = LocaleCode.getByCodeIgnoreCase(langCode);
Locale locale = code.toLocale();
return locale;
}
OutPut:
iw_IL,
Expected Output:
he-IL
Main Problem in Here, In Java Locale.Class:
Finally I Want an Locale Object without Converting to old ISO.
You should avoid non-standard 3rd party classes in problem descriptions, especially when they do not contribute to the problem at all.
We can simply use
Locale locale = new Locale("he", "IL");
System.out.println(locale.toString());
locale = new Locale("iw", "IL");
System.out.println(locale.toString());
and get
iw_IL
iw_IL
under JDK 8, which is in line with the documentation:
This constructor accepts both the old codes ("iw", "ji", and "in") and the new codes ("he", "yi", and "id"), but all other API on Locale will return only the OLD codes.
This has changed with JDK 17:
Obsolete ISO 639 codes ("iw", "ji", and "in") are mapped to their current forms.
and running the same example program with JDK 17 accordingly now prints
he_IL
he_IL
So the cleanest and probably the only solution to your issue would be updating the Java version. Any patch forcing Locale to return the new code in older versions may cause compatibility problems with other components of the runtime relying on the old behavior.

Java - Not the expected format from Locale

I'm having some issues in some countries, on this case is with the Locale("cs", "CZ") the format of the date I'm looking for is dd. MM. but is returning dd.MM., how can I solve this?
I'm trying to get the pattern doing this :
val pattern = DateFormat.getBestDateTimePattern(locale, "ddMM")
val dateFormatted = DateTimeFormatter.ofPattern(pattern,locale).withLocale(locale).format(date)
But the problem is since I'm using this Locale it's somehow assigned with an unexpected date formatter.
How can I get this dd. MM. pattern?
The code is in kotlin but i tag java so if you put a Java answer and it works I'll mark it as a correct one.
If you need a pattern for the complete date, DateTimeFormatter#ofLocalizedDate meets your requirement precisely. However, if you need to restrict it to just days and months, the options are limited. I have shown a couple of the options below:
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
public class Main {
public static void main(String[] args) {
Locale locale = new Locale("cs", "CZ");
DateTimeFormatter dtf1 = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM).localizedBy(locale);
// Test
String formatted = dtf1.format(LocalDate.now());
System.out.println(formatted);
// If you are required to remove year, do it explicitly
formatted = formatted.replaceAll("\\s+\\d{4}", "");
System.out.println(formatted);
// Or use a fixed format
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("dd. MM.", locale);
// Test
System.out.println(dtf2.format(LocalDate.now()));
}
}
Output:
7. 10. 2021
7. 10.
07. 10.
ONLINE DEMO
Learn more about the modern Date-Time API* from Trail: Date Time.
* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

java.util.Locale constructor does not workwhen using "gsw" as a Swiss language locale in Java 8

The context:
A customer asked me to help him with a method to format a Joda DateTime with a Locale specified as a parameter.
The problem:
Using "gsw" (Swiss German) as a language does not seem to have an effect on the translation (falls back to English in my case).
"gsw" is part of ISO639-2 (which is ISO639 alpha-3, which the Java locale says it supports, https://docs.oracle.com/javase/8/docs/api/java/util/Locale.html).
I verified to be 100% sure and gsw is part of alpha-3, but it wasn't part of alpha-2 (ISO639-1): https://www.loc.gov/standards/iso639-2/php/English_list.php
I've tried with other languages which are part of alpha-3 but were not present in alpha-2 and I see the same strange behavior.
Have I might not read well enough the Locale helpcenter?
The code:
//all are types belong to joda.time, except Locale, which is java.util
DateTimeZone dtZone = DateTimeZone.forID(IANATimezone);
DateTime dtus = dateTime.withZone(dtZone);
Locale locale = new Locale(Language, Region);
DateTimeFormatter dtf = DateTimeFormat.fullDateTime().withLocale(locale);

Using DateFormat.getDateTimeInstance().format(date);

When running some tests I came across the following issue. When using:
private String printStandardDate(Date date) {
return DateFormat.getDateTimeInstance(
DateFormat.SHORT, DateFormat.SHORT).format(date);
}
I found this produced different formats of Date depending on the location the tests where run from. So locally in windows / eclipse I got a result: 04/02/12 18:18 but on the Linux box in America I get 2/4/12 6:18 PM
This causes my Tests/Build to fail:
expected:<[04/02/12 18:18]> but was:<[2/4/12 6:18 PM]>
Could anyone explain this behavior?
That's not strange, that's exactly how it's supposed to work.
The API documentation of DateFormat.getDateTimeInstance says:
Gets the date/time formatter with the given date and time formatting styles for the default locale.
The default locale is different on your Windows system than on the Linux box in America.
If you want exact control over the date and time format, use SimpleDateFormat and specify the format yourself. For example:
private String printStandardDate(Date date) {
return new SimpleDateFormat("dd/MM/yy HH:mm").format(date);
}
Even better would be to re-use the SimpleDateFormat object, but beware that it is not thread-safe (if the method might be called from multiple threads at the same time, things will get messed up if those threads use the same SimpleDateFormat object).
private static final DateFormat DATE_FORMAT =
new SimpleDateFormat("dd/MM/yy HH:mm");
private String printStandardDate(Date date) {
return DATE_FORMAT.format(date);
}
The format is based on the default locale in your code. If you want to ensure results you must make sure to use a specific locale. The getDateTimeInstance method is overloaded to offer an alternative method that receives the locale that you want to use as parameter.
public static final DateFormat getDateTimeInstance(int dateStyle,
int timeStyle,
Locale aLocale)
If you use the same locale in both testing environments, the result should be the same.

Custom date format in android for given locale

I'm trying to format a date for a given locale new Locale("mk", "MK"). The locale is valid, it returns the country name and language properly. I want to use custom string, in my case "E, kk:mm" or "EEEE, kk:mm". I want the output to be "сабота, 12:00", but what I get is "7, 12:00".
This is how I use it and I tried many ways, but they all seem to behave the same.
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK));
sdf.format(new Date());
// output: 7, 12:30
Another method I tried
Calendar calendar = Calendar.getInstance(new Locale("mk", "MK"));
calendar.setTimeInMillis(new Date().getTime());
DateFormat.format("EEEE, kk:mm", calendar);
// output: Saturday, 12:30
I also tried using java.text.DateFormat instead android class, but no change.
The phone locale is set to English, but this is localized app, I want to show dates in a fixed locale format.
I've looked into many SO question regarding this issue and I wasn't able to find answer. I'm not interested in predefined formats, I want to use my own format and I want the date/month names to be formatted for the input locale.
I think the problem is that Macedonia is not a supported locale on the Android JVM. If you run your code as plain Java console app, it's fine. The method Locale.getAvailableLocales() returns 152 members in plain Java, only 88 in an Android emulator. If you have the code snippet:
Locale[] locales = Locale.getAvailableLocales();
String cCode;
for (Locale loc :locales){
cCode = loc.getCountry();
if (cCode.equalsIgnoreCase("MK"))
Toast.makeText(this, cCode, Toast.LENGTH_SHORT).show();
// Or System.out.println() in a Java app
}
Then the toast doesn't show for "MK" although it will println in the Java app
From documentation of SimpleDateFormat:
**Text**: For formatting, if the number of pattern letters is 4 or more,
the full form is used; otherwise a short or abbreviated form is used if
available. For parsing, both forms are accepted, independent of the
number of pattern letters.
So this should fix it:
SimpleDateFormat sdf = new SimpleDateFormat("EEEE, kk:mm", new Locale("mk", "MK"));
NickT was faster :-), so just adding to his answer: if you want to see your locales supported on Android, run:
for (Locale l:Locale.getAvailableLocales()) {
Log.d(l.getDisplayCountry(),l.toString());
}
and you will see that Macedonia is not on the list.

Categories

Resources