Home > database >  How to convert Date format to dd-MM-yyyy in Java which return a Date object
How to convert Date format to dd-MM-yyyy in Java which return a Date object

Time:10-23

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

CodePudding user response:

You should change

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

to

    this.date=formatter.format(date);

CodePudding user response:

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
}
  • Related