0

I am totally out of my depth here. I have this pseudo code that I would like to write in Java but have no idea where to start. The following is the logic that I would like to express. Any advice would be much appreciated.

if(bestBeforeDate <= today() + 2days) // if a product is two days before its best before date
toShipOut = false;
else if (bestBeforeDate >= today >= bbDate- 8 days) // from 8 days before best before day
DiscountedPrice();
else
toShipOut = true;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
user3013144
  • 61
  • 2
  • 13
  • One way would be use methods from Date() class. – kosa Dec 02 '13 at 21:01
  • Seriously, start writing code. It's already very similar to the end result, just look trough the docs to find the exact methods you want. – Jeroen Vannevel Dec 02 '13 at 21:01
  • One of less fun parts of Java (in my opinion), but not so bad if you are starting from scratch. See http://docs.oracle.com/javase/tutorial/datetime/index.html – eebbesen Dec 02 '13 at 21:02
  • You could take a look at [this example](http://stackoverflow.com/questions/16074398/comparing-dates-in-java/16074453#16074453) and [this example](http://stackoverflow.com/questions/18629428/determine-if-current-time-in-java-is-past-a-predetermined-time-by-15mins/18629816#18629816) – MadProgrammer Dec 02 '13 at 21:14

3 Answers3

1

Check Out Date.

Notice the constructor Date(int year, int month, int date)

Also notice the boolean method after(Date when)

You should be able to find everything you need there.

GregorionCalendar is usually preferred to Date now, but it is the same concept. GregorianCalendar(int year, int month, int dayOfMonth)

public int compareTo(Calendar anotherCalendar)

DoubleDouble
  • 1,493
  • 10
  • 25
  • 2
    The constructor you've suggested is deprecated. Better simply use `Calendar` or even better Joda-Time - IMHO – MadProgrammer Dec 02 '13 at 21:08
  • This is just my opinion, but you are better off using `Calendar.getInstance()` as it has the ability to provide a localised instance of `Calendar`, unless you have a very particular need to use a particular instance of `Calendar` for specific work – MadProgrammer Dec 02 '13 at 21:19
  • I assume OP will want a specific Calendar time for the `bestBeforeDate` and will want to use `Calendar.getInstance` for the `today()` method. However, if he is setting the `bestBeforeDate` based off todays date I would agree with you, in which case he should use Malvon's answer. – DoubleDouble Dec 02 '13 at 21:40
1

Use Calendar.add() to add number of days. You need to have a temporary Calendar for each additions.

Calendar c = Calendar.getInstance();
c.setTime(new Date()); // Current date
c.add(Calendar.DATE, 2); // Adding 2 days
Malvon
  • 1,591
  • 3
  • 19
  • 42
  • Add some discussion about comparing `Date`s and earn some up-votes ;) – MadProgrammer Dec 02 '13 at 21:26
  • @MadProgrammer The very set of examples you posted after my answer points to `Calendar.add()` method. I did not take into the account the fact that the original poster needs help with the _comparison_, which should be obvious. – Malvon Dec 02 '13 at 21:35
1

Beware of Date/Calendar Classes

Caution: The java.util.Date/Calendar classes bundled with Java are notoriously bad. Instead, use the third-party library Joda-Time, or in Java 8 use the new JSR 310 features (inspired by Joda-Time).

Considerations

Think about time as well as date. And time zones. Generally it is best to store and work with UTC time (no time zone offset), then convert to zoned date-time for presentation to user.

Example Code

I'm not promising this source code example is logically consistent, but it will put you in the right direction. Joda-Time 2.3 on Java 7 on a Mac.

The boolean logic in your pseudo-code is flawed, so I ignored that aspect. I focused on the date-time angle.

// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;

DateTimeZone denverTimeZone = DateTimeZone.forID( "America/Denver" );

// In real world, get 'bestBeforeDate' from storage. Stored in UTC.
// Call 'withTimeAtStartOfDay' method rather than try to set midnight. Not every day in every time zone has a midnight.
// For this example, we'll hard-code bestBeforeDate to first moment of day Nov 1 in Denver. Then convert to UTC.
DateTime bestBeforeDate = new DateTime( 2013, DateTimeConstants.NOVEMBER, 1, 3, 3, denverTimeZone ).withTimeAtStartOfDay().toDateTime( DateTimeZone.UTC );

DateTime now = new DateTime().toDateTime( DateTimeZone.UTC );
DateTime twoDaysFromNow = now.plusDays( 2 );

if ( bestBeforeDate.isBefore( twoDaysFromNow ) ) {
    // Do something
} else {
    // Do something else.
}

System.out.println( "bestBeforeDate: " + bestBeforeDate );
System.out.println( "now: " + now );
System.out.println( "twoDaysFromNow: " + twoDaysFromNow );

When run…

bestBeforeDate: 2013-11-01T06:00:00.000Z
now: 2013-12-03T04:54:55.405Z
twoDaysFromNow: 2013-12-05T04:54:55.405Z
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154