Sort ArrayList of custom Objects by property

Since Date implements Comparable, it has a compareTo method just like String does. So your custom Comparator could look like this: public class CustomComparator implements Comparator<MyObject> { @Override public int compare(MyObject o1, MyObject o2) { return o1.getStartDate().compareTo(o2.getStartDate()); } } The compare() method must return an int, so you couldn’t directly return a boolean like you … Read more

Get the current year in JavaScript

Create a new Date() object and call getFullYear(): new Date().getFullYear() // returns the current year Example usage: a page footer that always shows the current year: document.getElementById(“year”).innerHTML = new Date().getFullYear(); footer { text-align: center; font-family: sans-serif; } <footer> ©<span id=”year”></span> by Donald Duck </footer> See also, the Date() constructor’s full list of methods.

Where can I find documentation on formatting a date in JavaScript?

I love 10 ways to format time and date using JavaScript and Working with Dates. Basically, you have three methods and you have to combine the strings for yourself: getDate() // Returns the date getMonth() // Returns the month getFullYear() // Returns the year Example: var d = new Date(); var curr_date = d.getDate(); var … Read more

How to add days to Date?

You can create one with:- Date.prototype.addDays = function(days) { var date = new Date(this.valueOf()); date.setDate(date.getDate() + days); return date; } var date = new Date(); console.log(date.addDays(5)); This takes care of automatically incrementing the month if necessary. For example: 8/31 + 1 day will become 9/1. The problem with using setDate directly is that it’s a … Read more