/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rjconsultores.ventaboletos.utilerias;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;

public final class DateUtil {

	public static Integer DIA = Integer.valueOf(1000 * 60 * 60 * 24);
	public static Integer UM_DIA = Integer.valueOf(1);

	/**
	 * Formato 24Hs
	 */
	public static String ddMMaaHH24mm = "dd/MM/yyyy HH:mm";
	public static String ddMMaaHH24mmss = "dd/MM/yyyy HH:mm:ss";
	
	/**
	 * Formato 12hs
	 */
	public static String ddMMaaHHmm = "dd/MM/yyyy hh:mm";
	public static String ddMMaaaa = "dd/MM/yyyy";
	public static String ddMMaa_anodoisdigitos = "dd/MM/yy";
	public static String ddMMaa_sembarra = "ddMMyy";
	public static String HHmm = "HH:mm";
	public static String formatGMT = "yyyy-MM-dd'T'HH:mm:ss";
	public static String formatJson = "yyyy-MM-dd'T'HH:mm:ss.SSS";
	public static String formatExp = "yyyyMMdd";
	public static String YYYY_MM_DD_T_HH_MM_SS_Z = "yyyy-MM-dd'T'HH:mm:ssZ";

	/**
	 * No need for an instance
	 */
	private DateUtil() {
	}

	/**
	 * Elapsed days based on current time
	 * 
	 * @param date
	 *            Date
	 * 
	 * @return int number of days
	 */
	public static int getElapsedDias(Date date) {
		return elapsed(date, Calendar.DATE);
	}

	/**
	 * Elapsed days based on two Date objects
	 * 
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * 
	 * @return int number of days
	 */
	public static int getElapsedDias(Date d1, Date d2) {
		return elapsed(d1, d2, Calendar.DATE);
	}

	/**
	 * Elapsed months based on current time
	 * 
	 * @param date
	 *            Date
	 * 
	 * @return int number of months
	 */
	public static int getElapsedMeses(Date date) {
		return elapsed(date, Calendar.MONTH);
	}

	/**
	 * Elapsed months based on two Date objects
	 * 
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * 
	 * @return int number of months
	 */
	public static int getElapsedMeses(Date d1, Date d2) {
		return elapsed(d1, d2, Calendar.MONTH);
	}

	/**
	 * Elapsed years based on current time
	 * 
	 * @param date
	 *            Date
	 * 
	 * @return int number of years
	 */
	public static int getElapsedAnos(Date date) {
		return elapsed(date, Calendar.YEAR);
	}

	/**
	 * Elapsed years based on two Date objects
	 * 
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * 
	 * @return int number of years
	 */
	public static int getElapsedAnos(Date d1, Date d2) {
		return elapsed(d1, d2, Calendar.YEAR);
	}

	/**
	 * All elaspsed types
	 * 
	 * @param g1
	 *            GregorianCalendar
	 * @param g2
	 *            GregorianCalendar
	 * @param type
	 *            int (Calendar.FIELD_NAME)
	 * 
	 * @return int number of elapsed "type"
	 */
	private static int elapsed(GregorianCalendar g1, GregorianCalendar g2, int type) {
		GregorianCalendar gc1, gc2;
		int elapsed = 0;
		// Create copies since we will be clearing/adding
		if (g2.after(g1)) {
			gc2 = (GregorianCalendar) g2.clone();
			gc1 = (GregorianCalendar) g1.clone();
		} else {
			gc2 = (GregorianCalendar) g1.clone();
			gc1 = (GregorianCalendar) g2.clone();
		}
		if (type == Calendar.MONTH || type == Calendar.YEAR) {
			gc1.clear(Calendar.DATE);
			gc2.clear(Calendar.DATE);
		}
		if (type == Calendar.YEAR) {
			gc1.clear(Calendar.MONTH);
			gc2.clear(Calendar.MONTH);
		}
		while (gc1.before(gc2)) {
			gc1.add(type, 1);
			elapsed++;
		}
		return elapsed;
	}

	/**
	 * All elaspsed types based on date and current Date
	 * 
	 * @param date
	 *            Date
	 * @param type
	 *            int (Calendar.FIELD_NAME)
	 * 
	 * @return int number of elapsed "type"
	 */
	private static int elapsed(Date date, int type) {
		return elapsed(date, new Date(), type);
	}

	/**
	 * All elaspsed types
	 * 
	 * @param d1
	 *            Date
	 * @param d2
	 *            Date
	 * @param type
	 *            int (Calendar.FIELD_NAME)
	 * 
	 * @return int number of elapsed "type"
	 */
	private static int elapsed(Date d1, Date d2, int type) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
		cal.setTime(d2);
		GregorianCalendar g2 = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));
		return elapsed(g1, g2, type);
	}

	public static Date getDateFromString(String data, String formato) throws java.text.ParseException {
		Date d = new Date();
		DateFormat df = new SimpleDateFormat(formato);

		d = df.parse(data);
		return d;
	}
	
	public static java.sql.Timestamp getTimestampFromString(String data, String formato) throws java.text.ParseException {
		Date d = new Date();
		DateFormat df = new SimpleDateFormat(formato);

		d = df.parse(data);
		return new java.sql.Timestamp(d.getTime());
	}
	
	

	public static int compareDate(java.util.Date d1) {
		return compareDate(d1, new java.util.Date());
	}

	/**
	 * Compara a data atual com d1 de forma que a chama fica compareOnlyDate(d1, new java.util.Date());
	 * 
	 * {@link #compareOnlyDate(Date, Date)}
	 */
	public static int compareOnlyDate(java.util.Date d1) {
		return compareOnlyDate(d1, new java.util.Date());
	}

	/**
	 * não leva em consideração os segundos/minutos. Só é considerado o dia/mes/ano <br/>
	 * 
	 * Retorno igual a {@link #compareDate(Date, Date)}
	 */
	public static int compareOnlyDate(java.util.Date d1, java.util.Date d2) {
		java.util.Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		GregorianCalendar g1 = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE));

		java.util.Calendar cal2 = Calendar.getInstance();
		cal2.setTime(d2);
		GregorianCalendar g2 = new GregorianCalendar(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), cal2.get(Calendar.DATE));
		return g1.compareTo(g2);
		// return cal.compareTo(cal2);
	}

	/**
	 * Retorna : - 0 se igual </br>
	 * - < 0 se d1 < d2 </br>
	 * - > 0 se d1 > d2 </br>
	 */
	public static int compareDate(java.util.Date d1, java.util.Date d2) {
		java.util.Calendar cal = Calendar.getInstance();
		cal.setTime(d1);
		java.util.Calendar cal2 = Calendar.getInstance();
		cal2.setTime(d2);

		return cal.compareTo(cal2);
	}

	public static Integer getIntegerDate(java.util.Date d, String formato) {
		if (formato.contains("/")) {
			throw new IllegalArgumentException("O formato não deve conter o simbolo \\ ");
		}

		String s = getStringDate(d, formato);
		return Integer.parseInt(s);
	}

	public static String getStringDate(java.util.Date d, String formato) {
		if (d != null) {
			DateFormat df = new SimpleDateFormat(formato);
			return df.format(d);
		}
		return null;
	}

	/**
	 * Formato 12 horas
	 * @param d
	 * @return
	 */
	public static String getStringDateHour(java.util.Date d) {
		if (d != null) {
			DateFormat df = new SimpleDateFormat(ddMMaaHHmm);
			return df.format(d);
		}
		return null;
	}
	
	/**
	 * Formato 12 horas
	 * @param d
	 * @return
	 */
	public static Date getDateStringHour(String d) {
		try {
			if (d != null) {
				DateFormat df = new SimpleDateFormat(ddMMaaHHmm);
				return df.parse(d);
			}
		} catch (ParseException e) {
		}

		return null;
	}
	
	/**
	 * Formato 24 horas
	 * @param d
	 * @return
	 */
	public static String getStringDate24Hour(java.util.Date d) {
		if (d != null) {
			DateFormat df = new SimpleDateFormat(ddMMaaHH24mm);
			return df.format(d);
		}
		return null;
	}
	
	/**
	 * Formato 24 horas
	 * @param d
	 * @return
	 */
	public static Date getDateString24Hour(String d) {
		try {
			if (d != null) {
				DateFormat df = new SimpleDateFormat(ddMMaaHH24mm);
				return df.parse(d);
			}
		} catch (ParseException e) {
		}

		return null;
	}
	
	public static String getStringDate(java.util.Date d) {
		try {
			return getStringDate(d, "dd/MM/yyyy");
		}catch(Exception e) {
			return null;
		}
	}

	public static String getStringDate(java.sql.Date d) {
		return getStringDate(new java.util.Date(d.getTime()));
	}

	public static String getStringCurrentDate(String formato) {
		return getStringDate(new java.util.Date(), formato);
	}

	public static String changeFormatStringDate(String date, String currentFormat, String newFormat) {

		try {
			return DateUtil.getStringDate(DateUtil.getDateFromString(date, currentFormat), newFormat);
		} catch (ParseException ex) {
			LogManager.getLogger(DateUtil.class.getName()).log(Level.ERROR, "", ex);
		}
		return null;
	}

	public static Integer changeFormatIntDate(Integer date, String currentFormat, String newFormat) {

		try {
			return DateUtil.getIntegerDate(DateUtil.getDateFromString(String.valueOf(date), currentFormat), newFormat);
		} catch (ParseException ex) {
			LogManager.getLogger(DateUtil.class.getName()).log(Level.ERROR, "", ex);
		}
		return null;
	}

	/**
	 * Retorna uma array onde [0] é hora, [1] é minuto
	 * 
	 * @param hora
	 * @return
	 */
	private static int[] getArrayHora(Integer hora) {

		String strHora = String.valueOf(hora);

		int[] array = new int[2];

		if (strHora.length() <= 2) {
			array[0] = 0;
			array[1] = Integer.parseInt(strHora);
		} else if (strHora.length() == 3) {
			array[0] = Integer.parseInt(strHora.substring(0, 1));
			array[1] = Integer.parseInt(strHora.substring(1));
		} else if (strHora.length() == 4) {
			array[0] = Integer.parseInt(strHora.substring(0, 2));
			array[1] = Integer.parseInt(strHora.substring(2));
		}

		return array;
	}

	public static java.util.Date changeFormatIntDate(Integer date, String currentFormat, Integer hora) {

		try {
			if (currentFormat.equals("yMMdd") || currentFormat.equals("yyMMdd")) {

				currentFormat = "yyyyMMdd";
				String strData = String.valueOf(date);

				if (strData.length() == 5) {
					strData = "200" + strData;
				} else if (strData.length() == 6) {
					strData = "20" + strData;
				}

				date = Integer.parseInt(strData);
			}
			Date data = DateUtil.getDateFromString(String.valueOf(date), currentFormat);

			if (hora == null) {
				return data;
			}

			int horaArray[] = DateUtil.getArrayHora(hora);
			Calendar c = Calendar.getInstance();
			c.setTime(data);
			c.set(Calendar.HOUR_OF_DAY, horaArray[0]);
			c.set(Calendar.MINUTE, horaArray[1]);

			return c.getTime();

		} catch (ParseException ex) {
			LogManager.getLogger(DateUtil.class.getName()).log(Level.ERROR, "", ex);
		}
		return null;
	}

	public static java.util.Date changeFormatStringDate(String date, String currentFormat, Integer hora) {

		try {
			if (currentFormat.equals("yMMdd") || currentFormat.equals("yyMMdd")) {

				currentFormat = "yyyyMMdd";

				if (date.length() == 5) {
					date = "200" + date;
				} else if (date.length() == 6) {
					date = "20" + date;
				}
			}
			Date data = DateUtil.getDateFromString(String.valueOf(date), currentFormat);

			if (hora == null) {
				return data;
			}

			int horaArray[] = DateUtil.getArrayHora(hora);
			Calendar c = Calendar.getInstance();
			c.setTime(data);
			c.set(Calendar.HOUR_OF_DAY, horaArray[0]);
			c.set(Calendar.MINUTE, horaArray[1]);

			return c.getTime();

		} catch (ParseException ex) {
			LogManager.getLogger(DateUtil.class.getName()).log(Level.ERROR, "", ex);
		}
		return null;
	}

	public static int getIntHoraSRVP(Date data) {
		Calendar c = Calendar.getInstance();
		c.setTime(data);

		String hora = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
		hora = (hora.length() < 2) ? "0" + hora : hora;

		String minuto = String.valueOf(c.get(Calendar.MINUTE));
		minuto = (minuto.length() < 2) ? "0" + minuto : minuto;

		return Integer.parseInt(hora + minuto);
	}

	public static long getElapsedHoras(Date dtInic, Date dtFim) {
		return (dtFim.getTime() - dtInic.getTime()) / (1000 * 60 * 60);
	}

	public static long getElapsedMinutos(Date dtInic, Date dtFim) {
		return (dtFim.getTime() - dtInic.getTime()) / (1000 * 60);
	}

	/**
	 * Regresa los minutos pela hora inicial del sistema
	 * 
	 * @param dtInic
	 * @param dtFim
	 * @return
	 */
	public static HoraSistema getHorasMinutosByInicio(Date dtFim) {
		return new HoraSistema(dtFim);
	}

	/**
	 * Verifica se o intervalo de datas dtStartCompare e dtEndCompare estão dentro do intervalo dtStart dtEnd
	 * 
	 * @param dtStart
	 * @param dtEnd
	 * @param dtStartCompare
	 * @param dtEndCompare
	 * @return
	 */
	public static boolean intersectDate(Date dtStart, Date dtEnd, Date dtStartCompare, Date dtEndCompare) {
		return TraslaparUtil.intersectDate(dtStart, dtEnd, dtStartCompare, dtEndCompare);
	}

	public static void main(String ar[]) {
		System.out.println(Arrays.asList(1,2,3,4,5).toString().replaceAll("[\\]\\[]", ""));
	}

	public static void main2(String args[]) {
		java.util.Date agora = new java.util.Date();

		System.out.println(agora);

		Calendar c = Calendar.getInstance();
		c.setTime(agora);

		System.out.println(DateUtil.getIntHoraSRVP(agora));
		// c.add(Calendar.MINUTE, 60);

		System.out.println("final " + c.getTime());
		System.out.println("1 " + c.get(Calendar.HOUR_OF_DAY));

	}

	/**
	 * Es la fecInicial del sistema para campos de horas
	 * 
	 * @return
	 */
	public static Calendar getFecInicio() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.YEAR, 1970);
		c.set(Calendar.DAY_OF_MONTH, 1);
		c.set(Calendar.MONTH, 0);
		c.set(Calendar.HOUR_OF_DAY, 0);
		c.set(Calendar.MINUTE, 0);
		c.set(Calendar.MILLISECOND, 0);
		c.set(Calendar.SECOND, 0);

		return c;
	}

	/**
	 * Es la fecInicial del sistema con los campos de hora y minutos del parametro informado
	 * 
	 * @return
	 */
	public static Calendar getFecInicio(Date horaMinuto) {
		Calendar tmp = Calendar.getInstance();
		tmp.setTime(horaMinuto);
		Calendar c = getFecInicio();
		c.set(Calendar.HOUR_OF_DAY, tmp.get(Calendar.HOUR_OF_DAY));
		c.set(Calendar.MINUTE, tmp.get(Calendar.MINUTE));

		return c;
	}

	/**
	 * Es la fecInicial del sistema con los campos de hora y minutos del parametro informado
	 * 
	 * @return
	 */
	public static Calendar getFecInicio(Integer hora, Integer minuto) {

		Calendar c = getFecInicio();
		c.add(Calendar.HOUR_OF_DAY, hora);
		c.add(Calendar.MINUTE, minuto);

		return c;
	}

	/**
	 * Quita los segundos y milesegundos de la fecha
	 * 
	 * @param fecha
	 * @return
	 */
	public static Date normalizar(Date fecha) {
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(fecha);
		gcalendar.set(Calendar.SECOND, 0);
		gcalendar.set(Calendar.MILLISECOND, 0);

		return gcalendar.getTime();
	}

	/**
	 * Quita los segundos, milesegundos, hora y minuto de la fecha
	 * 
	 * @param fecha
	 * @return
	 */
	public static Date normalizarToFecha(Date fecha) {
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(fecha);
		gcalendar.set(Calendar.SECOND, 0);
		gcalendar.set(Calendar.MILLISECOND, 0);
		gcalendar.set(Calendar.HOUR_OF_DAY, 0);
		gcalendar.set(Calendar.MINUTE, 0);

		return gcalendar.getTime();
	}

	public static Date inicioFechaPeriodoMeses(Date periodo) {
		return inicioFecha(periodo, true);
	}

	public static Date fimFechaPeriodoMeses(Date periodo) {
		return fimFecha(periodo, true);
	}

	private static Date inicioFecha(Date fecha, boolean diaInicial) {
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(fecha);
		int date = gcalendar.get(Calendar.DATE);
		if (diaInicial) {
			date = gcalendar.getActualMinimum(Calendar.DAY_OF_MONTH);
		}
		int month = gcalendar.get(Calendar.MONTH);
		int year = gcalendar.get(Calendar.YEAR);
		int hourOfDay = 00;
		int minute = 00;
		int second = 00;

		gcalendar.set(year, month, date, hourOfDay, minute, second);
		return gcalendar.getTime();
	}

	private static Date fimFecha(Date fecha, boolean diaFinal) {
		GregorianCalendar gcalendar = new GregorianCalendar();
		gcalendar.setTime(fecha);

		int date = gcalendar.get(Calendar.DATE);
		if (diaFinal) {
			date = gcalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		}
		int month = gcalendar.get(Calendar.MONTH);
		int year = gcalendar.get(Calendar.YEAR);
		int hourOfDay = 23;
		int minute = 59;
		int second = 59;

		gcalendar.set(year, month, date, hourOfDay, minute, second);
		return gcalendar.getTime();
	}

	public static Date inicioFecha(Date fecha) {
		return inicioFecha(fecha, false);
	}

	public static Date fimFecha(Date fecha) {
		return fimFecha(fecha, false);
	}
	
	public static java.sql.Date inicioFechaSql(Date fecha) {
		return new java.sql.Date(inicioFecha(fecha, false).getTime());
	}

	public static java.sql.Date fimFechaSql(Date fecha) {
		return new java.sql.Date(fimFecha(fecha, false).getTime());
	}

	public static Date somarDias(Date data, int quantidadeDias) {
		Integer dias = UM_DIA * quantidadeDias;
		Calendar c = Calendar.getInstance();
		c.setTime(data);
		c.add(Calendar.DAY_OF_MONTH, dias);
		return c.getTime();
	}

	public static Date somarMinutos(Date data, int quantidadeMinutos) {
		Calendar c = Calendar.getInstance();
		c.setTime(data);
		c.add(Calendar.MINUTE, quantidadeMinutos);
		return c.getTime();
	}

	/**
	 * Formato competencia: MM/YYYY
	 * 
	 * @param competencia
	 * @return
	 */
	public static Set<Integer> carregarDiasCompetencia(String competencia, Date dataMovimento) {
		Set<Integer> diasCompetencia = new TreeSet<Integer>();
		if (competencia == null && dataMovimento != null) {
			Integer diaInteger = new Integer(getStringDate(dataMovimento).split("/")[0]);
			diasCompetencia.add(diaInteger);
		} else {
			String[] vetCompetencia = competencia.split("/");
			Calendar cal = Calendar.getInstance();
			cal.set(Calendar.MONTH, Integer.valueOf(vetCompetencia[0]) - 1);
			cal.set(Calendar.YEAR, Integer.valueOf(vetCompetencia[1]));

			for (int i = 1; i <= cal.getActualMaximum(Calendar.DAY_OF_MONTH); i++) {
				diasCompetencia.add(i);
			}
		}
		return diasCompetencia;
	}

	public static Set<Integer> carregarDiasCompetencia(Date dataInicial, Date dataFinal) {
		Set<Integer> diasCompetencia = new TreeSet<Integer>();

		Calendar cal = Calendar.getInstance();

		cal.setTime(dataInicial);
		Integer diaInicial = cal.get(Calendar.DAY_OF_MONTH);

		cal.setTime(dataFinal);
		Integer diaFinal = cal.get(Calendar.DAY_OF_MONTH);

		for (int i = diaInicial; i <= diaFinal; i++) {
			diasCompetencia.add(i);
		}

		return diasCompetencia;
	}

	public static boolean isCompetenciaValida(String competencia) {
		if (StringUtils.isNotBlank(competencia)) {
			String mesString = competencia.split("/")[0].replaceAll("_", "");
			if (StringUtils.isNotBlank(mesString)) {
				Integer mes = Integer.valueOf(competencia.split("/")[0]);
				Integer minMes = Calendar.JANUARY + 1;
				Integer maxMes = Calendar.DECEMBER + 1;
				return mes >= minMes && mes <= maxMes;
			}
		}

		return false;
	}

	public static Date getDataFinalCompetencia(String competencia) throws ParseException {
		if (isCompetenciaValida(competencia)) {
			Date dataInicio = getDateFromString("01/" + competencia, "dd/MM/yyyy");
			Calendar cal = Calendar.getInstance(LocaleUtil.getLocale());
			cal.setTime(dataInicio);
			return getDateFromString(cal.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" + competencia, "dd/MM/yyyy");
		}
		return null;
	}

	public static Date getDataInicialCompetencia(String competencia) throws ParseException {
		if (isCompetenciaValida(competencia)) {
			return getDateFromString("01/" + competencia, "dd/MM/yyyy");
		}
		return null;
	}

	public static Date getYesterdayDate() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, -1);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		Date yesterday = calendar.getTime();
		return yesterday;
	}

	public static String getYesterdayDateString() {
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		return dateFormat.format(cal.getTime());
	}

	public static Date getDataTempoDecorrido(String tempo) {
		String[] tempoDecorrido = tempo.split(":");

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR, Integer.parseInt(tempoDecorrido[0].isEmpty() ? "0" : tempoDecorrido[0]));
		calendar.set(Calendar.MINUTE, Integer.parseInt(tempoDecorrido[1].isEmpty() ? "0" : tempoDecorrido[1]));

		return calendar.getTime();
	}

	public static int diferencaEntreDatasEmdias(Date inicio, Date fim) {
		long quantidadeHoras = ((fim.getTime() - inicio.getTime()));
		return (int) (quantidadeHoras / DIA);
	}
	
	public static String dataFormatoBPe(Date data){
		SimpleDateFormat dateFormat = new SimpleDateFormat(YYYY_MM_DD_T_HH_MM_SS_Z) {
			private static final long serialVersionUID = 1L;
			public StringBuffer format(Date date, StringBuffer toAppendTo, java.text.FieldPosition pos) {
	            StringBuffer toFix = super.format(date, toAppendTo, pos);
	            return toFix.insert(toFix.length()-2, ':');
	        };
	    };
    	
    	return dateFormat.format(data);
    }
    
	public static Date dateFormatBPe(final String data) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(YYYY_MM_DD_T_HH_MM_SS_Z);
			
			String value = data;
			
			if (value.substring(value.length()-3,value.length()-2).equals(":")) {
				StringBuilder dx = new StringBuilder(value);
				dx = dx.delete(dx.length() - 3, dx.length() - 2);
				value = dx.toString();
			}
			
			return sdf.parse(value);
		} catch (ParseException e) {
			return null;
		}
	}
	
	/**
	 * Verifica se a data1 é igual a data2
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static boolean isIgual(final Date data1, final Date data2) {
		if(data1 == null || data2 == null) {
			return false;
		}
		
		Calendar c1 = Calendar.getInstance();
		c1.setTime(data1);
		
		Calendar c2 = Calendar.getInstance();
		c2.setTime(data2);
		
		return c1.equals(c2);
	}
	
	/**
	 * Verifica se a data1 é igual ou menor que a data2
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static boolean isIgualORMenor(final Date data1, final Date data2) {
		return isIgual(data1, data2) || isMenor(data1, data2);
	}
	
	/**
	 * Verifica se a data1 é menor que a data2
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static boolean isMenor(final Date data1, final Date data2) {
		if(data1 == null || data2 == null) {
			return false;
		}
		
		Calendar c1 = Calendar.getInstance();
		c1.setTime(data1);
		
		Calendar c2 = Calendar.getInstance();
		c2.setTime(data2);
		
		return c1.before(c2);
	}
	
	/**
	 * Verifica se a data1 é igual ou maior que a data2
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static boolean isIgualORMaior(final Date data1, final Date data2) {
		return isIgual(data1, data2) || isMaior(data1, data2);
	}
	
	/**
	 * Verifica se a data1 é maior que a data2
	 * @param data1
	 * @param data2
	 * @return
	 */
	public static boolean isMaior(final Date data1, final Date data2) {
		if(data1 == null || data2 == null) {
			return false;
		}
		
		Calendar c1 = Calendar.getInstance();
		c1.setTime(data1);
		
		Calendar c2 = Calendar.getInstance();
		c2.setTime(data2);
		
		return c1.after(c2);
	}
	
	/***
	 * retorna dia da semana da data Domingo 1, até sábado 7
	 * 
	 * @param dataCobranca
	 * @return
	 */
	public static int getDiaDaSemana(Date dataCobranca) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(dataCobranca);

		return cal.get(Calendar.DAY_OF_WEEK);
	}

}