'how to make grpc proto "timestamp" change to Date input format?

I want to make timestamp to convert to Date but I was expecting to input Date format ,"NOT" seconds and nano. How do change it to date format input?

This is the insert data picture

@GrpcService
public class ProductGRPCserver extends ProductServiceImplBase {

    @Autowired
    private ProductServiceImpl productServiceImpl;

    public static Date getDateFromTimestamp(Timestamp timestamp) {
        return new Date(Timestamps.toMillis(timestamp));
    }

    @Override
    public void insert(Product request, StreamObserver<APIResponse> responseObserver) {
        ProductEntity productEntity = new ProductEntity();

        Date date = getDateFromTimestamp(request.getProductexpirationdate());

        productEntity.setPurchase_item(request.getPurchaseItem());
        productEntity.setProductname(request.getProductname());
        productEntity.setProductbrand(request.getProductbrand());
        productEntity.setProductprice(request.getProductprice());
        productEntity.setProductdescription(request.getProductdescription());
        productEntity.setProductquantity(request.getProductquantity());
        productEntity.setProductexpirationdate(date);
        System.out.println(date);
        productServiceImpl.saveDataFromDTO(productEntity);

        APIResponse.Builder responce = APIResponse.newBuilder();
        responce.setResponseCode(0).setResponsemessage("Succefull added to database " + productEntity);

        responseObserver.onNext(responce.build());
        responseObserver.onCompleted();

    }


Solution 1:[1]

In order to get more efficient serialization and more descriptive code than just having a string you could do copy the implementation of Date from the Google API repo. If you are working only with Java you will only need to copy this:

syntax = "proto3";

package google.type;

option java_multiple_files = true;
option java_outer_classname = "DateProto";
option java_package = "com.google.type";

message Date {
  int32 year = 1;
  int32 month = 2;
  int32 day = 3;
}

and then you will be able to import with:

import com.google.type.Date;

And obviously you can personalise the package and java_package if needed. After that, this is pretty simple, you just set the year, month and date.

Let me know if you need more help

Solution 2:[2]

Thanks here what i did. I imported the date.proto to other proto file and change the "String" to Date from date.proto @ClémentJean here is photo update

import "date.proto";

......

message Product {
    int32 purchase_item = 1;
    string productname = 2;
    string productbrand = 3;
    double productprice = 4;
    string productdescription = 5;
    int32 productquantity = 6;
    .google.type.Date productexpirationdate = 7;
}
//convert to Date to make it compatible to a Entity Date
@GrpcService
public class ProductGRPCserver  extends ProductServiceImplBase{
    
    @Autowired
    private ProductServiceImpl productServiceImpl;
    
//convert Date from date.proto to Java "Date".
    public static Date getDateFromDateProto(com.google.type.Date date) {
        Integer year = date.getYear();
        Integer month = date.getMonth();
        Integer days = date.getDay();
        
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DATE, days);
        Date datess = calendar.getTime();
        
        return datess;
    }
    @Override
    public void insert(Product request, StreamObserver<APIResponse> responseObserver) {
        ProductEntity productEntity = new ProductEntity();

        productEntity.setPurchase_item(request.getPurchaseItem());
        productEntity.setProductname(request.getProductname());
        productEntity.setProductbrand(request.getProductbrand());
        productEntity.setProductprice(request.getProductprice());
        productEntity.setProductdescription(request.getProductdescription());
        productEntity.setProductquantity(request.getProductquantity());

        //setProductexpirationdate is Date and getProductexpirationdate is customize Date proto to make is compatible. convert it
        productEntity.setProductexpirationdate(getDateFromDateProto(request.getProductexpirationdate()));
    
        productServiceImpl.saveDataFromDTO(productEntity);
        APIResponse.Builder  responce = APIResponse.newBuilder();
        responce.setResponseCode(0).setResponsemessage("Succefull added to database " +productEntity);
    
        responseObserver.onNext(responce.build());
        responseObserver.onCompleted(); 
    }
}

The Code above is sending data with converted Date. Now for retrieving Data is opposite code above as shown below.

Lets just say that im calling the ProductEntity.class method "toProduct" with converted "Java Date" to "proto Date" thats means its opposite to sending data. You can also use the method "getDateFromDateProto" to get Data for 1 row in database like findbyid and get data in 1 row this picture is retrieve data "List"



import java.util.Calendar;
import java.util.Date;

import com.grpcserver.product.ProductServer.Product;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductEntity {
    
    private Integer purchase_item;
    private String productname;
    private String productbrand;
    private Double productprice;
    private String productdescription;
    private Integer productquantity;
    private Date  productexpirationdate;
    
    public Product toProduct(){

        return Product.newBuilder()
                .setPurchaseItem(getPurchase_item())
                .setProductname(getProductbrand())
                .setProductbrand(getProductbrand())
                .setProductprice(getProductprice())
                .setProductdescription(getProductdescription())
                .setProductquantity(getProductquantity())
                .setProductexpirationdate(getDateFromDateProto(getProductexpirationdate()))
                .build();
    }
    
    public static com.google.type.Date getDateFromDateProto(Date date) {
        
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        
        com.google.type.Date datess = com.google.type.Date.newBuilder().setYear(year).setMonth(month).setDay(day).build();
        
        return datess ;
    }
    

    

}

//get list
//"toProduct"
    @Override
    public void findAllRepeated(Product request, StreamObserver<ProductList> responseObserver) {
    
        List<ProductDTO> list = productServiceImpl.getAllPpoduct();
        
        List<Product> products = list.stream().map(ProductDTO::toProduct).collect(Collectors.toList());
        ProductList productList = ProductList.newBuilder().addAllProduct(products)
                .setResultCount(Int64Value.newBuilder().setValue(list.size()).build()).build();
        responseObserver.onNext(productList);
        responseObserver.onCompleted();
        
    }

for Rest Api output postman output via rest api

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Clément Jean
Solution 2