Date and Time in Java

  1. LocalDate represents just a date(year, month, day) — not time.
  2. LocalTime represents only a time(hours, minutes, seconds, nanoseconds).
  3. LocalDateTime represents date and time.

LocalDate

  • We can add and subtract years, months, days, and weeks from our date object.
  • Note LocalDate is an immutable object so, in order to change it, we need to reassign it and most of the methods return an instance of it.
  • We can get a local date in different string formats by using java.time.format.DateTimeFormatter.java class. The pattern we provide follows regular expression.
  • if our string is in default format (YYYY-MM-dd), we can convert to date object just in one step.
  • if you have your dates in a different format than the default one, you have to use DateTimeFormatter to provide your custom pattern.
  • YYYY pattern doesn’t work so we need to use uuuu for the year.

LocalTime

  • It’s similar to LocalDate, but it’s used only for a time(hours, minutes, seconds, and nanoseconds).
  • It is immutable as well.
  • LocalTime.now() that’s how we get the current time.

LocalDateTime

  • LocalDateTime can work dates and times as well.
  • The methods are really similar to LocalDate and LocalTime
  • After T time goes

Summary

Q&A

--

--

--

Software Developer, Java Instructor https://www.techleadacademy.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to upload Database from your local to cpanel through terminal

Some HTML5 Secrets and Tricks I Learned During a Bootcamp

How to create a Python Package in Minutes

Port Swigger Lab-SQL injection vulnerability in WHERE clause allowing retrieval of hidden data

Let’s pay some attention to the man behind the curtain: the Land of SysAdmins.

Simple wildcard for Prometheus queries

Querying in ActiveRecord: Why SQL is still important

Merge Sort Explained

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Beknazar

Beknazar

Software Developer, Java Instructor https://www.techleadacademy.io/

More from Medium

Unit Testing in Java

Java Maps- My toolbox

Streams in Java — Quick Guide with Examples

what is Garbage Collector, Stack ,Heap in JAVA?