Helper.java 3.29 KB
Newer Older
1 2
package com.roshka.utils;

3 4 5 6
import com.roshka.modelo.DBFile;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

Joel Florentin committed
7
import java.io.IOException;
8 9
import java.text.ParseException;
import java.text.SimpleDateFormat;
10 11 12
import java.time.LocalDate;
import java.time.Period;
import java.time.format.DateTimeFormatter;
13
import java.util.Date;
14
import java.util.concurrent.TimeUnit;
15 16 17 18 19 20 21 22 23

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 {
24
                if(fecha == null || fecha.trim().equals("")) return null;
25 26 27 28 29 30 31 32
                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;
        }
    }
33

34 35 36 37 38 39
    /**
     * Diferencia en meses entre 2 fechas.
     * @param date1 La fecha inicial. No puede ser nulo
     * @param date2 La fecha final. Si es nulo, se asume a la fecha de hoy
     * @return
     */
40
    public static final long getMonthsDifference(Date date1, Date date2) {
41
        if(date2==null) date2 = new Date();
42 43
        long diffInMillies = Math.abs(date2.getTime() - date1.getTime());
        long diff = TimeUnit.DAYS.convert(diffInMillies, TimeUnit.MILLISECONDS);
44
    
45
        return Math.round(diff/30.d) ;
46
    }
Joel Florentin committed
47

48
    public static LocalDate convertToLocalDateViaSqlDate(Date dateToConvert) {
49
        if(dateToConvert == null) return null;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        return new java.sql.Date(dateToConvert.getTime()).toLocalDate();
    }

    public static int calculateAge(LocalDate birthDate, LocalDate currentDate) {
        if ((birthDate != null) && (currentDate != null)) {
            return Period.between(birthDate, currentDate).getYears();
        } else {
            return 0;
        }
    }

    public static int calculateAge(Date birthDate) {
        LocalDate currentDate = LocalDate.now();
        return calculateAge(convertToLocalDateViaSqlDate(birthDate),currentDate);
    }

    public static String formatDate(LocalDate fecha, String format){
        if(fecha == null || format == null) return null;
        return fecha.format(DateTimeFormatter.ofPattern(format));
    }
    public static String formatDate(Date fecha, String format){
        return formatDate(convertToLocalDateViaSqlDate(fecha), format);
    }

Joel Florentin committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    public static DBFile createFile(MultipartFile file) {
        // Normalize file name
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        try {
            // Check if the file's name contains invalid characters
            if(fileName.contains("..")) {
                throw new Exception("Sorry! Filename contains invalid path sequence " + fileName);
            }
            if(file.getSize()==0)  throw new Exception("Sorry! File cant be void");;

            DBFile dbFile = new DBFile(fileName, file.getContentType(), file.getBytes());

            return dbFile;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }

98
}