Helper.java 1.09 KB
Newer Older
1 2 3 4
package com.roshka.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
5 6 7
import java.time.YearMonth;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
8 9 10 11 12 13 14 15 16 17
import java.util.Date;

public class Helper {
    /**
     * Se espera fecha en el formato yyyy-MM-dd
     * @param fecha
     * @return retorna fecha correcta o nulo si no es posible convertir
     */
    public static Date convertirFecha(String fecha) {
        try {
18
                if(fecha == null || fecha.trim().equals("")) return null;
19 20 21 22 23 24 25 26
                return new SimpleDateFormat("yyyy-MM-dd").parse(fecha);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            System.err.println("Error al parsear");
            e.printStackTrace();
            return null;
        }
    }
27 28 29 30 31 32 33

    public static final long getMonthsDifference(Date date1, Date date2) {
        YearMonth m1 = YearMonth.from(date1.toInstant().atZone(ZoneOffset.UTC));
        YearMonth m2 = YearMonth.from(date2.toInstant().atZone(ZoneOffset.UTC));
    
        return m1.until(m2, ChronoUnit.MONTHS) + 1;
    }
34
}