Home > Java > javaTutorial > How to format date using Gson library in Java?

How to format date using Gson library in Java?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-19 12:45:23
forward
1068 people have browsed it

How to format date using Gson library in Java?

A Gson is a JSON library for Java, which is created by Google. By using Gson, we can generate JSON and convert JSON to java objects. We can create a Gson instance by creating a GsonBuilder instance and calling with the create() method. The GsonBuilder().setDateFormat() method configures Gson to serialize Date objects according to the pattern provided.

Syntax

public GsonBuilder setDateFormat(java.lang.String pattern)
Copy after login

Example

的中文翻译为:

示例

import java.util.Date;
import com.google.gson.*;
public class DateformatTest {
   public static void main(String[] args) {
      Employee emp = new Employee(115, "Surya", <strong>new Date()</strong>, 25000.00);
      <strong>Gson </strong>gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
      String result = gson.toJson(emp);
      System.out.println(result);
   }
}
// Employee class<strong>
</strong>class Employee {
   private int id;
   private String name;
   private Date doj;
   private double salary;
   public Employee(int id, String name, Date doj, double salary) {
      this.id = id;
      this.name = name;
      this.doj = doj;
      this.salary = salary;
   }
}
Copy after login

输出

{"id":115,"name":"Surya","doj":"2019-09-26","salary":25000.0}
Copy after login

The above is the detailed content of How to format date using Gson library in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template