johanley / date4j Goto Github PK
View Code? Open in Web Editor NEWLightweight alternative to Java's built-in date-time classes. Android-friendly. Compiles under JDK 1.5.
License: Other
Lightweight alternative to Java's built-in date-time classes. Android-friendly. Compiles under JDK 1.5.
License: Other
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.
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:
minus
method to subtract 6 months, with DateTime.DayOverflow.FirstDay
.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.
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.
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?
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...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.