Why DurationFormatUtils ignore year in format? - java

I have this code:
return DurationFormatUtils.formatDuration(2034430000000L, yyyy-MM-dd_HH:mm:ss.SSS)
the result is: 0000-00-23546_15:26:40.000
why the year and days are 0 and so many days?

Because the documentation says so: DurationFormatUtils.formatDuration().
From the docs:
This method formats durations using the days and lower fields of the format pattern. Months and larger are not used.
The reason is that when you are talking about durations, how long is a month? How long is a year? A month is a variable number of days, as is a year. Now if these durations are anchored, such a thing can make sense; in these cases these are what Joda-Time would call periods.
But in general, a duration is just a number of milliseconds, so months and years really do not make sense.

Related

Get total days from Period [duplicate]

This question already has answers here:
How to calculate the number of days in a period?
(3 answers)
Closed 3 years ago.
I have Period object which comes from api. I have to calculate total days it contains. I found many answers how to get days between two dates, but no one answers how I can get total days exactly from Period object.
E.g:
LocalDate start = LocalDate.now();
LocalDate end = LocalDate.now().plusYears(1);
Period period = Period.between(start, end);
I have only the last object with name period and i have to get 365 days from it.
getDays() returns only days count within one month. And I don't have two dates objects. Only period.
You might wanna read up on the Period Java API Documentation.
Period represents the time duration in this format, "2 years, 3 months and 4 days"
For your case above, its exactly 1 year, 0 Months and 0 days.
When you do a get Days, it will show you 0 days.
Try with an end date of LocalDate end = LocalDate.now().minusDays(398);
The getYears(), getMonths(), getDays() each returns its own value. ;)
Since you already have a Period object, you can do a function to get the number of days from a Period Object using the getYears, getMonths and getDays() methods then sum them together.
The Duration object might help with that.
did you tried import java.time package and use Period class? if you didn't, here's a little help for it https://docs.oracle.com/javase/8/docs/api/java/time/Period.html

get number of days in month with time4j

Is there a way to get number of days in a month using time4j lib?
in android default calendar, we can get it so simple like below
Calendar calendar=Calendar.getInstance();
int numOfDaysInMonth=calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
I mean a standard way, not crazy ways like going to the first Day of next month then come back one day and get day of month.
so can we do that in time4j calendars like "PersianCalendar"
The answer of #محمد علی using the default maximum has a problem: It does not use any calendar context so the maximum in leap years cannot be determined for the last month ESFAND. But the old comment given by #Tunaki is already a good and simple answer:
PersianCalendar today = PersianCalendar.nowInSystemTime();
int lengthOfCurrentMonth = today.lengthOfMonth();
Alternatively, you can also use the element PersianCalendar.DAY_OF_MONTH but then you should determine the contextual maximum, not the default maximum:
PersianCalendar today = PersianCalendar.nowInSystemTime();
int lengthOfCurrentMonth = today.getMaximum(PersianCalendar.DAY_OF_MONTH);
Both expressions will yield the same results in all ways and are completely equivalent.
For standard months (FARVARDIN (1) until BAHMAN (11)) the results will agree with the default maximum. But the last month ESFAND has either 29 days in normal years or 30 days in leap years. Both methods presented here will take this into account (but not the default maximum method).

A Bug in java time calculating months between 2 dates

When using java.time in Scala I experienced a strange behavior. I want to calculate the number of months between two dates like this:
import java.time._
Period.between(LocalDate.parse("2015-03-31"), LocalDate.parse("2015-04-30"))
// java.time.Period = P30D
// I would expect java.time.Period = P1M
Period.between(LocalDate.parse("2015-03-31"), LocalDate.parse("2015-05-01"))
// java.time.Period = P1M1D
Is this a bug or do I have got it all wrong?
org.joda.time works as I would expect it:
import org.joda.time.DateTime
import org.joda.time.Months
Months.monthsBetween( new DateTime().withDate(2015, 3, 31), new DateTime().withDate(2015, 4, 30))
//org.joda.time.Months = P1M
When adding months to a java.time.LocalDate it works fine:
java.time.LocalDate.parse("2015-03-31").plusMonths(1)
// java.time.LocalDate = 2015-04-30
This is not a bug, and it is behaving like expected (see also JDK-8152384 and JDK-8037392, which were closed as "Not An Issue"). Joda Time and the Java Time API have different behaviour regarding this. Quoting Stephen Colebourne from the previous bug report:
The OP appears to want a rule where the days are calculated based on the original month length, not the one that results once the month-year difference is applied. The OP is not wrong, its just that its not how we choose to make the calculation in java.time.
Indeed, from Period.between:
The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign. [...] A month is considered to be complete if the end day-of-month is greater than or equal to the start day-of-month.
Between the 31st of March, and the 30th of April, no complete month has elapsed. As such, you have a period containing the number of days between the two dates, which is 30. To have the complete month of April elapsed, you need to add one day to the end date, and make it the 1st of June.
Joda has a different way of calculating the month period. From Months.monthsBetween:
This method calculates by adding months to the start date until the result is past the end date. As such, a period from the end of a "long" month to the end of a "short" month is counted as a whole month.
Joda explicitly takes the variable number of days in a month into account when calculating the number of months between the two dates. Java Time doesn't.
I agree that it is a bit unexpected, but it is the correct result if you take into account the javadoc.
From the javadoc
The start date is included, but the end date is not. The period is calculated by removing complete months, then calculating the remaining number of days, adjusting to ensure that both have the same sign. The number of months is then split into years and months based on a 12 month year. A month is considered if the end day-of-month is greater than or equal to the start day-of-month. For example, from 2010-01-15 to 2011-03-18 is one year, two months and three days.
The difference comes from what a "complete month" means.
In this case 1st April to 1st May (exclusive) is considered a complete month while 31st March to 30th April (exclusive) is not.
I believe the Period.between is returning P30D in the first example because the second parameter is exclusive. This is according to https://docs.oracle.com/javase/8/docs/api/java/time/Period.html#between-java.time.LocalDate-java.time.LocalDate-
public static Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)

Converting duration to years in Java8 Date API?

I have a date in the far past.
I found out what the duration is between this date and now.
Now I would like to know - how much is this in years?
I came up withthis solution using Java8 API.
This is a monstrous solution, since I have to convert the duration to Days manually first, because there will be an UnsupportedTemporalTypeException otherwise - LocalDate.plus(SECONDS) is not supported for whatever reason.
Even if the compiler allows this call.
Is there a less verbous possibility to convert Duration to years?
LocalDate dateOne = LocalDate.of(1415, Month.JULY, 6);
Duration durationSinceGuss1 = Duration.between(LocalDateTime.of(dateOne, LocalTime.MIDNIGHT),LocalDateTime.now());
long yearsSinceGuss = ChronoUnit.YEARS.between(LocalDate.now(),
LocalDate.now().plus(
TimeUnit.SECONDS.toDays(
durationSinceGuss1.getSeconds()),
ChronoUnit.DAYS) );
/*
* ERROR -
* LocalDate.now().plus(durationSinceGuss1) causes an Exception.
* Seconds are not Supported for LocalDate.plus()!!!
* WHY OR WHY CAN'T JAVA DO WHAT COMPILER ALLOWS ME TO DO?
*/
//long yearsSinceGuss = ChronoUnit.YEARS.between(LocalDate.now(), LocalDate.now().plus(durationSinceGuss) );
/*
* ERROR -
* Still an exception!
* Even on explicitly converting duration to seconds.
* Everything like above. Seconds are just not allowed. Have to convert them manually first e.g. to Days?!
* WHY OR WHY CAN'T YOU CONVERT SECONDS TO DAYS OR SOMETHING AUTOMATICALLY, JAVA?
*/
//long yearsSinceGuss = ChronoUnit.YEARS.between(LocalDate.now(), LocalDate.now().plus(durationSinceGuss.getSeconds(), ChronoUnit.SECONDS) );
Have you tried using LocalDateTime or DateTime instead of LocalDate? By design, the latter does not support hours/minutes/seconds/etc, hence the UnsupportedTemporalTypeException when you try to add seconds to it.
For example, this works:
LocalDateTime dateOne = LocalDateTime.of(1415, Month.JULY, 6, 0, 0);
Duration durationSinceGuss1 = Duration.between(dateOne, LocalDateTime.now());
long yearsSinceGuss = ChronoUnit.YEARS.between(LocalDateTime.now(), LocalDateTime.now().plus(durationSinceGuss1) );
System.out.println(yearsSinceGuss); // prints 600
Although the accepted answer of #Matt Ball tries to be clever in usage of the Java-8-API, I would throw in following objection:
Your requirement is not exact because there is no way to exactly convert seconds to years.
Reasons are:
Most important: Months have different lengths in days (from 28 to 31).
Years have sometimes leap days (29th of February) which have impact on calculating year deltas, too.
Gregorian cut-over: You start with a year in 1415 which is far before first gregorian calendar reform which cancelled full ten days, in England even 11 days and in Russia more. And years in old Julian calendar have different leap year rules.
Historic dates are not defined down to second precision. Can you for example describe the instant/moment of the battle of Hastings? We don't even know the exact hour, just the day. Assuming midnight at start of day is already a rough and probably wrong assumption.
Timezone effects which have impact on the length of day (23h, 24h, 25h or even different other lengths).
Leap seconds (exotic)
And maybe the most important objection to your code:
I cannot imagine that the supplier of the date with year 1415 has got the intention to interprete such a date as gregorian date.
I understand the wish for conversion from seconds to years but it can only be an approximation whatever you choose as solution. So if you have years like 1415 I would just suggest following very simple approximation:
Duration d = ...;
int approximateYears = (int) (d.toDays() / 365.2425);
For me, it is sufficient in historic context as long as we really want to use a second-based duration for such an use-case. It seems you cannot change the input you get from external sources (otherwise it would be a good idea to contact the duration supplier and ask if the count of days can be supplied instead). Anyway, you have to ask yourself what kind of year definition you want to apply.
Side notes:
Your complaint "WHY OR WHY CAN'T JAVA DO WHAT COMPILER ALLOWS ME TO DO?" does not match the character of new java.time-API.
You expect the API to be type-safe, but java.time (JSR-310) is not designed as type-safe and heavily relies on runtime-exceptions. The compiler will not help you with this API. Instead you have to consult the documentation in case of doubt if any given time unit is applicable on any given temporal type. You can find such an answer in the documentation of any concrete implementation of Temporal.isSupported(TemporalUnit). Anyway, the wish for compile-safety is understandable (and I have myself done my best to implement my own time library Time4J as type-safe) but the design of JSR-310 is already set in stone.
There is also a subtile pitfall if you apply a java.time.Duration on either LocalDateTime or Instant because the results are not exactly comparable (seconds of first type are defined on local timeline while seconds of Instant are defined on global timeline). So even if there is no runtime exception like in the accepted answer of #Matt Ball, we have to carefully consider if the result of such a calculation is reasonable and trustworthy.
Use Period to get the number of years between two LocalDate objects:
LocalDate before = LocalDate.of(1415, Month.JULY, 6);
LocalDate now = LocalDate.now();
Period period = Period.between(before, now);
int yearsPassed = period.getYears();
System.out.println(yearsPassed);

How to find which day of the week it is java

I want to find the day of the week in java without the use of date and other methods that do it on theirselves.I can find the daydifference between 2 dates but I cant understand how I can find which day of the week that specific date is.
If you can find the day difference between two days, then just use the mod operator.
For instance, if you know that day1 = Monday, and you want to find which day it is after 701 days, it is Monday + 701 % 7 = Monday + 1 = Tuesday.
There are formulas for figuring out what day of the week a particular day is on:
http://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Purely_mathematical_methods
The approach you've chosen seems extremely complicated and error prone.
I suggest creating your own class named Date.
A date will contain a month, day, and year and will know how to subtract or add days.
It should also be able to determine if it is equal to, before, or after another date.
You should use an array of ints to represent the number of days in each month.
This array can be a private static field of your Date class.
Then you can create an instance of your class that represents date2 and subtract one day at a time until it equals date1.
You can use a similar approach to determine the day of the week by comparing the input dates to a base date with a known day of week.
I'm certain this is what your instructor intended for this assignment.
This will teach you a valuable lesson:
Take advantage of objects to use encapsulation in stead of using a bunch of if statements.

Categories

Resources