How Java transforms Bcl.datetime objects in Protobuf-net

Source: Internet
Author: User

Define a DateTime Message

Reference Document:Https://github.com/mgravell/protobuf-net/blob/master/src/Tools/bcl.proto

message datetime{optional sint64 value =1;//The offset (in units of the selected scale) from 1970/01/01Optional Timespanscale Scale=2[default= days];//The scale of the timespan  enumTimespanscale { days=0; HOURS=1; MINUTES=2; SECONDS=3; MILLISECONDS=4; TICKS=5; Minmax= the;//dubious  }}

A datetime contains two attributes: Value,timespanscale;
Value is the time value, Timespanscale is the time modulus: seconds, microseconds, milli-subtlety, Ticks;
Ticks is a timing cycle in. NET, refer to:https://msdn.microsoft.com/zh-cn/library/system.datetime.ticks.aspx
1ticks=100 subtle, 10000ticks=1 milliseconds;

Second, generate the corresponding Java class

Protoc.exe-i=d:/tmp--java_out=d:/tmp D:/tmp/date_time.proto

Third, parsing c#datetime as Java's java.util.Date

 Public classDatetimeutil {PrivateFinalStatic LongTicks_per_millisecond =10000; /** * Convert the datetime type in C # to date in Java * * @param bcldatetime * @return*/     PublicFinalStaticDate fromdatetimetodate (datetime datetime) {LongTimelong =Datetime.getvalue (); Datetime.timespanscale Timespanscale=Datetime.getscale (); Calendar C=calendar.getinstance (); Timezone.setdefault (Timezone.gettimezone ("UTC")); Switch(Timespanscale) { CaseDays :// * * *C.settimeinmillis (Timelong *86400000); returnC.gettime ();  CaseHOURS:// * *C.settimeinmillis (Timelong *3600000); returnC.gettime ();  CaseMINUTES://* +C.settimeinmillis (Timelong *60000); returnC.gettime ();  CaseSeconds:c.settimeinmillis (Timelong* +); returnC.gettime ();  CaseMilliseconds:c.settimeinmillis (Timelong); returnC.gettime ();  CaseTicks:c.settimeinmillis (Timelong/Ticks_per_millisecond); returnC.gettime (); default: C.settimeinmillis (0L); returnC.gettime (); }    }}

Reference Document:http://www.cnblogs.com/cuyt/p/6141723.html

How Java transforms Bcl.datetime objects in Protobuf-net

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.