Obscure Java date Processing

Source: Internet
Author: User
Tags date now time and date

The purpose of this article is to yell at the obscure status quo of Date Processing in the java standard library. Make sure that you know joda before you refute it ). The cause of our problem is that the delivered customers do not accept (for some reason) the use of open source resources in their projects.

My colleagues and I recently developed a simple Java application in the management library. Yesterday, I began to write a module to calculate the fines for expired books. To complete this module, I had to calculate the number of days between the deadline and the current date. In fact, this is the subtraction of two dates, but the java standard library makes this very complicated.

These two dates are saved in the date object. In order to do subtraction, I must do this:

  1. Convert them to calendar objects
  2. The clearing time is midnight.
  3. Convert them to epoch-based millisecond values
  4. Subtraction in milliseconds
  5. Divided by 86,000,000 (converted to milliseconds per day)

The code I wrote is as follows:

Private int daysbetween (date now, date returndate ){
Calendar cnow = calendar. getinstance ();
Calendar creturndate = calendar. getinstance ();
Cnow. settime (now );
Creturndate. settime (returndate );
Settimetomidnight (cnow );
Settimetomidnight (creturndate );
Long todayms = cnow. gettimeinmillis ();
Long returnms = creturndate. gettimeinmillis ();
Long intervalms = todayms-returnms;
Return millisecondstodays (intervalms );
}

Private int millisecondstodays (long intervalms ){
Return (INT) (intervalms/(1000*86400 ));
}

Private void settimetomidnight (calendar ){
Calendar. Set (calendar. hour_of_day, 0 );
Calendar. Set (calendar. Minute, 0 );
Calendar. Set (calendar. Second, 0 );
}

There may be better methods, but this can be thought of after many iterations yesterday afternoon. You can imagine that the java standard library has been built with a lot of wisdom and energy. The basic functions of such date subtraction must have been designed.

I have encountered this problem earlier. I used other class libraries to solve this problem before, but in the current situation, it has to be the only choice.

Looking at the date/calendar of the java standard library reminds me of a lesson I learned 10 years ago. At that time, I was trying to develop a C ++ reusable class library. I remember what stroustrup said at that time: "The only reusable class libraries are those that have already been reused ." In other words, if you decide to develop a class library using an imaginary realistic and difficult method, it is doomed to fail. From another perspective, you can only participate in multiple real projects and provide them with effective components to ensure the successful creation of the framework.

I don't know how the data/Calendar class library is created, but it exposes the illusion that reality is difficult. In addition, at least I think it is not good enough.

 

(Tim's opinion)

I remember the saying that the difference between the use of software and the waste depends on whether the person who develops it develops for himself or for others. The former creates valuable software, the latter can only be a waste product. It still seems like a classic talk. When I see the date library of Java or MFC, or other libraries, and then look at Perl, Python, Ruby, or even c ++, I can see from this. Although good things will gradually fall behind, at least they are useful.

 

 

Annotation 1, joda, a well-known Java open-source class library, is more powerful and useful than the java standard library in terms of processing time and date. If you are interested in this, you can find details on the SourceForge website.

(Original URL: English blog URL:) http://butunclebob.com/articles.unclebob.javadates; Robert C. martinhttp: // www.butunclebob.com/articles.unclebob

Author profile: Robert C. Martin is the president of object mentor and a senior consultant in the field of Object design, pattern, UML, Agile Methodology and extreme programming. He is not only the author of Jolt's award-winning book Agile Software Development: Principles, models and practices (Chinese version) (Agile Software Development (English version, or the author of the bestselling book Designing object-oriented C ++ applications using the booch method. Martin is the editor of pattern development ages of Program Design 3 and more C ++ gems, and has co-authored XP in practice with James Newkirk. He is a well-known speaker at the international conference of programmers and has been an editor of C ++ report for four years.

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.