Creating dates in AnyLogic: Java tips & custom calendar model

Calendar pages with dates

Have you ever wondered, "How many dimensions do we live in?" You might quickly say three, but the answer is more complex than it seems at first. Einstein's theory of relativity challenges you to think differently, suggesting that time itself is a dimension.

In simulation modeling, the ability to manage and manipulate time is crucial. AnyLogic, as a powerful simulation tool, provides users with complete control over time and dates, enabling advanced model customization. To effectively handle date-related scenarios, it's important to understand how to use Java classes to set dates.

For example:

  • A supply chain might experience route disruptions during certain seasons. Some transportation routes could become impassable due to weather conditions. Using Java classes for dates, the model can detect these dates and reroute through alternative paths.
  • Public holidays can also impact business. During these dates, manufacturing may halt, unlike other times of the year. Using Java classes for dates, you can create date-specific logic to account for these non-operational periods.

You can model such scenarios dynamically and accurately by learning to create dates and working with Java classes for dates in AnyLogic. This blog post covers the main aspects of incorporating this to handle complex time-related behaviors in simulation models.

Contents:

  1. How to work with dates in AnyLogic
  2. Pitfalls
  3. Get complete control over dates

How to work with dates in AnyLogic

Create dates

The most basic date-related settings in AnyLogic are the Start date and Stop date, which define the beginning and end of a simulation run. Even if you haven't explicitly created a date, be sure these settings were set by default. You can always adjust them to fit the experiment's properties.

AnyLogic interface for model time settings

Model time settings in the simulation experiment's properties (click to enlarge)

Everything should be set here manually before the model run. But what if we need to define start and stop dates dynamically? For example, a model might require start and stop dates to be fetched from an external source, such as a database or an Excel file, before the simulation run. You can achieve this using the functions from AnyLogic's Engine Java class, which can be accessed programmatically (e.g., in the experiment's properties):

//Initialize my dates
Date startDate = new Date (125, 1, 20); //Date is 02/20/2025
Date stopDate = new Date (125, 1, 28); //Date is 02/28/2025
//Set the dates above as the model's start and stop date getEngine ().setStartDate (startDate) ;
getEngine () .setStopDate (stopate); 

The code above allows you to create the desired dates and set them as the model's start and stop dates. These functions should be called in the Before simulation run action field of the experiment.

Note: These functions expect a Date type object as input.

Use LocalDate Java class to create dates

The Date Java class is commonly used for setting dates, but it's not the most convenient one. It counts years starting from 1900, so we use "125" to represent 2025. Additionally, the months index from "0," meaning February is the 1st month. When using the Date Java class to create dates in your simulation models, you must consider these peculiarities.

Alternatively, the more modern and convenient LocalDate Java class offers enhanced features. You can initialize LocalDate objects more easily and then convert the result to Date type, as AnyLogic's functions still require this type:

//Initialize my dates
LocalDate startDateLocal = LocalDate.of (2025, 2, 20); //Date is 02/20/2025
LocalDate stopDateLocal = LocalDate.of (2025, 2, 28)://Date is 02/28/2025
			
//Conversion from LocalDate to Date
Date startDate = Date. from (startDateLocal
	• atStartOfDay () .atZone (ZoneId.systemDefault () ).toInstant ()) ;
			
Date stopDate = Date. from (stopDateLocal
	•atStartOfDay () .atzone (ZoneId.systemDefault ()).toInstant ()) ;
	
//Set the dates above as the model's start date and stop date 
getEngine () .setStartDate (startDate) ;
getEngine () . setStopDate (stopDate);

While setting dates is simpler with LocalDate, the code may seem more complex due to the necessary conversion to the Date Java class. In order to enhance readability and maintainability, consider defining a separate custom function within AnyLogic to handle this conversion:

Converting LocalDate type object to Date type in AnyLogic

Converting LocalDate type object to Date type (click to enlarge)

It simplifies the code above and allows you to reuse it once needed:

//Initialize my dates
LocalDate startDateLocal = LocalDate.of (2025, 2, 20); //Date is 02/20/2025
LocalDate stopDateLocal = LocalDate.of (2025, 2, 28);//Date is 02/28/2025

//Conversion from LocalDate to Date
Date startDate = convertToDate (startDateLocal);
Date stopDate = convertToDate (stopDateLocal);

//Set the dates above as the model's start date and stop date 
getEngine () .setStartDate (startDate) ;
getEngine () •setStopDate (stopDate) ;

The LocalDate class offers many functions that simplify working with dates. For example, its plusDays(...) function makes it simple to calculate a date after a given number of days. Achieving the same result with the outdated Date class would be much more complicated. Therefore, using LocalDate provides significant advantages. Additionally, there is the LocalTime Java class, which allows you to set the time alongside the date.

Use SimpleDateFormat Java class to create a date in a desired format

Earlier, we explained how to set the model's start and stop dates. However, you can also set any other date within the model and use it as a variable or retrieve the current model date with the date() function.

In many cases, you may not want to just create a date but also to display it in the Console or the model's user interface. When you provide a Date or LocalDate object, the output format can vary and might look like this:

Default representation of dates initialized by Date and LocalDate Java classes
Default string representation of dates initialized by Date and LocalDate Java classes

But what if we want to see this date in a different format? For instance, you prefer it as "12/27/2024," but both Date and LocalDate use different default representations. Hopefully, the SimpleDateFormat Java class allows you to create any custom date format you want. Just make this class's instance, set the desired format, and call the created object's format(...) function where you pass the date:

SimpleDateFormat customFormat = new SimpleDateFormat ("MM/dd/yYYY") ;
Date currentDate = date ();
traceln (customFormat. format (currentDate)) ;

You can check the example code in the Digital Watch example model or the other model we will discuss in this article later.

Use the CalendarView model to create a date via the picker component

Some of our users have occasionally requested a more user-friendly way to create dates instead of doing so through code. While currently, there is no such built-in component within AnyLogic, we've developed a model that uses custom Calendar or Date Picker objects. It provides a convenient and intuitive way for users to select dates.

The model incorporates all the best practices discussed above. Feel free to download the model from AnyLogic Cloud and utilize the calendar component in your projects. Moreover, you can customize it as you see fit.

CalendarView model (source files available)

Pitfalls

When working with dates in AnyLogic, you need to be aware of some pitfalls you may come across. You will likely need to set or get the current real-life date within a model. You can achieve this using the following code with the Date Java class:

Date currentDate = new Date();
or in case of LocalDate class:
LocalDate currentLocalDate = LocalDate.now();

The date is determined by the machine's current operating system time when the model is run. This implies that the results may vary between model runs on different machines, potentially affecting the model's reproducibility.

For example, a model may contain a Schedule element that operates based on a time zone. The time zone of an AnyLogic model is determined by the user's time zone as set in the machine's operating system, and the entire model operates within that time zone.

Since Daylight Saving Time rules differ between time zones, model results can vary when run on computers in different locales. To ensure consistent results, set the desired time zone explicitly via code, regardless of the machine's settings. For example:

Changing the default time zone in AnyLogic

Changing the default time zone to a custom AWST (click to enlarge)

Note: All such factors also lead to unreproducible runs, as listed in AnyLogic Help.

Get complete control over time and dates with AnyLogic

Imagine having full control over time in real life—it would offer countless advantages! While that's impossible in reality, simulation modeling allows you to control this magic right now. Using our tips on creating dates with the Java Classes in AnyLogic, you can confidently incorporate date and time logic into your models.


Have you tried time manipulation yet? Now's the perfect time to start—just download AnyLogic and dive in!

Get AnyLogic

Related posts