Skip to content

Commit

Permalink
fix: fix date formatting with Java 20 (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
titivermeesch committed Feb 26, 2024
1 parent ee8123d commit 40b9b58
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ java {
}

group = 'me.playbosswar.com'
version = '8.5.2'
version = '8.5.3'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -61,7 +61,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer'
version = '8.5.2'
version = '8.5.3'

from components.java
}
Expand Down
4 changes: 2 additions & 2 deletions java8-build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ java {
}

group = 'me.playbosswar.com'
version = '8.5.2'
version = '8.5.3'
description = 'CommandTimer'

repositories {
Expand Down Expand Up @@ -69,7 +69,7 @@ publishing {
maven(MavenPublication) {
groupId = 'me.playbosswar.com'
artifactId = 'commandtimer'
version = '8.5.2'
version = '8.5.3'

from components.java
}
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/me/playbosswar/com/utils/Files.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
package me.playbosswar.com.utils;

import com.google.gson.GsonBuilder;
import io.sentry.ITransaction;
import io.sentry.Sentry;
import me.playbosswar.com.CommandTimerPlugin;
import me.playbosswar.com.api.events.EventCondition;
import me.playbosswar.com.api.events.EventConfiguration;
import me.playbosswar.com.api.events.EventExtension;
import me.playbosswar.com.conditionsengine.validations.Condition;
import me.playbosswar.com.conditionsengine.validations.ConditionType;
import me.playbosswar.com.conditionsengine.validations.SimpleCondition;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.time.LocalTime;
import java.util.Date;

public class GsonConverter {
protected final Gson gson;

public GsonConverter() {
final GsonBuilder builder = new GsonBuilder();
builder
this.gson = builder
.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE)
.setPrettyPrinting()
.registerTypeAdapter(LocalTime.class, new GsonLocalTime());
this.gson = builder.create();
.registerTypeAdapter(Date.class, new GsonDate())
.registerTypeAdapter(LocalTime.class, new GsonLocalTime())
.create();
}

public <T> String toJson(T obj) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/me/playbosswar/com/utils/gson/GsonDate.java
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));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
main: me.playbosswar.com.CommandTimerPlugin
name: "CommandTimer"
version: "8.5.2"
version: "8.5.3"
description: "Schedule commands like you want"
author: PlayBossWar
api-version: 1.13
Expand Down

0 comments on commit 40b9b58

Please sign in to comment.