/*
* Children Immunization Registry System (IRS). Copyright (C) 2011 PATH (www.path.org)
* 
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
* Author: Tran Trung Hieu
* Email: htran282@gmail.com
*/

package org.hil.util;

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

/**
 *
 * @author Tran Trung Hieu
 */
public class DateUtil {
    public static String getDateString(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String strMonth = month < 10 ? "0" + month : "" + month;       
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        String strDay = day < 10 ? "0" + day : "" + day;
        return strDay + "/" + strMonth + "/" + year;
    }
    
    public static int getFullYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        
        return year;
    }
    
    public static Date getFirstDateMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }
    
    public static Date getFirstDatePreviousMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int yea = calendar.get(Calendar.YEAR);
        int mon = calendar.get(Calendar.MONTH) >= 1 ? calendar.get(Calendar.MONTH) - 1 : 11;
        if (calendar.get(Calendar.MONTH) == 0)
            calendar.set(Calendar.YEAR, yea-1);        
        calendar.set(Calendar.MONTH, mon);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        
        return calendar.getTime();
    }
    
    public static String getMonthYear(Date date, String glue) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        String strMonth = month < 10 ? "0" + month : "" + month; 
        return strMonth + glue + year;
    }
}
