-2

I am Making an API which handle date in dd-MM-yyyy format. But Using Date object I get yyyy-MM-dd format. I tried to change Date format By many way like this code -

package com.example.internshala.model;


import com.fasterxml.jackson.annotation.JsonProperty;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class Ship {

    private String loadingPoint;
    private String unloadingPoint;
    private String productType;
    private String truckType;
    private int noOfTrucks;
    private int weight;
    private String comment;
    private UUID shipperId;
    private String date;

    //--------------------------------------
    SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");

    //--------------------------------------

    public String getLoadingPoint() {
        return loadingPoint;
    }


    public String getUnloadingPoint() {
        return unloadingPoint;
    }


    public String getProductType() {
        return productType;
    }


    public String getTruckType() {
        return truckType;
    }


    public int getNoOfTrucks() {
        return noOfTrucks;
    }


    public int getWeight() {
        return weight;
    }


    public String getComment() {
        return comment;
    }


    public UUID getShipperId() {
        return shipperId;
    }


    public String getDate() {
        return date;
    }


    public Ship(@JsonProperty("loadingPoint") String loadingPoint,
                @JsonProperty("unloadingPoint") String unloadingPoint,
                @JsonProperty("productType") String productType,
                @JsonProperty("truckType") String truckType,
                @JsonProperty("noOfTrucks") int noOfTrucks,
                @JsonProperty("weight") int weight,
                @JsonProperty("comment") String comment,
                @JsonProperty("shipperId") UUID shipperId,
                @JsonProperty("Date") Date date) {
        this.loadingPoint = loadingPoint;
        this.unloadingPoint = unloadingPoint;
        this.productType = productType;
        this.truckType = truckType;
        this.noOfTrucks = noOfTrucks;
        this.weight = weight;
        this.comment = comment;
        this.shipperId = shipperId;
        String newDate=date.toString();
        this.date=formatter.format(newDate);



    }
}

I also Applied it to direct Date object as Constructor parameter but It give error --com.fasterxml.jackson.databind.exc.ValueInstantiationException

  • 2
    Don't use `Date` or `SimpleDateFormat` in new code. These are legacy classes that have been replaced by the `java.time` API. – David Conrad Oct 22 '21 at 22:43
  • 1
    Additionally, you call `formatter.format` with a `String` which is surely not valid. – David Conrad Oct 22 '21 at 22:48
  • I recommend using standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) formats for exchanging date-time values, such as your API. For a date-only value, that would be YYYY-MM-DD. Bonus: The *java.time* classes use these standard formats by default when parsing/generating text. `LocalDate.of( 2022 , Month.JANUARY , 23 ).toString()` and `LocalDate.parse( "2022-01-23" )`. – Basil Bourque Oct 23 '21 at 00:28
  • 1
    The terrible `SimpleDateFormat`, `Date`, and `Calendar` classes are legacy, supplanted years ago by the modern *java.time* classes defined in JSR 310. – Basil Bourque Oct 23 '21 at 00:29
  • What did your search bring up? Were you awsre that you are supposed to search before posting a question here, and in your question report what you found and how it fell short of answering your question? Please. – Ole V.V. Oct 23 '21 at 09:02
  • Don’t do `private String date;`. In you `Ship` object keep the date as a `LocalDate` (from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/)), not a `String`. – Ole V.V. Oct 25 '21 at 06:47

3 Answers3

0

You should change

    String newDate=date.toString();
    this.date=formatter.format(newDate);

to

    this.date=formatter.format(date);
0

I can't think of any good reason to not save a date in anything other than a date object. (As mentioned Date is outdated, so LocalDate is a better choice.)

From what I see in your code you are using jackson to read/write a file. Instead of changing your own class so it will output what you expect in the file you change the library you are using, in this case jackson, to write and read the values in the format you want them to be.

You have many different ways to do it. For example you can set your format as the default format like this:

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
objectMapper.setDateFormat(df);

Or you only change it for one attribute

public class Ship {
    // Code
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    private Date date;
    // Code
}
magicmn
  • 1,787
  • 7
  • 15
  • Yes, defining Jsonformart worked, But on calling GET method it is returning one day before the entered date. but database showing correct entered date. Only calling get method making issue of wrong date. – Roshan Kannaujiya Oct 23 '21 at 08:53
0

import java.text.SimpleDateFormat; import java.util.Date;

public class SamDateFormat
 {
    public static void main(String[] args)
      {
         Date date = new Date();
          SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
          String currDate= formatter.format(date);
          System.out.println(currDate);
       }
  }
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 23 '21 at 07:36