-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
fix Java9 DateFormat changes #1211
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
gson/src/main/java/com/google/gson/internal/PreJava9DateFormatProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2017 The Gson authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.google.gson.internal; | ||
|
||
import java.text.DateFormat; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Provides DateFormats for US locale with patterns which were the default ones before Java 9. | ||
*/ | ||
public class PreJava9DateFormatProvider { | ||
|
||
/** | ||
* Returns the same DateFormat as {@code DateFormat.getDateInstance(style, Locale.US)} in Java 8 or below. | ||
*/ | ||
public static DateFormat getUSDateFormat(int style) { | ||
return new SimpleDateFormat(getDateFormatPattern(style), Locale.US); | ||
} | ||
|
||
/** | ||
* Returns the same DateFormat as {@code DateFormat.getDateTimeInstance(dateStyle, timeStyle, Locale.US)} | ||
* in Java 8 or below. | ||
*/ | ||
public static DateFormat getUSDateTimeFormat(int dateStyle, int timeStyle) { | ||
String pattern = getDatePartOfDateTimePattern(dateStyle) + " " + getTimePartOfDateTimePattern(timeStyle); | ||
return new SimpleDateFormat(pattern, Locale.US); | ||
} | ||
|
||
private static String getDateFormatPattern(int style) { | ||
switch (style) { | ||
case DateFormat.SHORT: | ||
return "M/d/yy"; | ||
case DateFormat.MEDIUM: | ||
return "MMM d, y"; | ||
case DateFormat.LONG: | ||
return "MMMM d, y"; | ||
case DateFormat.FULL: | ||
return "EEEE, MMMM d, y"; | ||
default: | ||
throw new IllegalArgumentException("Unknown DateFormat style: " + style); | ||
} | ||
} | ||
|
||
private static String getDatePartOfDateTimePattern(int dateStyle) { | ||
switch (dateStyle) { | ||
case DateFormat.SHORT: | ||
return "M/d/yy"; | ||
case DateFormat.MEDIUM: | ||
return "MMM d, yyyy"; | ||
case DateFormat.LONG: | ||
return "MMMM d, yyyy"; | ||
case DateFormat.FULL: | ||
return "EEEE, MMMM d, yyyy"; | ||
default: | ||
throw new IllegalArgumentException("Unknown DateFormat style: " + dateStyle); | ||
} | ||
} | ||
|
||
private static String getTimePartOfDateTimePattern(int timeStyle) { | ||
switch (timeStyle) { | ||
case DateFormat.SHORT: | ||
return "h:mm a"; | ||
case DateFormat.MEDIUM: | ||
return "h:mm:ss a"; | ||
case DateFormat.FULL: | ||
case DateFormat.LONG: | ||
return "h:mm:ss a z"; | ||
default: | ||
throw new IllegalArgumentException("Unknown DateFormat style: " + timeStyle); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2017 The Gson authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.gson.util; | ||
|
||
/** | ||
* Utility to check the major Java version of the current JVM. | ||
*/ | ||
public class VersionUtils { | ||
|
||
private static final int majorJavaVersion = determineMajorJavaVersion(); | ||
|
||
private static int determineMajorJavaVersion() { | ||
String[] parts = System.getProperty("java.version").split("[._]"); | ||
int firstVer = Integer.parseInt(parts[0]); | ||
if (firstVer == 1 && parts.length > 1) { | ||
return Integer.parseInt(parts[1]); | ||
} else { | ||
return firstVer; | ||
} | ||
} | ||
|
||
/** | ||
* @return the major Java version, i.e. '8' for Java 1.8, '9' for Java 9 etc. | ||
*/ | ||
public static int getMajorJavaVersion() { | ||
return majorJavaVersion; | ||
} | ||
|
||
/** | ||
* @return {@code true} if the application is running on Java 9 or later; and {@code false} otherwise. | ||
*/ | ||
public static boolean isJava9OrLater() { | ||
return majorJavaVersion >= 9; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this logic work on Android?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question! For Android, "java.version" is "0", so the method getMajorJavaVersion() returns "0", and isJava9OrLater() returns "false". Guess it is fine until Android is actually moved to Java 9 (if it will ever happen).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://developer.android.com/about/versions/13/features#core-libraries
Android is moving to newer Java implementations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@seventhmoon, thanks for pointing that out. Would you mind creating a separate issue so that we can investigate there if / which effect this will have on Gson and its
VersionUtils.isJava9OrLater()
method?Edit: Follow-up issue #2365