Usage of JodaTime Java library
Posted By : Pradeep Singh Kushwah | 22-Apr-2018
Hi Guys,
In this blog, I am going to explain you about JodaTime classes and API provided by this library.
Joda-Time library provides a quality replacement over Java time and date classes.Joda-Time library is the de facto standard time and date library for Java SE 8. Mostly Users are now asked to migrate to java.time.Joda-Time library is licensed under the Apace 2.0 licence business-friendly.It is a quality implementation of the DateTime library for Java. JodaTime support classes include Duration, Period, Timezone, DateTimeFormat etc.
Joda library key features:
LocalDate - only date without time
LocalTime - onlytime without date
Instant - an instant point on the time-line
DateTime - full time and date with time-zone
DateTimeZone - time-zone
Duration and Period - specific amounts of time
Interval - the time interval between two instants
A flexible and comprehensive formatter-parser
Joda-Time includes following classes:
1. DateTime
2.LocalDateTime
3.LocalTime
4.LocalDate
5.DateMidnight.
Each DateTime classes provides some easy methods to access the DateTime fields. For example, to access the Year, Month, Week you can use:
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss");
DateTime datetime = formatter.parseDateTime("31/07/2018 13:55:08");
datetime.getWeekyear(); //Returns 29 Week Count in that year
datetime.getDayOfWeek(); //Returns 1 day of the week
datetime.getMonthOfYear(); //Returns 7 Month of Year
datetime.getMinuteOfDay(); //Returns 835 Minutes of Day
datetime.getYear(); //Returns 2018 Year
datetime.getHourOfDay(); //Returns 13 Hour of Day
datetime.getSecondOfDay(); //Returns 50108 Second of Day
datetime.getSecondOfMinute(); //Returns 08 Second of Minute
We can get the Object of Date, LocalDate, LocalTime like this
Date date = datetime.toDate(); //Returns Date Object 31/07/2018 13:55:08:000 LocalDate localDate = datetime.toLocalDate(); //Returns LocalDate Object 31/07/2018 LocalTime localTime = datetime.toLocalTime(); //Returns LocalTime Object 13:55:08:000 LocalDateTime = datetime.toLocalDateTime(); //Returns LocalDateTime Object 31/07/2018 13:55:08:000
We can Also Compare Two Dates like This
DateTime dateTime2 = formatter.parseDateTime("1/08/2018 11:20:00");
dateTime.isAfter(dateTime2); //Return False
dateTime.isBefore(dateTime2); //Return True
dateTime.isAfterNow() //Return False
We can Also Add Hours, Minutes, Days, Seconds, Weeks like this
dateTime.plusDays(1); //Return 01/08/2018 13:55:08
dateTime.plusMinutes(50); //Return 31/07/2018 14:45:08
dateTime.plusWeeks(7); //Return 07/08/2018 13:55:08
dateTime.plusHours(5); //Return 31/07/2018 18:55:08
dateTime.plusSeconds(60); //Return 31/07/2018 14:55:08
dateTime.plusYears(1); //Return 31/07/2019 13:55:08
Note That DateTime,LocalDate,LocalTime are Immutable Classes.So everytime you call a plus() method you need to assign it to a variable like
dateTime = dateTime.plusDays(1);
I think it will be very helpful over Java.Util.Date API.
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.