Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date determine if DAY is greater

I have time stamps as string format Sun Jul 10 17:47:55 EDT 2011

I need to determine if the current DAY is greater than the stored day.

I will get the current day with Date currentDate = new Date();

and I will parse my string into a Date object with SimpleDateFormat dateParser = new SimpleDateParser("E MMM d HH:mm:ss z y");

but using the "currentDate.after()" function, or the currentDate.compare() function will only tell me if ANY date is greater or less than, which includes by the hour,minute or second.

So My next hunch would be to convert the date into the day of the year, and compare the new integers, if integer1>integer2, then.. but how would I do that?

I also considered breaking the string up to a substring consisting of only the first half of the stored string date. Sun Jul 10 to Sun Jul 10 but the problem here is that the day value is sometimes 1 digit and othertimes 2 digits.

Also I think the Calendar abstract class is the best way to go about this, but I am unsure, and currently in a fog about how to convert the Date objects into the Calendar object for comparison!

Insight appreciated

like image 295
CQM Avatar asked Mar 07 '26 15:03

CQM


2 Answers

This may be helpful in your case (as quick way):

Locale.setDefault(Locale.UK);
String storedDateString = "Sun Jul 10 17:47:55 EDT 2011";
SimpleDateFormat dateParser = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy");

Date storedDate = dateParser.parse(storedDateString);

GregorianCalendar storedCal = new GregorianCalendar();
storedCal.setTime(storedDate);
GregorianCalendar currentCal = new GregorianCalendar();

int storedDayOfYear = storedCal.get(Calendar.DAY_OF_YEAR);
int currentDayofYear = currentCal.get(Calendar.DAY_OF_YEAR);

//int storedDayYear = storedCal.get(Calendar.YEAR);
//int currentDayYear = storedCal.get(Calendar.YEAR);

System.out.println(storedDayOfYear);
System.out.println(currentDayofYear);

//System.out.println(storedDayYear);
//System.out.println(currentDayYear);

Result:

191
192

After that it's trivial to compare int values.

like image 157
Grzegorz Szpetkowski Avatar answered Mar 09 '26 05:03

Grzegorz Szpetkowski


If you want to convert a Date object into a Calendar objet, use

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(new Date());

You can get the day value of the Calendar object with

    cal.get(Calendar.DAY_OF_MONTH);

or

    calendar.get(Calendar.DAY_OF_YEAR);

however, note that when comparing dates of different months (first example) or years (second example) this wont work. You should better set the milliseconds, seconds, minutes and hours to 0 on each Calendar object and the compare the objects:

    Calendar c1 = Calendar.getInstance();
    Calendar c2 = Calendar.getInstance();
    c1.setTime(date1);
    c2.setTime(date2);

    c1.set(Calendar.MILLISECOND, 0);
    c1.set(Calendar.SECOND, 0);
    c1.set(Calendar.MINUTE, 0);
    c1.set(Calendar.HOUR, 0);

    c2.set(Calendar.MILLISECOND, 0);
    c2.set(Calendar.SECOND, 0);
    c2.set(Calendar.MINUTE, 0);
    c2.set(Calendar.HOUR, 0);

    c1.compareTo(c2);
like image 32
GeorgeG Avatar answered Mar 09 '26 04:03

GeorgeG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!