Programming Tips - Java: The Date class does not have a setMilliseconds() method. How do you do that?

Date: 2010jun19 Language: Java Q. Java: The Date class does not have a setMilliseconds() method. How do you do that? A. Here's a function that does it:
// Helper function // Taken from org.apache.commons.lang.time private static Date set(Date date, int calendarField, int amount) { if (date == null) return null; // getInstance() returns a new object, so this method is thread safe. Calendar c = Calendar.getInstance(); c.setLenient(false); c.setTime(date); c.set(calendarField, amount); return c.getTime(); } public static Date setMilliseconds(Date date, int ms) { return set(date, Calendar.MILLISECOND, ms); }
javaScript already has this method.