Example usage for java.util Date compareTo

List of usage examples for java.util Date compareTo

Introduction

In this page you can find the example usage for java.util Date compareTo.

Prototype

public int compareTo(Date anotherDate) 

Source Link

Document

Compares two Dates for ordering.

Usage

From source file:org.kuali.kfs.sys.util.KfsDateUtils.java

/**
 * Determines if the given date d1 is on the same day or a later day than the given date d2.
 * @param d1 a date//from  www .ja v a  2 s . c  o  m
 * @param d2 another date, to compare the first date to
 * @return true if d1 is later or the same day as d2, false otherwise or if either value is null
 */
public static boolean isSameDayOrLater(Date d1, Date d2) {
    if (ObjectUtils.isNull(d1) || ObjectUtils.isNull(d2)) {
        return false; // we can't compare against nulls
    }
    if (isSameDay(d1, d2)) {
        return true;
    }
    return d1.compareTo(d2) > 0;
}

From source file:edu.umn.cs.spatialHadoop.nasa.MultiHDFPlot.java

public static boolean multiplot(Path[] input, Path output, OperationsParams params)
        throws IOException, InterruptedException, ClassNotFoundException, ParseException {
    String timeRange = params.get("time");
    final Date dateFrom, dateTo;
    final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");
    try {/* ww  w .jav  a2s  .co  m*/
        String[] parts = timeRange.split("\\.\\.");
        dateFrom = dateFormat.parse(parts[0]);
        dateTo = dateFormat.parse(parts[1]);
    } catch (ArrayIndexOutOfBoundsException e) {
        System.err.println("Use the seperator two periods '..' to seperate from and to dates");
        return false; // To avoid an error that causes dateFrom to be uninitialized
    } catch (ParseException e) {
        System.err.println("Illegal date format in " + timeRange);
        return false;
    }
    // Number of frames to combine in each image
    int combine = params.getInt("combine", 1);
    // Retrieve all matching input directories based on date range
    Vector<Path> matchingPathsV = new Vector<Path>();
    for (Path inputFile : input) {
        FileSystem inFs = inputFile.getFileSystem(params);
        FileStatus[] matchingDirs = inFs.listStatus(input, new PathFilter() {
            @Override
            public boolean accept(Path p) {
                String dirName = p.getName();
                try {
                    Date date = dateFormat.parse(dirName);
                    return date.compareTo(dateFrom) >= 0 && date.compareTo(dateTo) <= 0;
                } catch (ParseException e) {
                    LOG.warn("Cannot parse directory name: " + dirName);
                    return false;
                }
            }
        });
        for (FileStatus matchingDir : matchingDirs)
            matchingPathsV.add(new Path(matchingDir.getPath(), "*.hdf"));
    }
    if (matchingPathsV.isEmpty()) {
        LOG.warn("No matching directories to given input");
        return false;
    }

    Path[] matchingPaths = matchingPathsV.toArray(new Path[matchingPathsV.size()]);
    Arrays.sort(matchingPaths);

    // Clear all paths to ensure we set our own paths for each job
    params.clearAllPaths();

    // Create a water mask if we need to recover holes on write
    if (params.get("recover", "none").equals("write")) {
        // Recover images on write requires a water mask image to be generated first
        OperationsParams wmParams = new OperationsParams(params);
        wmParams.setBoolean("background", false);
        Path wmImage = new Path(output, new Path("water_mask"));
        HDFPlot.generateWaterMask(wmImage, wmParams);
        params.set(HDFPlot.PREPROCESSED_WATERMARK, wmImage.toString());
    }
    // Start a job for each path
    int imageWidth = -1;
    int imageHeight = -1;
    boolean overwrite = params.getBoolean("overwrite", false);
    boolean pyramid = params.getBoolean("pyramid", false);
    FileSystem outFs = output.getFileSystem(params);
    Vector<Job> jobs = new Vector<Job>();
    boolean background = params.getBoolean("background", false);
    Rectangle mbr = new Rectangle(-180, -90, 180, 90);
    for (int i = 0; i < matchingPaths.length; i += combine) {
        Path[] inputPaths = new Path[Math.min(combine, matchingPaths.length - i)];
        System.arraycopy(matchingPaths, i, inputPaths, 0, inputPaths.length);
        Path outputPath = new Path(output, inputPaths[0].getParent().getName() + (pyramid ? "" : ".png"));
        if (overwrite || !outFs.exists(outputPath)) {
            // Need to plot
            Job rj = HDFPlot.plotHeatMap(inputPaths, outputPath, params);
            if (imageHeight == -1 || imageWidth == -1) {
                if (rj != null) {
                    imageHeight = rj.getConfiguration().getInt("height", 1000);
                    imageWidth = rj.getConfiguration().getInt("width", 1000);
                    mbr = (Rectangle) OperationsParams.getShape(rj.getConfiguration(), "mbr");
                } else {
                    imageHeight = params.getInt("height", 1000);
                    imageWidth = params.getInt("width", 1000);
                    mbr = (Rectangle) OperationsParams.getShape(params, "mbr");
                }
            }
            if (background && rj != null)
                jobs.add(rj);
        }
    }
    // Wait until all jobs are done
    while (!jobs.isEmpty()) {
        Job firstJob = jobs.firstElement();
        firstJob.waitForCompletion(false);
        if (!firstJob.isSuccessful()) {
            System.err.println("Error running job " + firstJob.getJobID());
            System.err.println("Killing all remaining jobs");
            for (int j = 1; j < jobs.size(); j++)
                jobs.get(j).killJob();
            throw new RuntimeException("Error running job " + firstJob.getJobID());
        }
        jobs.remove(0);
    }

    // Draw the scale in the output path if needed
    String scalerange = params.get("scalerange");
    if (scalerange != null) {
        String[] parts = scalerange.split("\\.\\.");
        double min = Double.parseDouble(parts[0]);
        double max = Double.parseDouble(parts[1]);
        String scale = params.get("scale", "none").toLowerCase();
        if (scale.equals("vertical")) {
            MultiHDFPlot.drawVerticalScale(new Path(output, "scale.png"), min, max, 64, imageHeight, params);
        } else if (scale.equals("horizontal")) {
            MultiHDFPlot.drawHorizontalScale(new Path(output, "scale.png"), min, max, imageWidth, 64, params);
        }
    }
    // Add the KML file
    createKML(outFs, output, mbr, params);
    return true;
}

From source file:com.augmentum.common.util.DateUtil.java

public static boolean isBetweenTime(Date date, String start, String end) {
    boolean isTime = false;

    if ((date != null) && StringUtils.isNotBlank(start) && StringUtils.isNotBlank(end)) {
        Date startDate = getDateFromStr(start);
        Date endDate = getDateFromStr(end);

        if (endDate.after(startDate)) {
            if ((date.compareTo(startDate) >= 0) && (date.compareTo(endDate) <= 0)) {
                isTime = true;/*from   ww  w . j av a 2  s.c o m*/
            }
        }
    }

    return isTime;
}

From source file:com.krawler.common.util.SchedulingUtilities.java

public static int CountCmpHolidays(Date stdate, Date enddate, String[] CmpHoliDays) {
    int NonWorkingDays = 0;
    Calendar c1 = Calendar.getInstance();
    DateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
    try {/*from  www.  j  ava2  s .c  om*/
        c1.setTime(stdate);
        while ((stdate.compareTo(enddate)) < 0) {
            if (Arrays.binarySearch(CmpHoliDays, sdf1.format(c1.getTime())) >= 0) {
                NonWorkingDays += 1;
            }
            c1.add(Calendar.DATE, 1);
            stdate = c1.getTime();
        }
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }
    return NonWorkingDays;
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * ./*  w  w w . j av a2s.c  om*/
 *
 * @param begin  .
 * @param end   ? .
 * @return
 */
public static List<String> getDaysListBetweenDates(String begin, String end) {
    List<String> dateList = new ArrayList<String>();
    Date d1;
    Date d2;
    try {
        d1 = DateUtils.parseDate(begin, DATE_FORMAT);
        d2 = DateUtils.parseDate(end, DATE_FORMAT);
        if (d1.compareTo(d2) > 0) {
            return dateList;
        }
        do {
            dateList.add(DateFormatUtils.format(d1, DATE_FORMAT));
            d1 = DateUtils.addDays(d1, 1);
        } while (d1.compareTo(d2) <= 0);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return dateList;
}

From source file:cn.mypandora.util.MyDateUtils.java

/**
 * //w  w  w .ja va2  s  .com
 *
 * @param begin
 * @param end
 * @return
 */
public static List<String> getMonthsListBetweenDates(String begin, String end) {
    List<String> dateList = new ArrayList<String>();
    Date d1;
    Date d2;
    try {
        d1 = DateUtils.parseDate(begin, DATE_FORMAT);
        d2 = DateUtils.parseDate(end, DATE_FORMAT);
        if (d1.compareTo(d2) > 0) {
            return dateList;
        }
        do {
            dateList.add(DateFormatUtils.format(d1, MONTH_FORMAT));
            d1 = DateUtils.addMonths(d1, 1);
        } while (d1.compareTo(d2) <= 0);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    return dateList;
}

From source file:edu.indiana.soic.ts.crunch.CrunchDataReader.java

public static List<String> getDates() throws ParseException {
    List<String> allDates = new ArrayList<String>();
    Date startDate = getDate(CrunchDataReader.startDate);
    Date endDate = getDate(CrunchDataReader.endDate);
    ResultScanner scannerForDateTable = getScannerForDateTable();
    for (Result aResultScanner : scannerForDateTable) {
        String date = new String(aResultScanner.getRow());
        Date rowDate = getDate(date);
        if (startDate.compareTo(rowDate) * rowDate.compareTo(endDate) > 0) {
            allDates.add(date);//from  ww w.  j a v  a2 s. co m
        }
    }
    return allDates;

}

From source file:msc.fall2015.stock.kmeans.hbase.crunch.CrunchStockDataReader.java

public static List<String> getDates() throws ParseException {
    List<String> allDates = new ArrayList<String>();
    Date startDate = getDate(CrunchStockDataReader.startDate);
    Date endDate = getDate(CrunchStockDataReader.endDate);
    ResultScanner scannerForDateTable = getScannerForDateTable();
    for (Result aResultScanner : scannerForDateTable) {
        String date = new String(aResultScanner.getRow());
        Date rowDate = getDate(date);
        if (startDate.compareTo(rowDate) * rowDate.compareTo(endDate) > 0) {
            allDates.add(date);//w w w  . j  a v a 2s . com
        }
    }
    return allDates;

}

From source file:Main.java

public static boolean equalDate(String date) {
    Date todayDate = null;
    Date inputDate = null;//from w ww. j a va2s  .c  o m
    Date thirdDate = null;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String today = sdf.format(new Date());
    Calendar cal = Calendar.getInstance();
    //      cal.add(Calendar.DATE, -1);      
    //      String today = sdf.format(cal.getTime());
    try {
        todayDate = sdf.parse(today);
        inputDate = sdf.parse(date);
        cal.setTime(inputDate);
        cal.add(Calendar.DATE, 3);
        thirdDate = cal.getTime();
        if (todayDate.compareTo(inputDate) >= 0 && todayDate.compareTo(thirdDate) < 0) {
            return true;
        } else {
            return false;
        }
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }

}

From source file:com.krawler.common.util.SchedulingUtilities.java

public static int nonWorkingDays(java.util.Date stdate, java.util.Date enddate, int[] NonWorkDays,
        String[] CmpHoliDays) throws ServiceException {
    Calendar c1 = Calendar.getInstance();
    int NonWorkingDaysBetween = 0;
    Date StDate = stdate;/*w  ww.  ja  va 2  s.  co  m*/
    Date EndDate = enddate;
    try {
        c1.setTime(stdate);
        while ((stdate.compareTo(enddate)) < 0) {
            if (Arrays.binarySearch(NonWorkDays, (c1.get(Calendar.DAY_OF_WEEK) - 1)) > -1) {
                NonWorkingDaysBetween += 1;
            }
            c1.add(Calendar.DATE, 1);
            stdate = c1.getTime();
        }
        NonWorkingDaysBetween += CountCmpHolidays(StDate, EndDate, CmpHoliDays);
    } catch (Exception e) {
        throw ServiceException.FAILURE("Exception while calculating non working days : " + e.getMessage(), e);
    }
    return NonWorkingDaysBetween;
}