-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix date formatting with Java 20 (#250)
- Loading branch information
1 parent
ee8123d
commit 40b9b58
Showing
6 changed files
with
39 additions
and
11 deletions.
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
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
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,29 @@ | ||
package me.playbosswar.com.utils.gson; | ||
|
||
import com.google.gson.*; | ||
|
||
import java.lang.reflect.Type; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Locale; | ||
|
||
public class GsonDate implements JsonSerializer<Date>, JsonDeserializer<Date> { | ||
|
||
@Override | ||
public Date deserialize(JsonElement jsonElement, Type type, | ||
JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { | ||
SimpleDateFormat formatter = new SimpleDateFormat("MMM d, yyyy, H:mm:ss a", Locale.US); | ||
try { | ||
return formatter.parse(jsonElement.getAsString()); | ||
} catch(ParseException e) { | ||
throw new JsonParseException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonElement serialize(Date date, Type type, JsonSerializationContext jsonSerializationContext) { | ||
SimpleDateFormat sdf = new SimpleDateFormat("MMM d, yyyy, H:mm:ss a", Locale.US); | ||
return new JsonPrimitive(sdf.format(date)); | ||
} | ||
} |
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