Java Date Compare compareDate(String date1, File file)

Here you can find the source of compareDate(String date1, File file)

Description

compare Date

License

Open Source License

Parameter

Parameter Description
date1 first date
file the file we must compare the last modified date with the @date1

Return

0 if equal, >0 if date1 is older , <0 if date1 is more recent than date2

Declaration

public static int compareDate(String date1, File file) 

Method Source Code

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

import java.io.File;

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

import java.util.Date;
import java.util.Locale;

public class Main {
    /**/*w  ww.  ja v a2 s. c  o  m*/
     * 
     * @param date1 first date
     * @param file the file we must compare the last modified date with the @date1
     * @return 0 if equal, >0 if date1 is older , <0 if date1 is more recent than date2
     */
    public static int compareDate(String date1, File file) {
        long d1;
        if (!isNumeric(date1))
            // we add 1 sec to d1 because the date1 string doesnt include seconds
            d1 = stringToLong(date1) + 1000;
        else
            d1 = Long.parseLong(date1);

        long d2 = file.lastModified();

        if (d1 == d2)
            return 0;
        else if (d1 < d2)
            return -1;
        return 1;
    }

    /**
     * Checks if a String is Numeric
     * @param s the string to check if it is numeric
     * @return true if numeric else false
     */
    public static boolean isNumeric(String s) {
        for (int i = 0; i < s.length(); i++)
            if (s.charAt(i) < '0' || s.charAt(i) > '9')
                return false;
        return true;
    }

    /**
     * Converts a String representing a date to a long representing time
     * @param date the date as String
     * @return the given dare in milliseconds
     */
    public static long stringToLong(String date) {
        try {
            SimpleDateFormat f = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
            Date d = f.parse(date);
            return d.getTime();
        } catch (ParseException e) {
        }
        return 0;
    }
}

Related

  1. compareDate(String begingDate, String endDate, String format)
  2. compareDate(String d1, String d2)
  3. compareDate(String d1, String d2)
  4. compareDate(String date)
  5. compareDate(String date0, String date1)
  6. compareDate(String date1, String date2)
  7. compareDate(String DATE1, String DATE2)
  8. compareDate(String s, String e)
  9. compareDate(String s, String e)