Lamma I/O

Quick Start

There is no external dependency for Lamma, so it's very easy to use with Scala REPL. Now let's get started!

Step 1: Download

Lamma is available on both Scala 2.11 and 2.10

Click here for other download options like sbt or maven.

Step 2: Start with Scala REPL

Execute scala -cp lamma_2.11-2.2.0.jar for Scala 2.11 or scala -cp lamma_2.10-2.2.0.jar for 2.10.

Step 3: Basic Date Operations

Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_25).
Type in expressions to have them evaluated.
Type :help for more information.

scala> import io.lamma._   //IMPORTANT always import whole `io.lamma` package
import io.lamma._

scala> Date(2014, 7, 5)     // create io.lamma.Date object with yyyy, MM and dd
res0: io.lamma.Date = Date(2014,7,5)

scala> Date("2014-05-05")   // create io.lamma.Date object with ISO 8601 format string "yyyy-MM-dd"
res1: io.lamma.Date = Date(2014,5,5)

// ========== compare two dates ==============
scala> Date(2014, 7, 7) < Date(2014, 7, 8)
res2: Boolean = true

scala> (2014, 7, 7) < (2014, 7, 8)      // we can also use tuple form to represent a date
res3: Boolean = true

scala> (2014, 7, 7) >= (2014, 7, 8)     // other than <, Lamma also support >, <=, >= and ==
res4: Boolean = false

// ========== manipulate dates =============
scala> (2014, 7, 5) + 2  // add 2 days
res5: io.lamma.Date = Date(2014,7,7)

scala> (2014, 7, 5) + (2 weeks)         // add 2 weeks. Remember there is a ()
res6: io.lamma.Date = Date(2014,7,19)

scala> (2014, 7, 5) + (5 months)        // add 5 months. Again, remember the ()
res7: io.lamma.Date = Date(2014,12,5)

scala> (2014, 7, 5) + (10 years)        // add 10 years
res8: io.lamma.Date = Date(2024,7,5)

scala> (2014, 7, 5) - 5                 // `-` will work same as days / weeks / months / years
res9: io.lamma.Date = Date(2014,6,30)

scala> (2014, 7, 10) - (2014, 7, 3)     // minus date from another date will return the day difference
res10: Int = 7

// ========== week related ops ============
scala> Date(2014, 7, 7).is(Monday)     // check day-of-week
res11: Boolean = true

scala> Date(2014, 7, 7).next(Friday)    // find the next Friday excludes today
res12: io.lamma.Date = Date(2014,7,11)

scala> Date(2014, 7, 7).previous(Wednesday)  // find the past Wednesday excludes today
res13: io.lamma.Date = Date(2014,7,2)

scala> Date(2014, 7, 7).withDayOfWeek(Monday)   // Monday of this week. (a week starts with Monday)
res14: io.lamma.Date = Date(2014,7,7)

scala> Date(2014, 7, 7).withDayOfWeek(Sunday)   // Sunday of this week. (a week ends with Sunday)
res15: io.lamma.Date = Date(2014,7,13)

scala> Date(2014, 7, 7).daysOfWeek        // list of dates (Iterable[Date]) of this week
res16: io.lamma.DateRangeBuilder = DateRange(Date(2014,7,7), Date(2014,7,8), Date(2014,7,9), Date(2014,7,10), Date(2014,7,11), Date(2014,7,12), Date(2014,7,13))

// ========== month related ops ============
scala> Date(2014, 7, 7).daysOfMonth       // list of dates (Iterable[Date]) of this month
res17: io.lamma.DateRangeBuilder = DateRange(Date(2014,7,1), Date(2014,7,2), Date(2014,7,3), Date(2014,7,4), Date(2014,7,5), Date(2014,7,6), Date(2014,7,7), Date(2014,7,8), Date(2014,7,9), Date(2014,7,10), Date(2014,7,11), Date(2014,7,12), Date(2014,7,13), Date(2014,7,14), Date(2014,7,15), Date(2014,7,16), Date(2014,7,17), Date(2014,7,18), Date(2014,7,19), Date(2014,7,20), Date(2014,7,21), Date(2014,7,22), Date(2014,7,23), Date(2014,7,24), Date(2014,7,25), Date(2014,7,26), Date(2014,7,27), Date(2014,7,28), Date(2014,7,29), Date(2014,7,30), Date(2014,7,31))

scala> Date(2016, 2, 5).maxDayOfMonth   // max day of the month. Leap day is considered.
res18: Int = 29

scala> Date(2016, 2, 5).lastDayOfMonth    // last day of the month, leap day is considered.
res19: io.lamma.Date = Date(2016,2,29)

scala> Date(2016, 2, 5).sameWeekdaysOfMonth   // all Fridays in Feb, 2016
res20: List[io.lamma.Date] = List(Date(2016,2,5), Date(2016,2,12), Date(2016,2,19), Date(2016,2,26))

// ========== year related ops ============
scala> Date(2016, 2, 5).maxDayOfYear    // max day of the year.
res21: Int = 366

scala> Date(2016, 2, 5).dayOfYear       // 2016-02-05 is the 36th day of 2016
res22: Int = 36

scala> Date(2014, 7, 7).sameWeekdaysOfYear   // all Fridays in 2016
res23: List[io.lamma.Date] = List(Date(2014,1,6), Date(2014,1,13), Date(2014,1,20), Date(2014,1,27), Date(2014,2,3), Date(2014,2,10), Date(2014,2,17), Date(2014,2,24), Date(2014,3,3), Date(2014,3,10), Date(2014,3,17), Date(2014,3,24), Date(2014,3,31), Date(2014,4,7), Date(2014,4,14), Date(2014,4,21), Date(2014,4,28), Date(2014,5,5), Date(2014,5,12), Date(2014,5,19), Date(2014,5,26), Date(2014,6,2), Date(2014,6,9), Date(2014,6,16), Date(2014,6,23), Date(2014,6,30), Date(2014,7,7), Date(2014,7,14), Date(2014,7,21), Date(2014,7,28), Date(2014,8,4), Date(2014,8,11), Date(2014,8,18), Date(2014,8,25), Date(2014,9,1), Date(2014,9,8), Date(2014,9,15), Date(2014,9,22), Date(2014,9,29), Date(2014,10,6), Date(2014,10,13), Date(2014,10,20), Date(2014,10,27), Date(2014,11,3), Date(2014,11,10), Date(...
import io.lamma.Date;

public class QuickStart {

    public static void main(String [] args) {
        // ========== date creation =============
        // create date with constructor
        System.out.println(new Date(2014, 7, 5));
        // Output: Date(2014,7,5)

        // create date with helper method
        System.out.println(Dates.newDate(2014, 7, 5));
        // Output: Date(2014,7,5)

        // create date with ISO 8601 format
        System.out.println(Dates.newDate("2014-07-05"));
        // Output: Date(2014,7,5)

        // ========= compare dates =============
        System.out.println(Dates.newDate(2014, 7, 7).isBefore(Dates.newDate(2014, 7, 8)));
        // Output: true

        // also support isAfter, isOnOrBefore and isOnOrAfter
        System.out.println(Dates.newDate(2014, 7, 7).isOnOrAfter(Dates.newDate(2014, 7, 8)));
        // Output: false

        // ========== manipulate dates =============

        System.out.println(Dates.newDate(2014, 7, 5).plusDays(2));   // plus days
        // Output: Date(2014,7,7)

        System.out.println(Dates.newDate(2014, 7, 5).plusWeeks(2));  // plus weeks
        // Output: Date(2014,7,19)

        System.out.println(Dates.newDate(2014, 7, 5).plusMonths(5)); // plus months
        // Output: Date(2014,12,5)

        System.out.println(Dates.newDate(2014, 7, 5).plusYears(10)); // plus years
        // Output: Date(2024,7,5)

        System.out.println(Dates.newDate(2014, 7, 5).minusDays(5));  // minus will work similar as plus
        // Output: Date(2014,6,30)

        // minus two Dates will result day difference in int
        System.out.println(Dates.newDate(2014, 7, 10).minus(Dates.newDate(2014, 7, 3)));
        // Output: 7 (int)

        // ==== week operations  ======

        // is(DayOfWeek)
        System.out.println(Dates.newDate(2014, 7, 7).is(DayOfWeek.MONDAY));
        // Output: true

        // find the next Friday excludes today
        System.out.println(Dates.newDate(2014, 7, 7).next(DayOfWeek.FRIDAY));
        // Output: Date(2014,7,11)

        // find the past Wednesday excludes today
        System.out.println(Dates.newDate(2014, 7, 7).previous(DayOfWeek.WEDNESDAY));
        // Output: Date(2014,7,2)

        // goto Monday of current week (a week starts with Monday)
        System.out.println(Dates.newDate(2014, 7, 7).withDayOfWeek(DayOfWeek.MONDAY));
        // Output: Date(2014,7,7)

        // goto Sunday of current week (a week starts with Monday)
        System.out.println(Dates.newDate(2014, 7, 7).withDayOfWeek(DayOfWeek.SUNDAY));
        // Output: Date(2014,7,13)

        // list of dates of this week
        for (Date d : Dates.newDate(2014, 7, 7).daysOfWeek4j()) {
            System.out.println(d);
        }
        // Output: from Date(2014,7,7) to Date(2014,7,13)

        // ======== month operations ======

        // lists of Dates of this month
        for (Date d : Dates.newDate(2014, 7, 7).daysOfMonth4j()) {
            System.out.println(d);
        }
        // Output: from Date(2014,7,1) to Date(2014,7,31)

        // max day of month, leap day is considered
        System.out.println(Dates.newDate(2016, 2, 5).maxDayOfMonth());
        // Output: 29

        // last day of this month, leap day is considered
        System.out.println(Dates.newDate(2016, 2, 5).maxDayOfMonth());
        // Output: Date(2016, 2, 29)

        // all Fridays in Feb, 2016
        for (Date d : Dates.newDate(2016, 2, 5).sameWeekdaysOfMonth4j()) {
            System.out.println(d);
        }
        // Output: Date(2016,2,5), Date(2016,2,12), Date(2016,2,19) and Date(2016,2,26)

        // ======== year operations ======

        // max day of year, leap year is considered
        System.out.println(Dates.newDate(2016, 2, 5).maxDayOfYear());
        // Output: 366

        System.out.println(Dates.newDate(2016, 2, 5).dayOfYear());       // day of year
        // Output: 36 (2016-02-05 is the 36th day of 2016)

        // all Fridays in 2016
        for (Date d : Dates.newDate(2016, 2, 5).sameWeekdaysOfYear4j()) {
            System.out.println(d);
        }
        // Output: Date(2016,1,1), Date(2016,1,8), Date(2016,1,15), ..., Date(2016,12,30)
    }
}

Check API doc for io.lamma.Date for more details.