0

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

    }
Kai-Sheng Yang
  • 1,535
  • 4
  • 15
  • 21
SpicySandwich
  • 49
  • 1
  • 10
  • Does this answer your question? [How to convert Google proto timestamp to Java LocalDate?](https://stackoverflow.com/questions/52645487/how-to-convert-google-proto-timestamp-to-java-localdate) – Franck May 13 '22 at 17:12
  • Google Protocol Buffer does not have a Date type. See also: https://stackoverflow.com/questions/72231236/how-to-convert-grpc-java-proto-timestamp-to-date You can always declare a field of type String and do with it whatever you want, i.e. let it contain a date. – Franck May 13 '22 at 17:23
  • I think you are right @Franck i will make the proto as "String" then convert it to "Date" and lastly make a validation for Date. – SpicySandwich May 13 '22 at 17:36
  • should we assume you can't change the proto file ? – Clément Jean May 14 '22 at 00:41
  • I change the proto file as String instead of timestamp, its already working @ClémentJean – SpicySandwich May 14 '22 at 01:15
  • The reason for this is, its connected to a sepearated file "Grpc client" with rest api, i cannot have just seconds and nano @ClémentJean – SpicySandwich May 14 '22 at 01:22
  • My question was more related to the fact that you could define your own date format that is serialised more efficiently than a string and more idiomatic. If you are interested let me know. – Clément Jean May 14 '22 at 02:07
  • 1
    I see, i will try that @ClémentJean i think that is much better – SpicySandwich May 14 '22 at 04:35

3 Answers3

2

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.

Clément Jean
  • 1,735
  • 1
  • 14
  • 32
0

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

SpicySandwich
  • 49
  • 1
  • 10
0

Put this in your proto file:

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

and then you can use Date in your proto file, for instance:

message Customer {
  string name = 1;
  string address = 2;
  Date birthDate = 3;
}