Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GsonBuilder.setDateFormat(int) is ignored #1529

Closed
Marcono1234 opened this issue May 6, 2019 · 1 comment · Fixed by #2556
Closed

GsonBuilder.setDateFormat(int) is ignored #1529

Marcono1234 opened this issue May 6, 2019 · 1 comment · Fixed by #2556
Labels

Comments

@Marcono1234
Copy link
Collaborator

As described in https://stackoverflow.com/questions/6873020/gson-date-format, calling Gson.setDateFormat(int) has no effect, the created Gson object will not use the date format.

It appears the reason for this is that the method only sets dateStyle, but timeStyle remains unchanged, i.e. if it has not been changed before, then it is still DateFormat.DEFAULT.

public GsonBuilder setDateFormat(int style) {
this.dateStyle = style;
this.datePattern = null;
return this;

However addTypeAdaptersForDate only uses the styles if timeStyle != DateFormat.DEFAULT.

} else if (dateStyle != DateFormat.DEFAULT && timeStyle != DateFormat.DEFAULT) {

@Marcono1234
Copy link
Collaborator Author

Marcono1234 commented Nov 22, 2023

Actually maybe it would make sense to do the following:

  • Change the dateStyle != DateFormat.DEFAULT && timeStyle != DateFormat.DEFAULT expression in GsonBuilder (see description above) to use || instead of &&, because also for setDateFormat(int, int) this makes a difference:
    // Equivalent to `setDateFormat(DEFAULT, DEFAULT)`
    // > Jan 1, 1970, 1:00:00AM
    DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.DEFAULT, Locale.US).format(new Date(0))
    
    // Equivalent to `setDateFormat(DEFAULT, LONG)` (this currently does not work for Gson)
    // > Jan 1, 1970, 1:00:00AM CET
    DateFormat.getDateTimeInstance(DateFormat.DEFAULT, DateFormat.LONG, Locale.US).format(new Date(0))
  • Deprecate setDateFormat(int) and refer to setDateFormat(int, int) as alternative
    Originally this used DateFormat.getDateInstance and therefore taking only a 'date style' (but no 'time style') made sense; but since commit 2b9fd47 more than 10 years ago it seems this always uses DateFormat.getDateTimeInstance so taking no 'time style' is a bit confusing, and as pointed out in this issue here does not work correctly anyway (also since that same commit probably).
  • Maybe remove the following because they are only called from test code and not from the main code (if necessary adjust the test code to test the other remaining methods instead):
    • the methods createAdapterFactory(int) and createDefaultsAdapterFactory() of DefaultDateTypeAdapter.DateType
    • the constructor DefaultDateTypeAdapter(DateType, int)
    • the methods getDateFormatPattern(int) and getUSDateFormat(int) of PreJava9DateFormatProvider

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
1 participant