alibaba / fastjson2

🚄 FASTJSON2 is a Java JSON library with excellent performance.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE]时间格式化希望支持locale格式化

shiyishiaa opened this issue · comments

请描述您的需求或者改进建议

时间格式化希望支持locale格式化。

请描述你建议的实现方案

    @JSONField(format="yyyy MMMM dd", locale=Locale.ENGLISH)
    private Date today;

附加信息

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Date;

class DateFormatTest {
    @Test
    void enLocaleDateTest() {
        final String date = "{\"today\": \"2022 March 10\"}";
        Assertions.assertAll(() -> JSONObject.parseObject(date, Today.class));
    }

    @Test
    void zhLocaleDateTest() {
        final String date = "{\"today\": \"2022 五月 10\"}";
        Assertions.assertAll(() -> JSONObject.parseObject(date, Today.class));
    }

    private static class Today {
        @JSONField(format = "yyyy MMMM dd")
        private Date today;

        public Date getToday() {
            return today;
        }

        public void setToday(Date today) {
            this.today = today;
        }

        @Override
        public String toString() {
            return "Today{" +
                    "today=" + today +
                    '}';
        }
    }
}

zhLocaleDateTest()可以通过,enLocaleDateTest()失败。

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.3-SNAPSHOT/
已经支持,请用快照版本2.0.3-SNAPSHOT验证

@JSONField(format = "yyyy MMMM dd", locale="zh_CH")

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.3-SNAPSHOT/ 已经支持,请用快照版本2.0.3-SNAPSHOT验证

@JSONField(format = "yyyy MMMM dd", locale="zh_CH")

经测试,使用locale="en_US"enLocaleDateTest()通过。但JavaDoc好像还没有对该field的注释,期待进一步的优化。