Java longTime and C # date conversion (Structure + operator overloading ),

Source: Internet
Author: User

Java longTime and C # date conversion (Structure + operator overloading ),

A few days ago, I was connected to a company's java System for work reasons. When viewing the data, the whole person suddenly got worse, and the date field time in the database is Mao is an integer? I have never been familiar with java before, so I will take the opportunity to understand it. Originally, the System. currentTimeMillis () in java is saved in the database (). The return value is the number of milliseconds that have elapsed since 00:00:00, January 1, January 1, 1970, and is UTC time. I understand why it is an integer, and I use it. net mvc, to correctly display the time, you still need to make a certain amount of conversion, and then think of the simplest method, write a general method, convert this integer.. net DateTime.

A method from the Internet: http://blog.csdn.net/dragonpeng2008/article/details/8681435

Public static DateTime? ConvertJavaDateTimeToNetTime (long time_JAVA_Long) // java long integer date, in milliseconds {DateTime dt_1970 = new DateTime (1970, 1, 1, 0, 0, 0); long tricks_1970 = dt_1970.Ticks; // January 1, 1970 scale long time_tricks = tricks_1970 + time_JAVA_Long * 10000; // Log date scale DateTime dt = new DateTime (time_tricks ). addHours (8); // convert to DateTime return dt ;}

Then encapsulate it by yourself and use it directly.

If you encounter a long time in java, use DateTime dt = ConvertJavaDateTimeToNetTime (1207969641193.

The problem is solved.

 

However, if this problem is frequently encountered, is it very low that I call this method every time?

Think about it. In many java systems, time is saved in this way. Can we customize a type. Is it a java long time type in A. net System?

Now, you have to think of a better method: struct + operator overload.

In addition, there are many basic introductions on the Internet in terms of struct and operator overloading, but it is rarely used in general projects. Generally, classes are used instead (in fact, this is wrong, but I do not care too much ).

Let's give it a try:

Public struct JavaLongDateTime {// <summary> // currentTimeMillis in java /// </summary> public long JavaTimeMillis {get; set ;} /// <summary> /// convert. net DateTime date object // </summary> public DateTime Value {get {return ConvertToDateTime () ;}public JavaLongDateTime (long javaMillis) {JavaTimeMillis = javaMillis ;} # add and subtract public static JavaLongDateTime operator + (JavaLongDateTime o1, JavaLongDateTime o2) {JavaLongDateTime r = new testConsole. javaLongDateTime (); r. javaTimeMillis = o1.JavaTimeMillis + o2.JavaTimeMillis; return r;} public static JavaLongDateTime operator-(JavaLongDateTime o1, JavaLongDateTime o2) {JavaLongDateTime r = new testConsole. javaLongDateTime (); r. javaTimeMillis = region-Hangzhou; return r ;}# endregion # region JavaLongDateTime and integer plus or minus public static JavaLongDateTime operator + (JavaLongDateTime o1, long _ javaMillis) {JavaLongDateTime r = new testConsole. javaLongDateTime (); r. javaTimeMillis = o1.JavaTimeMillis + _ javaMillis; return r;} public static JavaLongDateTime operator-(JavaLongDateTime o1, long _ javaMillis) {JavaLongDateTime r = new testConsole. javaLongDateTime (); r. javaTimeMillis = o1.JavaTimeMillis-_ javaMillis; return r ;}# endregion // implicit conversion // long f = o1; public static implicit operator long (JavaLongDateTime o1) {return o1.Value. ticks;} // explicit conversion // JavaLongDateTime javaDateTime = (JavaLongDateTime) javaMillis; public static explicit operator JavaLongDateTime (long javaMillis) {return new JavaLongDateTime (javaMillis );} private DateTime ConvertToDateTime () {DateTime dt_1970 = new DateTime (1970, 1, 1, 0, 0, 0); long tricks_1970 = dt_1970.Ticks; // January 1, 1970 scale long time_tricks = tricks_1970 + JavaTimeMillis * 10000; // Log date scale DateTime dt = new DateTime (time_tricks ). addHours (8); // convert to DateTime return dt;} public override string ToString () {return this. value. toString ();}}

 

After completing the above Code, we will write another test method:

Public class Program {public static void Main (string [] args) {Encoding. registerProvider (CodePagesEncodingProvider. instance); long javaTimeMillis_1 = 1420335269000; JavaLongDateTime javaDateTime = new JavaLongDateTime (javaTimeMillis_1); Console. writeLine ("Java System. currentTimeMillis: "+ javaTimeMillis_1); Console. writeLine ("=> convert. net DateTime: "+ javaDateTime. value. toString (); Console. writeLine ("direct output:" + javaDateTime. toString (); Console. writeLine ("************************************* ****"); console. writeLine ("test explicit and implicit conversions"); long javaTimeMillis_2 = 1420335269000; JavaLongDateTime javaDateTime1 = (JavaLongDateTime) javaTimeMillis_2; // test Explicit conversions, directly convert from Long to our custom JavaLongDateTime Console. writeLine ("Java System. currentTimeMillis: "+ javaTimeMillis_2); Console. writeLine ("=> convert. net DateTime: "+ javaDateTime1.Value. toString (); long tick = javaDateTime1; // test implicit conversion to convert the custom JavaLongDateTime. net Ticks Console. writeLine ("converted. net DateTime Ticks: "+ tick); Console. readLine ();}}

 

 

 

Run the test:

 

Successful!

 

No exception capture is added to the above Code.

Related Article

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.