What is the most straightforward way to pad empty dates in sql results (on either mysql or perl end)?

When you need something like that on server side, you usually create a table which contains all possible dates between two points in time, and then left join this table with query results. Something like this: create procedure sp1(d1 date, d2 date) declare d datetime; create temporary table foo (d date not null); set d … Read more

What is the accepted way to replace java.util.Date(year,month,day)

The idea is to use the Calendar class, like so: Calendar cal = Calendar.getInstance(); cal.set(year, month, date); Date date = cal.getTime(); Indeed, if you check the Javadoc of the constructor you are mentioning, it is exactly what is suggested: Date(int year, int month, int date) Deprecated. As of JDK version 1.1, replaced by Calendar.set(year + … Read more

Function to return date of Easter for the given year

Python: using dateutil’s easter() function. >>> from dateutil.easter import * >>> print easter(2010) 2010-04-04 >>> print easter(2011) 2011-04-24 The functions gets, as an argument, the type of calculation you like: EASTER_JULIAN = 1 EASTER_ORTHODOX = 2 EASTER_WESTERN = 3 You can pick the one relevant to the US. Reducing two days from the result would … Read more

java: how to mock Calendar.getInstance()?

You can mock it using PowerMock in combination with Mockito: On top of your class: @RunWith(PowerMockRunner.class) @PrepareForTest({ClassThatCallsTheCalendar.class}) The key to success is that you have to put the class where you use Calendar in PrepareForTest instead of Calendar itself because it is a system class. (I personally had to search a lot before I found … Read more

Get the number of days, weeks, and months, since Epoch in Java

java.time Use the java.time classes built into Java 8 and later. LocalDate now = LocalDate.now(); LocalDate epoch = LocalDate.ofEpochDay(0); System.out.println(“Days: ” + ChronoUnit.DAYS.between(epoch, now)); System.out.println(“Weeks: ” + ChronoUnit.WEEKS.between(epoch, now)); System.out.println(“Months: ” + ChronoUnit.MONTHS.between(epoch, now)); Output Days: 16857 Weeks: 2408 Months: 553

Comparing two Calendar objects

Try compareTo Calendar c1 = Calendar.getInstance(); Calendar c2 = Calendar.getInstance(); c1.compareTo(c2); Returns: the value 0 if the time represented by the argument is equal to the time represented by this Calendar; a value less than 0 if the time of this Calendar is before the time represented by the argument; and a value greater than … Read more