Java Time Different isLowerThanMinTimeDiff(Date lastUpdate, Date now, Long minTimeDiff)

Here you can find the source of isLowerThanMinTimeDiff(Date lastUpdate, Date now, Long minTimeDiff)

Description

is Lower Than Min Time Diff

License

Open Source License

Declaration

public static Boolean isLowerThanMinTimeDiff(Date lastUpdate, Date now, Long minTimeDiff) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Date;

import java.util.concurrent.TimeUnit;

public class Main {
    static final String DATEFORMAT = "yyyy-MM-dd";

    public static Boolean isLowerThanMinTimeDiff(Date lastUpdate, Date now, Long minTimeDiff) {
        long diffinDays = diffInDays(lastUpdate, now);
        Boolean resultValue = minTimeDiff < diffinDays;
        return resultValue;

    }//from  ww w. ja v  a2s.  c  om

    public static Long diffInDays(Date date1, Date date2) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DATEFORMAT);

            String lastUpdateInUTCString = sdf.format(date1);
            String nowInUTCString = sdf.format(date2);

            Date lastUpdateInUTC;

            lastUpdateInUTC = stringDateToDate(lastUpdateInUTCString);

            Date nowInUTC = stringDateToDate(nowInUTCString);

            long nowInUTCMilis = nowInUTC.getTime();
            long lastUpdateInUTCMilis = lastUpdateInUTC.getTime();

            long diff = nowInUTCMilis - lastUpdateInUTCMilis;

            TimeUnit tu = TimeUnit.DAYS;
            long diffinDays = tu.convert(diff, TimeUnit.MILLISECONDS);
            return diffinDays;
        } catch (ParseException e) {
            return Long.MIN_VALUE;
        }

    }

    public static String format(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);
        return dateFormat.format(date);
    }

    public static Date stringDateToDate(String StrDate) throws ParseException {
        Date dateToReturn = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATEFORMAT);

        try {
            dateToReturn = (Date) dateFormat.parse(StrDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return dateToReturn;
    }
}

Related

  1. getDiffTime(String strDate, int idx)
  2. getFormattedTimeWithDiff(DateFormat dateFormat, long finishTime, long startTime)
  3. getTimeDiff(String start, String end, String format, int timeDiff)
  4. getTimeDiffMin(String format, String startDate, String endDate)
  5. isDateDiff_2(String pattern, String time2, String time1)
  6. printTimeDiff(Long oldDate, Long newDate, String message)
  7. printTimeDiffNano(String message, long start)
  8. timeDiff(Calendar calenndarCurrentTime, Calendar calenndarStartTime)
  9. timeDiff(Date salida, Date llegada)