Determine number of days represented by a time range in Java - date

Using Java 8 time I am simply trying to figure out the number of days represented in a time range. Consider the following:
LocalDate start = LocalDate.of(2016, Month.MARCH, 28);
LocalDate end = LocalDate.of(2016, Month.MARCH, 31);
Period period = Period.between(start, end);
The number of days in period is 3 which represents the number of days between the 2 dates, inclusive of start and exclusive of end. What I want is the number of days represented by the 2 dates which is actually 4 (March 28, March 29, March 30, March 31).
I know I can just add 1 to the number of days returned from Period.between() but I guess I was surprised that I couldn't find another call to return exactly what I want. Am I missing something or is adding 1 the only solution?

tl;dr
Always define your spans of time by the Half-Open approach where:
Beginning is inclusive.
Ending is exclusive.
When you want the four days of March 28, March 29, March 30, March 31, make the beginning March 28 and the ending April 1. Run through those dates starting at the first (March 28th) while going up to, but not including, the last (April 1st).
2016-03-28/2016-04-01
Half-Open
Am I missing something
You may be missing an appreciation for the usefulness of the Half-Open approach in defining spans of time.
Generally, the best practice for defining spans of time is the Half-Open approach. In Half-Open, the beginning is inclusive while the ending is exclusive.
This approach solves the problem of dealing with fractional seconds. Intuitively, many programmers will try to find the last possible moment as the ending of a span of time. But that last moment involves an infinitely divisible last second. You might think, "Well, just go to three decimal place for milliseconds, 12:59.59.999 for the end of noon lunch break, as that is all the resolution I will ever need, and that is the resolution of the legacy java.util.Date class.”. But then you would fail to find matches in your database like Postgres that store date-time values with a resolution of microseconds, 12:59:59.999999. So you decide to use six decimal places of fraction, x.999999. But the start experiencing mismatches with date-time values in the java.time classes, and you learn the offer a resolution of nanoseconds for nine digits of fractional second, x.999999999. You can disembark this carousel of frustrating bugs by using the Half-Open approach where the ending runs up to, but does not include, the next whole second.
I believe you will find consistent use of the Half-Open approach throughout your date-time handling code (whether fractional seconds may be involved or not) will:
Make your code easier to read and comprehend.
Ease the cognitive load overall.Knowing all your spans of time carry the same definition eliminates ambiguity.
Reduce bugs.
Examples:
A noon lunch period starts at the moment the clock strikes noon (12:00:00) and runs up to, but does not include, the moment when the clock strikes one o’clock. That means 12:00:00 to 13:00:00.
A full day starts at the first moment of the day (not always 00:00:00, by the way) and runs up to, but does not include, the first moment of the following day.
A week starts on a Monday and runs up to, but does not include, the following Monday. That means seven days in Monday-Monday.
A month starts of the first of the month and runs up to, but not including, the first of the following month. So the month of March is March 1 to April 1.
LocalDate
Rather than one adding 1 to get a total of days, define your span of time as Half-Open: beginning-is-inclusive, ending-is-exclusive. If you are trying to represent the four dates of March 28, 29, 30, and 31, then I suggest you define a span of time from March 28 to April 1.
LocalDate start = LocalDate.of( 2016, Month.MARCH, 28 ) ; // inclusive
LocalDate stop = LocalDate.of( 2016, Month.APRIL, 1 ) ; // exclusive
Period
The java.time classes wisely use the Half-Open approach. So the Period.between method treats the ending as exclusive, as noted in the Question. I suggest you go-with-the-flow here rather than fight it. Search Stack Overflow for many more examples of how well this approach works.
Period p = Period.between( start , stop );
p.toString(): P4D
ChronoUnit
If you want a total number of days, such as 45 for a month and a half, use the ChronoUnit enum, an implementation of TemporalUnit. See this Question for discussion.
Again, the java.time classes use the Half-Open approach. So
long daysBetween = ChronoUnit.DAYS.between( start, stop );
4
Live code
See this example code run live at IdeOne.com.

There doesn't seem to be an inclusive end date method in either Period or LocalDate so it seems that the only thing to do is something like:
Period.between(start, end.plusDays(1))
or
start.until(end.plusDays(1))
(Period.between just calls LocalDate.until)

I belive your day counting differs from java's. Period.between according to documentation: http://docs.oracle.com/javase/8/docs/api/java/time/Period.html#between-java.time.LocalDate-java.time.LocalDate-
"The start date is included, but the end date is not." Having that in mind - yes, adding 1 is the only solution.

the signature of the method is
between(LocalDate startDateInclusive, LocalDate endDateExclusive) {
and the method is not oerloaded, you have no choice other than add 1 to the given result...

Related

How to represent the duration of a calendar year in ISO 8601

I want to define a calendar year with the ISO 8601 standard. Not a specific year, but the general duration. This has to stand for something that has a duration of exactly one calendar year, that starts on the first of January and ends on December 31.
It is not P1Y, I think, because that could be the duration of a year starting anytime. I can only come up something like: P01/12, but that seems ambiguous or not according to the standard syntax.
You may try:
--01-01/P1Y
It’s not very clear to me whether it’s valid, but I think it follows the logic of the ISO 8601 standard. it’s a time interval that begins on January 1 with no year specified and lasts a year.
A time interval is pretty clearly what you are after. According to Wikipedia:
There are four ways to express a time interval:
Start and end, such as "2007-03-01T13:00:00Z/2008-05-11T15:30:00Z"
Start and duration, such as "2007-03-01T13:00:00Z/P1Y2M10DT2H30M"
Duration and end, such as "P1Y2M10DT2H30M/2008-05-11T15:30:00Z"
Duration only, such as "P1Y2M10DT2H30M", with additional context information
So I have taken number 2. from the list.
Link: Wikipedia article ISO 8601, section Time intervals

What Date Time Format is this?

What date time format is this : 735715:37344280
<ExecDateTOD Friendly="Monday April 27, 2015 10:23:00am">735715:37344280</ExecDateTOD>
It's found in C:\Windows\Performance\WinSAT\DataStore\file_name.xml, and is the date time when the Windows Experience Index Assessment test was run.
Any idea how it's structured and can be edited? I need to change it to a previous years Date.
It seems that this format is called VariantTime, in MSDN the call to convert time is called VariantTimeToSystemTime. So it may be number of days, with decimal part after the :.
For the timestamp 735715:37344280
The first number (the one before the colon) is the number of days since the year 0:
735715 / 365 = 2015.66
The second number (the one after the colon) is the number of milliseconds that have passed within the current day.
37344280 / (1000*60*60) = 10.37 hours since start of day
So you can just subtract 365 days from the first number to obtain the previous year like this:
<ExecDateTOD Friendly="Monday April 27, 2014 10:23:00am">735350:37344280</ExecDateTOD>
Note that there were no leap years in either 2015 or 2014, so these year are exactly 365 days long.
Here is a link to a page with another <ExecDateTOD> tag where you can compare: http://www.scribd.com/doc/82935159/2012-01-30-16-00-49-986-Formal-assessment-Recent-WinSAT#scribd
I think that if you subtract 365 from that number, you'll be in previous year.
That number seems to be days since the year 0. The first part might be the number of days, taking into account leap years, etc). The second part the time coded in some way.

Matlab- Changing uniques date code values to more manageable data

I have a variable called sentDate which stores the month and day from Nov 27th - Dec 6th.Each day has a number of sentiment ratings that it represents therefore I need to assign unique day codes to each day so I can perform...
allSents(dayCodes==1)
So far I have managed to assign day codes using...
[a,b,dayCodes]=unique(sentDate);
[d,e,allSents]=unique(sentiment);
However the day codes take the last digit on the date e.g 27th becomes 7, 28th becomes 8, etc. I need it so the day codes start from 1 and increase for each day until the 6th of December, therefore 1-11.
Any idea on how I may do this ?
have you tried the datenum function? then subtract off whatever offset to give the appropriate start day number.
For those who may have a similar problem, by specifying stable in as a parameter e.g
[a,b,dayCodes]=unique(sentDate,'stable');
Will specify the daycodes in the same order as in sentDate.

UniVerse native date format

I am in the process of optimizing some UniVerse data access code we have which uses UniObjects. After some experimentation, it seems that using a UniSession.OConv call to parse certain things such as decimal numbers (most we have a MR4 or MR2 or MR2$) and dates (almost all are D2/) is extremely slow (I think it might make a call back to the server to parse it).
I have already built a parser for the MR*[$] codes, but I was wondering about the dates as they are stored so I can build one for D2/. Usually they seem to be stored as a 5 digit number. I thought it could be number of days since the Unix Epoch since our UniVerse server runs on HP-UX, but after finding '15766' as a last modified date and multiplying it by 86400 (seconds per day), I got March 02, 2013 which doesn't make sense as a last modified date since as far as I know that is still in the future.
Does anyone know what the time base of these date numbers are?
It is stored as a number of days. Just do a conversion on 0 and you will get the start date.
Edit:
As noted by Los, the Epoch used in UniVerse (and UniData) is 31st Dec 1967.
In Universe and any other Pick database, Dates and Times are stored as separate values.
The internal date is the number of days before of after 31/12/1967, which is day zero.
The internal time is the number of seconds after midnight. It can be stored as a decimal but is not normally.
In TCL there is a CDT command (stands for Convert Date) that converts dates from human readable to numeric and and vice versa:
CDT 9/28/2017
* Result: 18169
CDT 18169
* Result: 09/28/2017

Is there a Haskell library for dates?

Is there a function in Haskell that will allow me to enter component of a date (like a string representation or day month year components) that I can get information from (like day of week, days in a month, etc.)?
I've looked online and it looks like there are a lot of custom libraries, but I'm hoping there's one in the standard prelude library of ghci 10.6.4 that's just not well documented?
Are Data.Time.Calendar and Data.Time.Format in the time library sufficient?
You can parse a string representation of a date and get the length of a month using gregorianMonthLength. Not sure about day of the week, though you could format the date as a string using a format that just displays the week day.
A quick Google search turns up this, which may be what you want. It lets you parse strings representing dates and extract information from them.
You can find the day of the week with mondayStartWeek or sundayStartWeek, depending on whether you think a week starts on Monday, or on Sunday. Both functions are in Data.Time.Calendar.OrdinalDate.
λ> snd $ mondayStartWeek $ fromGregorian 2017 10 3
2
In the above example, the return value is 2, which indicates the second day of the week. Since the function is called mondayStartWeek, Monday is the first day, so 2 corresponds to Tuesday. This is true of October 3, 2017.
A warning regarding week numbers
Both functions return a tuple, where the second element is the week day. As far as I can tell, that should be trustworthy.
The first element, however, is the week number of the year. Be careful with that, because the rules for week numbering are political. If I remember correctly, in USA, week 1 is the week that contains January 1. That's not the case in Denmark, where I live. Here, week 1 is the first week where Thursday falls in the new year. This can mean that December 31 can fall in week 1 of the next year. IIRC, this is the rule for many other European countries. Some years, the American and the European week numbers align, but some years, they don't.