Git Product home page Git Product logo

date4j's People

Contributors

johanley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

date4j's Issues

No dateTime.plusMonths(), dateTime.plusYears() etc.

Where is only plus(Integer aNumYears, Integer aNumMonths, Integer aNumDays, Integer aNumHours, Integer aNumMinutes, Integer aNumSeconds, Integer aNumNanoseconds, DayOverflow aDayOverflow) and plusDays(Integer aNumDays) methods in DateTime class. Why there's no plusMonths(Integer aNumMonths), plusYears(Integer aNumYears) etc? Same for the minus.

Unexpected results when using the minus method with DayOverflow

After carefully reading the [API docs for DayOverflow](http://www.date4j.net/javadoc/hirondelle/date4j/DateTime.html#minus%28java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, java.lang.Integer, hirondelle.date4j.DateTime.DayOverflow%29) and API docs for DayOverflow, we encountered some unexpected behavior.

Scenario:

  • Create a DateTime.
  • Use the minus method to subtract 6 months, with DateTime.DayOverflow.FirstDay.
  • For some days of the year, this causes an unexpected result, e.g. for 2015-08-31 this selects 2015-01-01, whereas I would expect it to return 2015-02-01.

Here's an example test calculating all the dates in 2015 which hit this unexpected scenario:

     public void testGetDateSixMonthsBefore() throws Exception {
        DateTime fakeDate = DateTime.forDateOnly(2015, 1, 1);

        int count = 365;

        while (count > 0) {
            DateTime sixMonthsEarlier = fakeDate.minus(0, 6, 0, 0, 0, 0, 0, DateTime.DayOverflow.FirstDay);

            int days = sixMonthsEarlier.numDaysFrom(fakeDate);

            if (days > 184) {
                String s = fakeDate.format("YYYY-MM-DD") + " goes back " + days + " days";
                Log.w("", s);
            }

            fakeDate = fakeDate.plusDays(1);
            count--;
        }
     }

Example output:

W/: 2015-03-31 goes back 242 days
W/: 2015-05-31 goes back 242 days
W/: 2015-08-29 goes back 240 days
W/: 2015-08-30 goes back 241 days
W/: 2015-08-31 goes back 242 days
W/: 2015-10-31 goes back 244 days
W/: 2015-12-31 goes back 244 days

This issue caused a major production bug for us, as it resulted in our client sending an invalid date range to our server APIs, so a major feature completely stopped working. We didn't catch it during testing as we developed and tested between 31st May and 29th August. :-(

The minusDays method does not hit any similar issue, presumably because it only ever adjusts the date to valid days.

comparing 2 dates

I would like to point out an issue that might not be an issue after all but let me take you through my example..

i want to compare a date to another date without the hours , minutes ,seconds etc.

the one date is now without the hh mm ss

  //Now Date
        DateTime now = DateTime.now(TimeZone.getDefault()).getStartOfDay();

so the other date comes from a feed where the format is "YYYY-MM-DD"

so i create the date from this format
DateTime date_to_compare = new DateTime(date_strStartDate);

if the date_to_compare is the same day as today,

it doesn't equal the now date.

because date_to_compare has null values for hours, minutes,seconds etc.

so in the end this never returns true even though its the same date.

if (now .gteq(date_to_compare ) && now .lteq(date_to_compare )   ){
   return true;
}

in order for this to work i came down to this idea.
DateTime date_to_compare = new DateTime(date_strStartDate+" 00:00:00.0000");
i explicitely add hours minutes, seconds to the date string in order to give the constructor some values.
then the previous statement returns true finally.

but this got me headache for 4 days to understand. i am making this an issue so if you would like to address it as a solution to change the

default null values of the constructor to zeros 00:00:00.0000.

Make it easy to open the project and run it in Android Studio

The project structure doesn't match the Android Studio defaults, which means the source code doesn't get compiled, and you can't run tests. This is a shame. :-( If you are committed to the current project structure then maybe we need a build.gradle to customize the expected locations?

The .format() is really expensive on Android devices

I've used date4j on a project because this project uses the CalDroid library by @roomorama, and I've seen that this line of code:

return dateTime.format("YYYY-MM-DD");

takes about 6ms to complete!

Now I'm using:

` String year = String.valueOf(dateTime.getYear());

    int monthInt = dateTime.getMonth();
    String monthString = String.valueOf(monthInt);
    if (monthInt < 10) {
        monthString = "0" + monthString;
    }

    int dayInt = dateTime.getDay();
    String dayString = String.valueOf(dayInt);
    if (dayInt < 10) {
        dayString = "0" + dayString;
    }

    return year + "-" + monthString + "-" + dayString;`

which takes 0ms...

On a laptop there's almost no difference between the two options...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.