How To Use Interval and Duration Classes of Joda Library
Posted By : Pradeep Singh Kushwah | 06-May-2018
In this blog, I am going to tell you how we can use Interval, Period, Duration classes These classes are very helpful for me to find out duration, months, overlaps interval and much more but you can use these classes for many other things.
Interval: An interval represents a duration of time between
So we start with this class.
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm"); DateTime startTime = formatter.parseDateTime("20/01/2018 00:00");
DateTime endTime = formatter.parseDateTime("23/01/2018 00:00");
Interval? interval = new Interval(startTime, endTime); //This will create the Object of Interval
After that try these methods of Interval using System out like this
System.out.println("Interval :-" + interval);
System.out.println("StartTime :-" + interval.getStart());
System.out.println("EndTime :-" + interval.getEnd());
System.out.println("no of Days :-" + interval.toDuration().getStandardDays());
System.out.println("no of Hours :-" + interval.toDuration().getStandardHours());
System.out.println("no of Minutes :-" + interval.toDuration().getStandardMinutes());
System.out.println("no of Seconds :-" + interval.toDuration().getStandardSeconds());
// Add one more Day to the interval
interval:-interval.withEnd(interval.getEnd().plusDays(1));
System.out.println ("Interval :-" + interval);
Interval :- 20-01-2018 00:00:00:000+5:30/23-01-2018 00:00:00:000+5:30
StartTime :- 20-01-2018 00:00:00:000+5:30
EndTime :- 23-01-2018 00:00:00:000+5:30no of Days :- 3
no of Hours :- 72
no of Minutes :- 4320
no of Seconds :- 259200// Add one more Day to the interval
interval:-interval.withEnd(interval.getEnd().plusDays(1));
Interval :- 20-01-2018 00:00:00:000+5:30/24-01-2018 00:00:00:000+5:30?
And Try Other Things:
System.out.println("Contain now :"+interval.containsNow());
System.out.println("Contain Date :"+interval.containsNow(formatter.parseDateTime("21/01/2018 03:41")));
DateTime otherStartTime = formatter.parseDateTime("23/01/2018 03:41");
DateTime otherEndTime = formatter.parseDateTime("30/01/2018 00:00");
Interval otherInterval = new Interval(otherStartTime,otherEndTime);
System.out.println("Interval overlaps :"+interval.overlaps(otherInterval));
System.out.println("OverLaped Interval :"+interval.overlap(otherInterval));
The output will be:
Contain now : false
Contain Date : true
Interval overlaps : true
OverLaped Interval : 23-01-2018 03:41:00:000+5:30/24-01-2018 00:00:00:000+5:30
Duration: An duration specifying a length of time period in milliseconds.A immutable duration is defined by milliseconds which is fixed number. There is no concept of fields, such as days or seconds, as these fields can vary in length.
Duration intervalDuration = new Duration(startTime, endTime); //Or You can use Interval toDuration method like this Duration intervalDuration = interval.toDuration(); System.out.println("interval Duration :-" + duration); System.out.println("no of Days :-" + duration.getStandardDays()); System.out.println("no of Hours :-" + duration.getStandardHours()); System.out.println("no of Minutes :-" + duration.getStandardMinutes()); System.out.println("no of Seconds :-" + duration.getStandardSeconds());
interval Duration :- PT7689600S
no of Days :- 4
no of Hours :- 96
no of Minutes :- 5760
no of Seconds :- 345600
Period:-An time period specifying a set of duration hours,days fields values. A immutable time period is divided into a number of fields, such as hours and seconds.
Which no of fields are supported which is defined in the PeriodType class. The standard period type, which supports months, years, weeks, hours, days, minutes, seconds and
Period period = new Period(startTime,endTime);
//Or You can use Interval toPeriod method like this
Period period = interval.toPeriod();
System.out.println("interval Period:-" + period); System.out.println("no of Days :-" + period.getDays()); System.out.println("no of Hours :-" + period.getHours()); System.out.println("no of Minutes :-" + period.getMinutes()); System.out.println("no of Seconds :-" + period.getSeconds());
no of Days :- 4
no of Hours :- 96
no of Minutes :- 5760
no of Seconds :- 345600
You can Try many
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pradeep Singh Kushwah
Pradeep is an accomplished Backend Developer with in-depth knowledge and hands-on experience in various cutting-edge technologies. He specializes in Core Java, Spring-Boot, Optaplanner, Angular, and databases such as MongoDB, Neo4j, Redis, and PostgreSQL. Additionally, he has worked with cloud services like AWS and Google Cloud, and he has experience with monitoring tools such as Datadog and Raygun. Pradeep has honed his skills in API Implementations, Integration, optimization, Webservices, Development Testings, and deployments, code enhancements, and has contributed to company values through his deliverables in various client projects, including Kairos, Slick Payroll, Captionlabs, and FarmQ. He is a creative individual with strong analytical skills and a passion for exploring and learning new technologies.