Java SimpleDateFormat.format(date) configuration - java

i am developing a Google-Calendar alike using Zk-Calendar i have a question about java.text.SimpleDateFormat here is some code
1). how to get the day and month in first letter uppercase is this possible?
here is the code i'm using so far.
private final SimpleDateFormat dformat = new SimpleDateFormat("EEEE dd-MMMMM-yyyy HH:mm:ss a");
private void printDateTester()
{
final Locale VENEZUELA_LOCALE = new Locale("es","VE");
Locale locale = Locale.getDefault();
System.out.println(locale.getDisplayCountry()+" "+locale.getCountry());//prints United States US
System.out.println(VENEZUELA_LOCALE.getDisplayCountry()+" "+VENEZUELA_LOCALE.getCountry());//prints Venezuela VE
final Calendar TODAY_US = Calendar.getInstance(locale);
final Calendar TODAY_VEN = Calendar.getInstance(VENEZUELA_LOCALE);
System.out.println(dformat.format(TODAY_US.getTime())); //prints sábado 10-agosto-2013 11:07:55 AM
System.out.println(dformat.format(TODAY_VEN.getTime()));//prints sábado 10-agosto-2013 11:07:55 AM
}
it prints something like sabado 10-agosto-2013 09:42:24 AM in english something like saturday 10-august-2013 but i would like something like Saturday 10-August-2013 09:42:24 AM is this possible?
any ideas thanks a lot.
UPDATE
i have create 2 SimpleDateFormats 1 using Locale.US and 2 other using either Venezuela Locale and Spain Locale the US version prints in Uppercase but both Venezuela and Spain locales prints in lowercase but i need show the data in Spanish why is this??
final Locale VENEZUELA_LOCALE = new Locale("es","VE");
Locale locale = Locale.getDefault();
final SimpleDateFormat dformatI = new SimpleDateFormat("EEEE dd-MMMMM-yyyy HH:mm:ss a",VENEZUELA_LOCALE);
final SimpleDateFormat dformatII = new SimpleDateFormat("EEEE dd-MMMMM-yyyy HH:mm:ss a",Locale.US);
Locale: Venezuela VE
print: domingo 11-agosto-2013 09:24:25 AM
Locale: United States US
print: Sunday 11-August-2013 09:24:25 AM

Have you tried setting Locale in DateTime format object:
private final SimpleDateFormat dformat = new SimpleDateFormat("EEEE dd-MMMMM-yyyy HH:mm:ss a",Locale.US);
Not all languages are following US standard (Capitalized day and month names). Therefore what output value java DateTimeFormat is giving was expected.
You could find this interesting discussion in the following questions
How to control the capitalization of month and day names returned by DateFormat?

Related

How can I change the language of the months provided by LocalDate?

I need to find the current month and print it. I have the following code:
this.currentDate=LocalDate.now();
this.month = this.currentDate.getMonth();
The problem is that the month is in English and I need to print it in French, to match the rest of the website language. How can I select the language of the month provided by the LocalDate.now() method without needing to do a manual translation each time I need to display it?
You can convert the Month type into a String using getDisplayName(), which can change the locale to French as follows:
this.currentDate = LocalDate.now();
this.month = this.currentDate.getMonth().getDisplayName(TextStyle.FULL, Locale.FRANCE);
You can use the DateTimeFormatter to create a formatter for French as follows:
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE, dd MMMM, yyyy", Locale.FRENCH);
final String month = LocalDate.now().format(formatter);

SimpleDateFormat - convert to other locale

I define a format like following:
sdf = new SimpleDateFormat("MMMM dd\nEEEE, YYYY", Locale.getDefault())
Now I get exceptions on some devices like this one:
java.lang.IllegalArgumentException: Unknown pattern character 'Y'
So the problem is, the Y is not supported in this devices local. So I have to change the format to following:
// the string format is formatted in english, so we pass in Locale.ENGLISH
new SimpleDateFormat("MMMM dd\nEEEE, YYYY", Locale.ENGLISH)
Question
How can I now use this SimpleDateFormat to print a date in the locale language? I want that EEEE is german, english, spanish or whatever. So I need to use the english format string but then print the formatted string in local language. How can I do this?
Example
SimpleDateFormat sdf = new SimpleDateFormat("MMMM dd\nEEEE, YYYY", Locale.getDefault());
Date d = new Date();
String formatted = sdf.format(d);
Expected
DE: Juli 2017\nMittwoch, 2017
EN: July 2017\nWednesday, 2017
Idea
Create a SimpleDateFormat with the current local like new SimpleDateFormat("MMMM dd\nEEEE, YYYY", Locale.getDefault()); but now I can't be sure that my string passed into the constructor is valid for the current locale... (Like the exception shows, Y is not valid in every locale)
Fix strings must be inside of sibgle quotes:
new SimpleDateFormat("MMMM dd'\n'EEEE',' yyyy", Locale.ENGLISH)
But \n is a newline. Hopefuly you want it there.
UPDATE
(Like the exception shows, Y is not valid in every locale):
Y is valid in every Locale!!

Oneliner to get a string representation of Day of week, day of month and time according to default locale

I'm using a combination of DateFormat and SimpleDateFormat to achive following string from a Date object:
fre 20 22:48
This snippet produces the outcome above:
DateFormat localizedTimeFormatter = DateFormat.getTimeInstance(DateFormat.SHORT, Locale.getDefault());
String str = new SimpleDateFormat("EE d", Locale.getDefault()).format(date) + " " + localizedTimeFormatter.format(date);
The outcome is exactly as I want, but I think there are improvements that could be done. I would like to use only the SimpleDateFormat to achieve this and getting shortname for day in week and day of month are not the problem. The problem is getting time in only hours and minutes according to locale, or more correctly, 12 or 24 hour format. I've checked out the documentation on the SimpleDateFormat Javadoc SimpleDateFormat
but as I can see there are no way of getting a time(hhmm) in 12 or 24hour format just by setting a format string to the SimpleDateFormat? Are there any way to achieve this by only using SimpleDateFormat as a oneliner, or do I have to do it the way I have done already, or are there a completely different way of solving this little issue?
How about
SimpleDateFormat sdf = new SimpleDateFormat("EE d HH:mm", Locale.getDefault());
or
SimpleDateFormat sdf = new SimpleDateFormat("EE d hh:mm", Locale.getDefault());
Your method is on the right track.
However, I didn't need to use the DateFormatter.
I got your expected result in this line itself.
String str = new SimpleDateFormat("EEE, d, k:m", Locale.getDefault()).format(Calendar.getInstance().getTime());
Output is:
Mon, 30, 19:5
The pattern you use in the first argument to SimpleDateFormat is the key here I think.
The meaning of the individual alphabets and their combination is already mentioned in the link you gave.
Hope this helps.

Getting time in Germany - Android

How can I get the current time in Germany regardless of the location of the user and current device time?
Calendar buttoncal = Calendar.getInstance();
String tdate = new SimpleDateFormat("dd.MM",Locale.GERMANY).format(buttoncal.getTime());
String tday = new SimpleDateFormat("EEEE",Locale.GERMANY).format(buttoncal.getTime());
one_date.setText(tdate.toString());
one_day.setText(tday);
You can specify the timezone that needs to be used:
Calendar c = Calendar.getInstance(TimeZone.getTimeZone("Europe/Berlin"));
For a list of available time zones: TimeZone.getAvailableIDs().
Note: the locale in the SimpleDateFormat is used to read/format strings in that locale - using a German locale, for example, will print the months / days names in German. But it has nothing to do with the time zone.
Try using this:
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.GERMANY);
Date date = new Date();
dateFormat.format(date);
You'll get the 'date' in the format "yyyy-MM-dd HH:mm". Then you can use .split() to split the returned string format using space as delimiter. Something like:
String[] str_array = date.toString().split(" ");
String time = str_array[1];
Use android.text.format.Time instead as I suggested over here.

SimpleDateFormat pattern based on locale, but forcing a 4-digit year

I need to build a date format like dd/MM/yyyy. It's almost like DateFormat.SHORT, but contains 4 year digits.
I try to implement it with
new SimpleDateFormat("dd//MM/yyyy", locale).format(date);
However for US locale the format is wrong.
Is there a common way to format date that changes pattern based on locale?
Thank you
I would do it like this:
StringBuffer buffer = new StringBuffer();
Calendar date = Calendar.getInstance();
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
FieldPosition yearPosition = new FieldPosition(DateFormat.YEAR_FIELD);
StringBuffer format = dateFormat.format(date.getTime(), buffer, yearPosition);
format.replace(yearPosition.getBeginIndex(), yearPosition.getEndIndex(), String.valueOf(date.get(Calendar.YEAR)));
System.out.println(format);
Using a FieldPosition you don't really have to care about wheter the format of the date includes the year as "yy" or "yyyy", where the year ends up or even which kind of separators are used.
You just use the begin and end index of the year field and always replace it with the 4 digit year value and that's it.
java.time
Here’s the modern answer. IMHO these days no one should struggle with the long outdated DateFormat and SimpleDateFormat classes. Their replacement came out in the modern Java date & time API early in 2014, the java.time classes.
I am just applying the idea from Happier’s answer to the modern classes.
The DateTimeFormatterBuilder.getLocalizedDateTimePattern method generates a formatting pattern for date and time styles for a Locale. We manipulate the resulting pattern string to force the 4-digit year.
LocalDate date = LocalDate.of( 2017, Month.JULY, 18 );
String formatPattern =
DateTimeFormatterBuilder.getLocalizedDateTimePattern(
FormatStyle.SHORT,
null,
IsoChronology.INSTANCE,
userLocale);
formatPattern = formatPattern.replaceAll("\\byy\\b", "yyyy");
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(formatPattern, userLocale);
String output = date.format(formatter);
Example output:
For Locale.US: 7/18/2017.
For each of UK, FRANCE, GERMANY and ITALY: 18/07/2017.
DateTimeFormatterBuilder allows us to get the localized format pattern string directly, without getting a formatter first, that’s convenient here. The first argument to getLocalizedDateTimePattern() is the date format style. null as second argument indicates that we don’t want any time format included. In my test I used a LocalDate for date, but the code should work for the other modern date types too (LocalDateTime, OffsetDateTime and ZonedDateTime).
I have similar way to do this, but I need to get the locale pattern for the ui controller.
So here's the code
// date format, always using yyyy as year display
DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
SimpleDateFormat simple = (SimpleDateFormat) dateFormat;
String pattern = simple.toPattern().replaceAll("\\byy\\b", "yyyy");
System.out.println(pattern);
Can you not just use java.text.DateFormat class ?
DateFormat uk = DateFormat.getDateInstance(DateFormat.LONG, Locale.UK);
DateFormat us = DateFormat.getDateInstance(DateFormat.LONG, Locale.US);
Date now = new Date();
String usFormat = us.format(now);
String ukFormat = uk.format(now);
That should do what you want to do.

Categories

Resources