0

I've below code in Java 1.7:

DateFormat df = DateFormat.getInstance();
Date startDate = df.parse("07/28/12 01:00 AM, PST");

The above date time (07/28/12 01:00 AM, PST) is in the properties file which is configurable. If this date time is already passed, then I need to get the current date, set the time part from above string which is 01:00 AM PST in the current date & if this time is also already passed, then get the next day & set the time part from above string in it. The final object should be Date since I need to use it in Timer object.

How can I do this efficiently? Should I convert from date to Calendar or vice-versa? Can any one provide snippet?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Mike
  • 7,606
  • 25
  • 65
  • 82
  • 2
    While you can use Calendar and it work, I'd personal suggest Joda Time http://joda-time.sourceforge.net/ It simply takes a lot of the annoyance out of having to deal with Java Date's and times - IMHO – MadProgrammer Jul 31 '12 at 01:17
  • I also recommend http://joda-time.sourceforge.net/ its easy to use and comes up with features well enriched over Java's Date and Calender – exexzian Jul 31 '12 at 01:26
  • I cannot use joda due to our policy :(( Please look at my comment below. Appreciate any other alternatives... – Mike Jul 31 '12 at 01:27
  • I think you might need to define "time" and "date" :P – MadProgrammer Jul 31 '12 at 02:09

2 Answers2

2

You should look into the Calendar class. You can use constructs like:

    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    cal.add(Calendar.DAY_OF_YEAR, 1);

It also has methods for checking if your startDate is before() or after() a new date (use the current time).

While writing of the built-in Java date/time structure, i would be remiss if i didnt plug Joda Time, considered by many to be superior to the native Java implementation.

EDIT:

It would be more efficient to show an example of the Date.compareTo() process, as the Calendar.before() and Calendar.after() require comparisons against other Calendar objects, which can be expensive to create.

take a look at the following:

    DateFormat df = DateFormat.getInstance();
    Date startDate = df.parse("07/28/12 01:00 AM, PST");
    Calendar cal = Calendar.getInstance();
    cal.setTime(startDate);
    Date now = new Date();
    if (startDate.compareTo(now)< 0) {
        System.out.println("start date: " + startDate + " is before " + now);

        Calendar nowCal = Calendar.getInstance();
        nowCal.add(Calendar.DAY_OF_YEAR,1);
        cal.set(Calendar.DAY_OF_YEAR, nowCal.get(Calendar.DAY_OF_YEAR));

    } else  if (startDate.compareTo(now) == 0) {
        System.out.println("startDate: " +startDate + " is equal to " + now);
    } else {
        System.out.println("startDate: " + cal + " is after " + now);

    }
    System.out.println(cal.getTime());
akf
  • 38,619
  • 8
  • 86
  • 96
  • How can I do the comparisons? I need to add 1 day only if startDate is already passed.?? I cannot use any other open source like Joda due to our policy :(( And I cannot simply use cal.add(Calendar.DAY_OF_YEAR, 1); because if the startDate is very old, I'll have to keep on adding 1 day until it passes current date. Thats why I want to know if there is a way to extract only time portion from Date object & then apply it current date? – Mike Jul 31 '12 at 01:22
  • Hi, I cannot add 1 day blindly as I need to maintain the time stamp. So, if the property file has time "01:00 AM, PST", then it should be set to next day at the same time. – Mike Jul 31 '12 at 01:48
  • i have updated the code- now uses a brute force method of creating a second Calendar in the case that you need to make the increment. – akf Jul 31 '12 at 01:59
0

I think this should work...your algorthim has my head spinning a little and I'm in a different time zone, so you original string didn't work :P

    try {

        DateFormat df = DateFormat.getInstance();
        Date startDate = df.parse("28/07/2012 01:00 AM");

        System.out.println("StartDate = " + startDate);

        Date callDate = startDate;

        Calendar today = Calendar.getInstance();
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);

        System.out.println("Today = " + today.getTime());

        // If this date time is already passed
        // Tue Jul 31 12:18:09 EST 2012 is after Sat Jul 28 01:00:00 EST 2012
        if (today.after(start)) {

            //then I need to get the current date, set the time part from above string in the current date
            Calendar timeMatch = Calendar.getInstance();
            timeMatch.setTime(startDate);
            timeMatch.set(Calendar.DATE, today.get(Calendar.DATE));
            timeMatch.set(Calendar.MONTH, today.get(Calendar.MONTH));
            timeMatch.set(Calendar.YEAR, today.get(Calendar.YEAR));

            //& if this time is also already passed, then get the next day & set the time part from above string in it
            if (timeMatch.after(today)) {

                timeMatch.add(Calendar.DATE, 1);

            }

            callDate = timeMatch.getTime();

        }

        System.out.println("CallDate = " + callDate);

    } catch (ParseException exp) {

        exp.printStackTrace();

    }

This produces the following output

StartDate = Sat Jul 28 01:00:00 EST 2012
Today = Tue Jul 31 12:18:09 EST 2012
CallDate = Tue Jul 31 01:00:00 EST 2012
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366