Home > Net >  how to make grpc proto "timestamp" change to Date input format?
how to make grpc proto "timestamp" change to Date input format?

Time:05-14

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();

    }

CodePudding user response:

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

  • Related