Generate random dates within past 12 months - java

I'm using this Java code to generate random dates:
LocalDate localDate = LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
Date date = Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
Is there a way to limit the random dates within last 12 months?

You probably don't want to deal with timezones, leap years, leap seconds etc. yourself, so I recommend using the java.time library (java 8 and up).
If you only want LocalDate-precision, you could get any day within a specified range like this:
LocalDate now = LocalDate.now();
LocalDate then = now.minusYears(1);
long difference = now.toEpochDay() - then.toEpochDay();
int randomDifference = random.nextInt((int) difference);
LocalDate randomDate = then.plusDays(randomDifference);

Related

Subtract fiscal year quarters from current date using Java LocalDate

By using Java LocalDate how can I subtract eight quarters from the current quarter?
For example:
2019 Q3 - (8 x quarter) = 2017 Q4
I've tried using
LocalDate.now(ZoneId.of("Europe/London"));
now.minus(1, IsoFields.QUARTER_OF_YEAR);
but I am getting a negative value and I don't know how to handle this.
You are using QUARTER_OF_YEAR which is of type TimeField. You should use IsoFields.QUARTER_YEARS which is of type TemporalUnit:
Unit that represents the concept of a quarter-year. For the ISO calendar system, it is equal to 3 months.
LocalDate now = LocalDate.now(ZoneId.of("Europe/London"));
LocalDate ago = now.minus(3, IsoFields.QUARTER_YEARS);
System.out.println(ago);

Joda Time - difference in "complete" months between two dates

How to calculate the number of "full" months between two dates with joda time, dropping incomplete months?
For example, I have 2 dates
LocalDate from = new LocalDate (2018, 9, 10);
LocalDate to = new LocalDate (2018, 11, 15);
Between these dates there is one "full" month - October from 1 to 31.
So I want to get is the number "1" by dropping the "incomplete" months - September and November
I need something like this
System.out.println(Months.monthsBetween(from, to).getMonths()); // returns 2
System.out.println(Months.**completed**MonthsBetween(from, to).getMonths()); // returns 1
UPD 1.
I could achieve what I want as follows:
LocalDate from = new LocalDate (2018, 9, 10);
LocalDate to = new LocalDate (2018, 11, 15);
if (to.getDayOfMonth() != 1)
from = from.plusMonths(1).withDayOfMonth(1);
if (to.getDayOfMonth() != 1)
to = to.withDayOfMonth(1);
System.out.println(Months.monthsBetween(from, to).getMonths());
but maybe there is an out of the box method?
Wouldn't this means that you simply want to remove one month from each interval and do a difference between them?
LocalDate fromMinusOne = from.minus(Months.ONE);
LocalDate toMinusOne = to.minus(Months.ONE);
System.out.println(Months.monthsBetween(fromMinusOne, toMinusOne).getMonths());
Joda Time provides methods to extract days, months and years between two dates. Create two instances of you date.
DateTime startDate = DateTime.parse("1970-01-01", DateTimeFormat.forPattern("yyyy-MM-dd"))
DateTime endDate = DateTime.parse("2015-02-25", DateTimeFormat.forPattern("yyyy-MM-dd"))
You can create your date instances in LocalDate as well instead of DateTime.
Now, complete months between above two dates can be found easily with,
int months = Months.monthsBetween(startDate.withDayOfMonth(1), endDate.withDayOfMonth(1)).getMonths()
For days,
int days = Days.daysBetween(startDate, endDate).getDays()
Difference between two dates in months
For years,
int years = Years.yearsBetween(startDate, endDate).getYears();

Java 8 calculate months between two dates

NOTE THIS IS NOT A DUPLICATE OF EITHER OF THE FOLLOWING
Calculating the difference between two Java date instances
calculate months between two dates in java [duplicate]
I have two dates:
Start date: "2016-08-31"
End date: "2016-11-30"
Its 91 days duration between the above two dates, I expected my code to return 3 months duration, but the below methods only returned 2 months. Does anyone have a better suggestion? Or do you guys think this is a bug in Java 8? 91 days the duration only return 2 months.
Thank you very much for the help.
Method 1:
Period diff = Period.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30"));
Method 2:
long daysBetween = ChronoUnit.MONTHS.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30"));
Method 3:
I tried to use Joda library instead of Java 8 APIs, it works. it loos will return 3, It looks like Java duration months calculation also used days value. But in my case, i cannot use the Joda at my project. So still looking for other solutions.
LocalDate dateBefore= LocalDate.parse("2016-08-31");
LocalDate dateAfter = LocalDate.parse("2016-11-30");
int months = Months.monthsBetween(dateBefore, dateAfter).getMonths();
System.out.println(months);
Since you don't care about the days in your case. You only want the number of month between two dates, use the documentation of the period to adapt the dates, it used the days as explain by Jacob. Simply set the days of both instance to the same value (the first day of the month)
Period diff = Period.between(
LocalDate.parse("2016-08-31").withDayOfMonth(1),
LocalDate.parse("2016-11-30").withDayOfMonth(1));
System.out.println(diff); //P3M
Same with the other solution :
long monthsBetween = ChronoUnit.MONTHS.between(
LocalDate.parse("2016-08-31").withDayOfMonth(1),
LocalDate.parse("2016-11-30").withDayOfMonth(1));
System.out.println(monthsBetween); //3
Edit from #Olivier Grégoire comment:
Instead of using a LocalDate and set the day to the first of the month, we can use YearMonth that doesn't use the unit of days.
long monthsBetween = ChronoUnit.MONTHS.between(
YearMonth.from(LocalDate.parse("2016-08-31")),
YearMonth.from(LocalDate.parse("2016-11-30"))
)
System.out.println(monthsBetween); //3
Since Java8:
ChronoUnit.MONTHS.between(startDate, endDate);
//Backward compatible with older Java
public static int monthsBetween(Date d1, Date d2){
if(d2==null || d1==null){
return -1;//Error
}
Calendar m_calendar=Calendar.getInstance();
m_calendar.setTime(d1);
int nMonth1=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
m_calendar.setTime(d2);
int nMonth2=12*m_calendar.get(Calendar.YEAR)+m_calendar.get(Calendar.MONTH);
return java.lang.Math.abs(nMonth2-nMonth1);
}
The documentation of Period#between states the following:
The start date is included, but the end date is not.
Furthermore:
A month is considered if the end day-of-month is greater than or equal to the start day-of-month.
Your end day-of-month 30 is not greater than or equal to your start day-of-month 31, so a third month is not considered.
Note the parameter names:
public static Period between​(LocalDate startDateInclusive, LocalDate endDateExclusive)
To return 3 months, you can increment the endDateExclusive by a single day.
In case you want stick to java.time.Period API
As per java.time.Period documentation
Period between(LocalDate startDateInclusive, LocalDate endDateExclusive)
where
#param startDateInclusive the start date, inclusive, not null
#param endDateExclusive the end date, exclusive, not null
So it is better to adjust your implementation to make your end date inclusive and get your desired result
Period diff = Period.between(LocalDate.parse("2016-08-31"),
LocalDate.parse("2016-11-30").plusDays(1));
System.out.println("Months : " + diff.getMonths());
//Output -> Months : 3
You have to be careful, never use LocalDateTime to calculate months between two dates the result is weird and incorrect, always use LocalDate !
here's is some code to prove the above:
package stack.time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class TestMonthsDateTime {
public static void main(String[] args) {
/**------------------Date Time----------------------------*/
LocalDateTime t1 = LocalDateTime.now();
LocalDateTime t2 = LocalDateTime.now().minusMonths(3);
long dateTimeDiff = ChronoUnit.MONTHS.between(t2, t1);
System.out.println("diff dateTime : " + dateTimeDiff); // diff dateTime : 2
/**-------------------------Date----------------------------*/
LocalDate t3 = LocalDate.now();
LocalDate t4 = LocalDate.now().minusMonths(3);
long dateDiff = ChronoUnit.MONTHS.between(t4, t3);
System.out.println("diff date : " + dateDiff); // diff date : 3
}
}
My 2%
This example checks to see if the second date is the end of that month. If it is the end of that month and if the first date of month is greater than the second month date it will know it will need to add 1
LocalDate date1 = LocalDate.parse("2016-08-31");
LocalDate date2 = LocalDate.parse("2016-11-30");
long monthsBetween = ChronoUnit.MONTHS.between(
date1,
date2);
if (date1.isBefore(date2)
&& date2.getDayOfMonth() == date2.lengthOfMonth()
&& date1.getDayOfMonth() > date2.getDayOfMonth()) {
monthsBetween += 1;
}
After the short investigation, still not totally fix my question, But I used a dirty solution to avoid return the incorrect duration. At least, we can get the reasonable duration months.
private static long durationMonths(LocalDate dateBefore, LocalDate dateAfter) {
System.out.println(dateBefore+" "+dateAfter);
if (dateBefore.getDayOfMonth() > 28) {
dateBefore = dateBefore.minusDays(5);
} else if (dateAfter.getDayOfMonth() > 28) {
dateAfter = dateAfter.minusDays(5);
}
return ChronoUnit.MONTHS.between(dateBefore, dateAfter);
}
The Java API response is mathematically accurate according to the calendar. But you need a similar mechanism, such as rounding decimals, to get the number of months between dates that matches the human perception of the approximate number of months between two dates.
Period period = Period.between(LocalDate.parse("2016-08-31"), LocalDate.parse("2016-11-30"));
long months = period.toTotalMonths();
if (period.getDays() >= 15) {
months++;
}

Populate an arrayList with last days of week(Monday-Sunday) for three months based on current date using Java 8 time API

Developing a timesheet selection page where the selection should be pre-populated with list of end of week dates for last three months. Need an process with O(n) using Java 8 time API.
My current solution checks for if current date is Sunday, if not find out the end of this week and then iterate back for three months. Looking for an optimized solution.
I've answered a similar question HERE.
We've to check how many weeks are in between the last Saturday and the date three months ago. Then it's simple math:
LocalDate lastSunday = LocalDate.now()
.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
long weeks = ChronoUnit.WEEKS.between(lastSunday.minusMonths(3L), lastSunday);
List<LocalDate> collect = Stream.iterate(lastSunday.minusWeeks(weeks), d -> d.plusWeeks(1L))
.limit(weeks + 1)
.collect(Collectors.toList());
In JDK 9 there is a new method, datesUntil.
// today, but make sure you consider time-zones when using this method
LocalDate today = LocalDate.now();
// find the next Sunday
LocalDate endSun = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
// find 3 months earlier (there are 13 weeks in 3 months)
LocalDate startSun = endSun.minusWeeks(13);
// find dates
Stream<LocalDate> dates = startSun.datesUntil(endSun, Period.ofWeeks(1));
A Java 8 answer would be the following (if you're only looking for Sundays):
LocalDate closestSunday = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
LocalDate firstSunday = closestSunday.minusMonths(3);
List<LocalDate> sundays = new ArrayList<>();
for (; !closestSunday.isBefore(firstSunday); closestSunday = closestSunday.minusWeeks(1)) {
sundays.add(closestSunday);
}
I would use a Stream approach, but I'd rather wait 8 days until JDK 9 is released so I can use Stream#takeWhile.
EDIT: If you really want a Stream approach utilizing JDK 8, then the following is logically equivalent to the code above:
LocalDate closestSunday = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY));
LocalDate secondSunday = closestSunday.minusMonths(3).plusWeeks(1);
List<LocalDate> sundays = new ArrayList<>();
Stream.iterate(closestSunday, date -> date.minusWeeks(1))
.peek(sundays::add)
.allMatch(date -> date.isAfter(secondSunday));

How many instances of a partial date (month & day) appear in a range, and its day of the week

In Java, how would I go about constructing a utility that would take a range of dates (start and end date) and then would see how many times a given partial date ( the month and day-of-month) appears in that range, and will add an entry to a list for each match.
In my instance, I want to give it a range of say 5 years - starting Jan 1st 2014 and going to Dec 31st 2019. My check date is the 2nd August. I want the method to return the full information about each match of any August 2 of any year in the range. So for 2014 is will return Saturday 2nd August 2014, then Sunday 2nd August 2015 etc and so on.
I've been trying to get something working so far with Joda Time and the default date/calendar classes in Java and I'm just getting myself in a mess.
Thanks,
S
Edit: How silly of me, apologies for not adding my code :(
public static List<Date> getDaysInRange(Date startdate,
Date enddate,
Date checkDate) {
SimpleDateFormat sdf = new SimpleDateFormat("MMdd");
List<Date> dates = new ArrayList<>();
Calendar cal = new GregorianCalendar();
cal.setTime(startdate);
while (cal.getTime().before(enddate)) {
if (sdf.format(cal.getTime()).equals(sdf.format(checkDate))) {
Date result = cal.getTime();
dates.add(result);
}
cal.add(Calendar.DATE, 1);
}
return dates;
}
Date-Only
Since you want only a date without time-of-day and without time zone, use a date-only class. The old java.util.Date/.Calendar classes lack such a class. And those old classes are notoriously troublesome and flawed.
Instead use either:
Joda-Time
java.time, built into Java 8, inspired by Joda-Time.
Joda-Time
Here is some untested code using Joda-Time 2.6.
The main idea is to focus on the small set of possible year numbers rather than test every day of year. In the example below, that means six date-time values to compare rather than thousands. Besides efficiency, the purpose of the code becomes more apparent.
The arguments to your routine should be a month number and a day-of-month number, a pair of ints or Integers, rather than a Date. As seen in this examples two int variables, month and day.
LocalDate start = new LocalDate( 2011, 2, 3 );
LocalDate stop = new LocalDate( 2016, 4, 5 );
int yearStart = start.getYear();
int yearStop = stop.getYear();
int month = 11;
int day = 22;
for ( i = yearStart, i <= yearStop, i++ )
{
LocalDate x = new LocalDate( i, month, day );
boolean matchStart = ( x.isEqual( start ) || x.isAfter( start ) );
boolean matchStop = x.isBefore( stop ); // Half-Open approach where beginning of range is inclusive while ending is exclusive.
if ( matchStart && matchStop )
{
// Add to collection of LocalDate objects.
// Later you can ask each LocalDate object for its day-of-week.
{
}
java.time
The java.time package also offers a LocalDate class. The code would be similar to the above Joda-Time example.
I think using SimpleDateFormat is a bad idea. Use Calendar for comparison directly, like this
cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH) && cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH)

Categories

Resources