Q:

Trigger to prevent insert record between two dates

Hi what I am trying to do is write a Trigger for a custom object where users can pass in a from and to date. Now I am trying to make sure that they can't put another record between those 2 dates i.e. from and to dates
for example:
record1: from: 23 dec 2015 to: 24 dec 2015 fine
record2: from: 24 dec 2015 to: 25 dec 2015 Error
record3: from: 1 dec 2015 to: 23 dec 2015 Error 
record4: from: 1 dec 2015 to: 11 dec 2015 fine

A:

Variants on this question have been solved a few times by @sfdcfox. The basic idea is to use complex map keys and abuse the equals method to detect overlap.
You will indeed need Apex to validate in this way. First, create an object you can use to detect overlap:
public class DateRange
{
    final Date startDate, endDate;
    public DateRange(Date startDate, Date endDate)
    {
        this.startDate = startDate;
        this.endDate = endDate;
    }
    public Integer hashCode() { return 1; }
    public Boolean equals(Object instance)
    {
        DateRange that = (DateRange)instance;
        return startDate <= that.endDate && endDate >= that.startDate;
    }

    public static DateRange factory(MyObject__c record)
    {
        return new DateRange(record.From__c, record.To__c);
    }
}

Now, your code needs to generate a Set<DateRange> and check against it for each record. If you want to validate against ranges already in the database, you need to query for them as well.
Set<DateRange> ranges = new Set<DateRange>();
for (MyObject__c record : trigger.new)
{
    DateRange range = DateRange.factory(record);
    if (ranges.contains(range))
        record.addError('<message_here>');
    else
        ranges.add(range);
}

