Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

674
Vistas
From post man(rest service) how to send json date(string format) to java which accepts date object

How to bind dateOfJoining value(String type) to member variable "dateOfJoining"(Date type) in "DateInput" Class after sending below JSON through postman. How to convert String to Date object in java with the same format dd/MM/yyyy. Converted date should be in Date object but not String.

Json is as given below

{
 "dateOfJoining" : "03/04/2017"
}

Service URL hitting in postman -- localhost/Rest/hello

RestService class in java - HandleRestRequest.java

@RestController
public class HandleRestRequest
{

  @RequestMapping("\hello");
  public List setRequestParams(@RequestBody DateInput dateInput)
 {
   .......
  }
 }

 Pojo Class DateInput.java

  public class DateInput
 {
  private  Date dateOfJoining;
   .......
  }

If I send date from json in below format, its throwing error as unsatisfied input

 {
  "dateOfJoining" : 04/04/2017
 }

So I sending it as string format and by changing dateOfJoining as string in DateInput.java file and later I tried to convert it as date object as below

Modified DateInput.java file from Date to String

 public class DateInput
 {
  private  String dateOfJoining;
   .......
 }

Modified JSON

{
 "dateOfJoining" : "04/04/2017"
}

Code to convert user input from String to Date

 DateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
  String convertedDate = sdf.format(dateInput.getDateOfJoining());

Its converting into required format but return type is String but expected is Date object to send DAO layer. So I tried sdf.parse, its returning Date object but not in required format

Date date = sdf.parse(sdf.format(dateInput.getDateOfJoining()));
output is like -  Tue Apr 04 00:00:00 IST 2017
expected is 04/04/2017 (with return type Date object).

So please help me how to convert the string to date object with required format since DAO layer is expecting input as date object in format dd/MM/yyyy

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Edit: Updating answer in accordance with updated question.

Use annotation @JsonFormat from Jackson Databind to specify the date pattern.

public class DateInput
 {
  @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="dd-MM-yyyy")
  private  Date dateOfJoining;
   .......
  }
over 4 years ago · Santiago Trujillo Denunciar

0

With JSON-B (included in Java EE 8) you can do this:

class DateInput {
    @JsonbDateFormat("dd/MM/yyyy")
    public Date dateOfJoining;
}

In my tests with Thorntail 2.4 I don't need any annotation for ISO format when using java.util.Date:

{
    "dateOfJoining" : "2019-04-28T14:45:15"
}
over 4 years ago · Santiago Trujillo Denunciar

0

change your code to the below code snippet

public List setRequestParams(@RequestParam("dateOfJoining")@DateTimeFormat(pattern="dd-MM-yyyy")  DateInput dateInput)
{
   ...
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda