Java tutorial
//package com.java2s; //License from project: Apache License import java.util.Calendar; import java.util.Date; public class Main { /** * Copy the year, month and day from a date to another * @param dateOrigin Date origin where to get the data * @param dateDestiny Date destiny where to set the data * @return Date with year month and day from dateOrigin and rest from dateDestiny */ public static Date copyYearMonthDay(Date dateOrigin, Date dateDestiny) { //check null values if (dateOrigin == null && dateDestiny == null) { return Calendar.getInstance().getTime(); } else if (dateOrigin == null) { return dateDestiny; } else if (dateDestiny == null) { return dateOrigin; } //convert to calendars Calendar calOrigin = Calendar.getInstance(); calOrigin.setTime(dateOrigin); Calendar calDestiny = Calendar.getInstance(); calDestiny.setTime(dateDestiny); //return the time of destiny return copyYearMonthDay(calOrigin, calDestiny).getTime(); } /** * Copy the year, month and day from a date to another * @param cOrigin Calendar origin where to get the data * @param cDestiny Calendar destiny where to set the data * @return Date with year month and day from dateOrigin and rest from dateDestiny */ public static Calendar copyYearMonthDay(Calendar cOrigin, Calendar cDestiny) { //check null values if (cOrigin == null && cDestiny == null) { return Calendar.getInstance(); } else if (cOrigin == null) { return cDestiny; } else if (cDestiny == null) { return cOrigin; } //copy year, month and day cDestiny.set(Calendar.YEAR, cOrigin.get(Calendar.YEAR)); cDestiny.set(Calendar.MONTH, cOrigin.get(Calendar.MONTH)); cDestiny.set(Calendar.DAY_OF_MONTH, cOrigin.get(Calendar.DAY_OF_MONTH)); //return the time of destiny return cDestiny; } }