java GSon 是否弄乱了时间戳变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14939395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:56:58  来源:igfitidea点击:

Does GSon mess up Timestamp variables

javatimestampgson

提问by Lolmewn

I'm trying to send a class over sockets, and this all works fine. However, one of the variables gets messed up for no apparent reason. Let me explain further.

我正在尝试通过套接字发送一个类,这一切正常。但是,其中一个变量无缘无故地搞砸了。让我进一步解释一下。

The code I'm using is the following (For the Client socket, where the GSon gets created):

我使用的代码如下(对于创建 GSon 的客户端套接字):

while(!someQueueVariable.isEmpty()){
     QueryHolder h = this.someQueueVariable.poll();
     Gson g = new Gson();
     String send = g.toJson(h);
     out.println(send);
}

QueryHolder is a simple class that contains two Stringsand an Object[].

QueryHolder 是一个简单的类,包含两个Strings和一个Object[].

I tried Netbeans' built-in Debugger, and these variables were present: VariablesThe ones highlighted in blue are the ones you should look at. As you can see, there first was a Timestamp object with the value of 2013-02-18 15:49:36.415, which got turned into Feb 18, 2013 3:49:36PM. Am I doing something wrong here? Is it a bug in GSon?

我尝试了 Netbeans 的内置调试器,并且存在这些变量:变量以蓝色突出显示的变量是您应该查看的变量 。如您所见,首先有一个值为 的 Timestamp 对象2013-02-18 15:49:36.415,它变成了Feb 18, 2013 3:49:36PM。我在这里做错了吗?这是GSon中的错误吗?

回答by Brian Roach

The Gson User's Guidementions this when talking about creating custom serializers/deserializers. What you're seeing is the default serialization for your java.sql.Timestampobject (which is a subclass of Date) which is to output/format it in the format for your locale.

GSON用户指南谈到创建自定义的串行器/解串器时提到了这一点。您看到的是您的java.sql.Timestamp对象(它是 的子类Date)的默认序列化,即以您的语言环境的格式输出/格式化它。

If you look at the Javadoc for GsonBuilder()you'll find a setDateFormat()method that was created specifically for your issue - it no longer requires a custom serializer. You just need to provide the pattern you want in your JSON:

如果您查看GsonBuilder()Javadoc,您会发现一个setDateFormat()专门为您的问题创建的方法 - 它不再需要自定义序列化程序。你只需要在你的 JSON 中提供你想要的模式:

public static void main(String[] args)
{
    Timestamp t = new Timestamp(System.currentTimeMillis());
    System.out.println(t);
    System.out.println(t.toLocaleString());
    String json = new Gson().toJson(t);
    System.out.println(json);
    json = new GsonBuilder()
               .setDateFormat("yyyy-MM-dd hh:mm:ss.S")
               .create()
               .toJson(t);

    System.out.println(json);
}    

Output (As of right now, obviously):

输出(显然,截至目前):

2013-02-18 11:32:21.825
Feb 18, 2013 11:32:21 AM
"Feb 18, 2013 11:32:21 AM"
"2013-02-18 11:32:21.825"

2013-02-18 11:32:21.825
2013 年 2 月 18 日上午 11:32:21
“2013 年 2 月 18 日上午 11:32:21”
“2013-02-18 11:32:21.825”

回答by Brian Agnew

2013-02-18 15:49:36.415, which got turned into Feb 18, 2013 3:49:36PM

2013-02-18 15:49:36.415,变成了2013年2月18日3:49:36PM

and these are different how ? This simply looks like a rendering issue and your timestamp is being converted to a string via a SimpleDateFormator similar.

和这些有什么不同?这看起来像是一个渲染问题,您的时间戳正在通过SimpleDateFormat或类似方法转换为字符串。