How to get Date formatted in locale with specific pattern?


How to get Date formatted in locale with specific pattern?



After searching, i was unable to find a solution for this problem.



i have this:


Long parseDt = Long.valueOf(arrayJson.getJSONObject(i-j).getInt("dt")); // dt is a timestamp
Locale locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);
String date = new SimpleDateFormat("EEE d MMM", locale).format(new java.util.Date(parseDt * 1000));



So, the pattern works but when i change the phone language, it didn't swap the way it displays, i want to mean if the phone language is French it displays the date like this and it is good for most of the european language:



lun 2 juil



and if i switch language to English, it displays the date like that:



mon 2 july



instead of:



mon july 2



Do you have any clue to solve this problem respecting my pattern knowing that i want the name of the day with 3 characters maximum?



The existing predefined format (like FULL) could work but the name of the day is displayed entirely, for example it is "monday" and i would like only "mon", not "monday", so, any idea?





Why are you expecting the day number to be last with EEE d MMM?
– cricket_007
Jul 2 at 6:53


EEE d MMM





To display the date in mon july 2 format you should change the given format for SimpleDateFormat . From EEE d MMM to EEE MMMM d
– Suganthan Madhavan Pillai
Jul 2 at 6:56



mon july 2


SimpleDateFormat


EEE d MMM


EEE MMMM d





Are you sure that your users prefer “Mon”? Asking on one hand suspecting that an amount of work has been put into defining the predefined formats, on the other hand respecting that you know your own users best (at least I hope you do).
– Ole V.V.
Jul 2 at 7:50





@SuganthanMadhavanPillai, idealy i would like to do't have to swap the pattern. I would like it automatic according to the country. I am going to answer soon my question to explain my choices.
– eric
Jul 2 at 11:00





@OleV.V., agree, maybe the users would more prefer the full day name but in this case, my problem is that i have not enough room to display the full day name. Idealy, 3 characters for the day name, 2 for the day of the month and 3 or 4 for the month, and that's all, no more room.
– eric
Jul 2 at 11:05




3 Answers
3



You could toggle the format according to the locale you got.


Locale locale = ConfigurationCompat.getLocales(
Resources.getSystem().getConfiguration()).get(0);

String fmt = (locale == Locale.ENGLISH) ? "EEE MMM d" : "EEE d MMM";
String date = new SimpleDateFormat(fmt, locale)
.format(new java.util.Date(parseDt * 1000));





Thank you cricket_007 for your idea. I was thinking to do something like this, it is tricky. I would have prefer an automatic way to do it but i like it. If i don't find any more solution, i will take this answer as the accepted answer. Thnak you for the help.
– eric
Jul 2 at 9:21






If you use Android View binding, you should be able to do this within the XML, I think
– cricket_007
Jul 2 at 9:31





My accepted answer is for cricket_007 but my own answer (based on cricket answer) is perfectly appropriate to my problem and without the mistake of (the "==" instead of ".equals")
– eric
Jul 3 at 18:01




Java already “knows” in which locales the day of month goes before the month name and in which it goes after. This is what the full predefined format gave you. So for a general solution that may work in all or most locales I suggest that you rely on this. We can modify the predefined full pattern to use abbreviations for day of week and for month:


int parseDt = arrayJson.getJSONObject(i-j).getInt("dt"); // dt is a timestamp
Locale locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);
String formatPattern = DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.FULL, null, IsoChronology.INSTANCE, locale);
formatPattern = formatPattern.replaceAll("E{3,}", "EEE").replaceAll("M{3,}", "MMM");
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern(formatPattern, locale);
ZoneId zone = ZoneId.of("America/Whitehorse");
ZonedDateTime dateTime = Instant.ofEpochSecond(parseDt).atZone(zone);
String date = dateTime.format(dateFormatter);
System.out.println(date);



Example output:


Mon, 2 Jul 2018


Mon, Jul 2, 2018


lun. 2 juil. 2018



I am using regular expressions to make sure that if day of week and/or month is in the format pattern string (for example EEEE and MMMM), it is modified to its abbreviation. Since day of week may also be indicated by lowercase e or c and month by L, you may want to do the same trick with these letters.


EEEE


MMMM


e


c


L



I am using java.time, the modern Java date and time API. To use this on not-brand-new Android, get the ThreeTenABP library: use the links at the bottom. If you object to using a high-quality future-proof external library, you may be able to play a similar trick with SimpleDateFormat.


java.time


SimpleDateFormat



If you want to omit the year too (as in your examples), it is slightly more complicated, but doable. Here’s a simple attempt to remove the year if it comes last:


formatPattern = formatPattern.replaceFirst("^(.*[w'])([^w']*y+)$", "$1");



With this line inserted the output in French locale is just lun. 2 juil., for example. Year may also be given with letter u, you may want to take this into account too.


lun. 2 juil.


u



Converting your Unix time to a date is a time zone sensitive operation, so I give time zone in the code. Please put the one you desire where I put America/Whitehorse. If you trust that the JVM’s time zone setting reflects what the user wants, you may use ZoneId.systemDefault() (the SimpleDateFormat in your own code uses the VM setting too).


ZoneId.systemDefault()


SimpleDateFormat


java.time


java.time


java.time





Many thanks Ole V.V. for your interesting answer and the time spent. I am going to think about what would be the easiest way to do it. I will answer this post by myself according to my requirements and to explain my choices. Cheers & thank you.
– eric
Jul 2 at 10:43



According to the date format of the countries:



https://en.wikipedia.org/wiki/Date_format_by_country



and my app needs, i've choosen the easiest way to display it:


int parseDt = arrayJson.getJSONObject(i-j).getInt("dt"); // dt is a timestamp
Locale locale = ConfigurationCompat.getLocales(Resources.getSystem().getConfiguration()).get(0);
String dateFormat = ((locale.equals(Locale.US) || locale.equals(Locale.CHINA) || locale.equals(Locale.CANADA) || locale.equals(Locale.JAPAN)) ? "EEE, MMM d" : "EEE d MMM";
String date = new SimpleDateFormat(dateFormat, locale).format(new java.util.Date(parseDt * 1000));



cheers and thx to all.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages