如何使 grpc proto "timestamp" 更改为日期输入格式?

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

我想将时间戳转换为 Date,但我希望输入 Date 格式,“NOT”秒和纳米。怎么改成日期格式输入?

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

    }

为了获得比仅拥有字符串更有效的序列化和更具描述性的代码,您可以从 Google API 存储库中复制 Date 的实现。如果你只使用 Java 你只需要复制这个:

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;
}

然后您将能够导入:

import com.google.type.Date;

显然,如果需要,您可以对 packagejava_package 进行个性化设置。之后就很简单了,设置年月日就可以了。

如果您需要更多帮助,请告诉我

在此感谢我所做的一切。我将 date.proto 导入到其他原型文件并将“字符串”从 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(); 
    }
}

上面的代码正在发送带有转换日期的数据。 现在检索数据是上面相反的代码,如下所示。

我只是说我调用 ProductEntity.class 方法“toProduct”并将“Java 日期”转换为“原型日期”,这意味着它与发送数据相反。您还可以使用方法“getDateFromDateProto”获取数据库中 1 行的数据,如 findbyid 并获取 1 行中的数据 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();
        
    }

休息Api输出postman output via rest api