Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**
     * Copy the hour and minutes 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 hour and minutes from dateOrigin and rest from dateDestiny
     */
    public static Date copyHourMinute(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 copyHourMinute(calOrigin, calDestiny).getTime();
    }

    /**
     * Copy the hour and minutes from a date to another
     * @param cOrigin Calendar origin where to get the data
     * @param cDestiny Calendar destiny where to set the data
     * @return Calendar with hour and minutes from dateOrigin and rest from dateDestiny
     */
    public static Calendar copyHourMinute(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.HOUR_OF_DAY, cOrigin.get(Calendar.HOUR_OF_DAY));
        cDestiny.set(Calendar.MINUTE, cOrigin.get(Calendar.MINUTE));

        //return the time of destiny
        return cDestiny;
    }
}