jeramay10
jeramay10

Reputation: 155

Timestamp value in C# differs from java

I am trying to get a timestamp value from a webservice in C# and then match it to a Java application. However, I am getting a different value in the millisecond part. There is a specific format of the timestamp on the web service so I need to convert the actual timestamp that I am getting and convert it to a string type to be able to get the proper format.

This is the timestamp from a web service

2017-02-09 P14:01:53.1719701+8

My sample code in java

    Date webServiceTimeStamp=service.getApplication().getTimestamp().getTime();

    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd aHH:mm:ss.SSSSSSSX");
    String formattedTimeStamp = formatter.format(webServiceTimeStamp);
     if(formattedTimeStamp.contains("AM"))
     {
         formattedTimeStamp = formattedTimeStamp.replace("AM", "A");
     }else{

            formattedTimeStamp = formattedTimeStamp.replace("PM", "P");
     }
     formattedTimeStamp = formattedTimeStamp.replace("+0", "+");


    System.out.println("timestamp => " + formattedTimeStamp);

Output

timestamp => 2017-02-09 P14:01:53.0000172+8

Is there a way I can get the same millisecond value from the web service? Can anyone help me on how can I get the same value so I will be able to match it to my code. Thank you so much!

Upvotes: 0

Views: 291

Answers (1)

Frederik Heremans
Frederik Heremans

Reputation: 658

Millisecond only has 3 digits of precision, so using SSSSSSS will always pad the miliseconds with zero's in front...

If you really want 6 digits (nanoseconds) using only millisecond precision, use this format instead:

new SimpleDateFormat("yyyy-MM-dd aHH:mm:ss.SSS'000'X")

Upvotes: 1

Related Questions