codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

39 / 65 topics
38Java User Input39Java Date
Tutorials/Java Programming/Java Date
☕Java Programming

Java Date

Updated 2026-05-12
30 min read

Java Date

In the world of programming, handling dates and times is a common task. Whether you're building a calendar application, tracking events, or managing user schedules, understanding how to work with dates in Java is essential. In this tutorial, we'll explore Java's date and time API, focusing on classes like LocalDate, LocalTime, LocalDateTime, and DateTimeFormatter. By the end of this lesson, you'll be able to manipulate and format dates and times effectively.

Introduction

Java provides a robust set of classes in the java.time package to handle date and time operations. These classes are designed to be immutable, thread-safe, and easy to use. We'll start by looking at how to create and manipulate dates using LocalDate, then move on to times with LocalTime, and finally combine them into a single point in time with LocalDateTime. We'll also learn how to format these date-time objects using DateTimeFormatter.

Core Content

1. LocalDate

LocalDate represents a date without time-zone in the ISO-8601 calendar system (yyyy-MM-dd). It's perfect for handling just dates, such as birthdays or event dates.

Creating a LocalDate

You can create a LocalDate object using several factory methods:

LocalDateExample.java
1import java.time.LocalDate;
2
3public class LocalDateExample {
4 public static void main(String[] args) {
5 // Current date
6 LocalDate today = LocalDate.now();
7 System.out.println("Today's date: " + today);
8
9 // Specific date
10 LocalDate specificDate = LocalDate.of(2023, 10, 15);
11 System.out.println("Specific date: " + specificDate);
12 };
13}
Output
Today's date: 2023-10-15
Specific date: 2023-10-15

Manipulating LocalDate

You can easily manipulate dates by adding or subtracting days, months, or years:

LocalDateManipulation.java
1import java.time.LocalDate;
2import java.time.temporal.ChronoUnit;
3
4public class LocalDateManipulation {
5 public static void main(String[] args) {
6 LocalDate today = LocalDate.now();
7
8 // Adding days
9 LocalDate tomorrow = today.plusDays(1);
10 System.out.println("Tomorrow: " + tomorrow);
11
12 // Subtracting months
13 LocalDate lastMonth = today.minusMonths(1);
14 System.out.println("Last month: " + lastMonth);
15
16 // Adding years
17 LocalDate nextYear = today.plusYears(1);
18 System.out.println("Next year: " + nextYear);
19
20 // Calculating difference in days
21 long daysBetween = ChronoUnit.DAYS.between(today, tomorrow);
22 System.out.println("Days between today and tomorrow: " + daysBetween);
23 }
24}
Output
Tomorrow: 2023-10-16
Last month: 2023-09-15
Next year: 2024-10-15
Days between today and tomorrow: 1

2. LocalTime

LocalTime represents a time without a time-zone in the ISO-8601 calendar system (HH:mm:ss). It's useful for handling just times, such as appointment times or meeting start times.

Creating a LocalTime

You can create a LocalTime object using several factory methods:

LocalTimeExample.java
1import java.time.LocalTime;
2
3public class LocalTimeExample {
4 public static void main(String[] args) {
5 // Current time
6 LocalTime now = LocalTime.now();
7 System.out.println("Current time: " + now);
8
9 // Specific time
10 LocalTime specificTime = LocalTime.of(14, 30);
11 System.out.println("Specific time: " + specificTime);
12 }
13}
Output
Current time: 14:30:00.123456789
Specific time: 14:30

Manipulating LocalTime

You can manipulate times by adding or subtracting hours, minutes, or seconds:

LocalTimeManipulation.java
1import java.time.LocalTime;
2import java.time.temporal.ChronoUnit;
3
4public class LocalTimeManipulation {
5 public static void main(String[] args) {
6 LocalTime now = LocalTime.now();
7
8 // Adding hours
9 LocalTime later = now.plusHours(2);
10 System.out.println("Later: " + later);
11
12 // Subtracting minutes
13 LocalTime earlier = now.minusMinutes(30);
14 System.out.println("Earlier: " + earlier);
15
16 // Adding seconds
17 LocalTime preciseLater = now.plusSeconds(45);
18 System.out.println("Precise later: " + preciseLater);
19
20 // Calculating difference in minutes
21 long minutesBetween = ChronoUnit.MINUTES.between(now, later);
22 System.out.println("Minutes between now and later: " + minutesBetween);
23 }
24}
Output
Later: 16:30:00.123456789
Earlier: 14:00:00.123456789
Precise later: 14:30:45.123456789
Minutes between now and later: 120

3. LocalDateTime

LocalDateTime represents a date-time without time-zone in the ISO-8601 calendar system (yyyy-MM-ddTHH:mm:ss). It combines the functionality of LocalDate and LocalTime.

Creating a LocalDateTime

You can create a LocalDateTime object using several factory methods:

LocalDateTimeExample.java
1import java.time.LocalDateTime;
2
3public class LocalDateTimeExample {
4 public static void main(String[] args) {
5 // Current date-time
6 LocalDateTime now = LocalDateTime.now();
7 System.out.println("Current date-time: " + now);
8
9 // Specific date-time
10 LocalDateTime specificDateTime = LocalDateTime.of(2023, 10, 15, 14, 30);
11 System.out.println("Specific date-time: " + specificDateTime);
12 }
13}
Output
Current date-time: 2023-10-15T14:30:00.123456789
Specific date-time: 2023-10-15T14:30

Manipulating LocalDateTime

You can manipulate date-times by adding or subtracting days, months, years, hours, minutes, or seconds:

LocalDateTimeManipulation.java
1import java.time.LocalDateTime;
2import java.time.temporal.ChronoUnit;
3
4public class LocalDateTimeManipulation {
5 public static void main(String[] args) {
6 LocalDateTime now = LocalDateTime.now();
7
8 // Adding days
9 LocalDateTime tomorrow = now.plusDays(1);
10 System.out.println("Tomorrow: " + tomorrow);
11
12 // Subtracting months
13 LocalDateTime lastMonth = now.minusMonths(1);
14 System.out.println("Last month: " + lastMonth);
15
16 // Adding years
17 LocalDateTime nextYear = now.plusYears(1);
18 System.out.println("Next year: " + nextYear);
19
20 // Adding hours
21 LocalDateTime later = now.plusHours(2);
22 System.out.println("Later: " + later);
23
24 // Subtracting minutes
25 LocalDateTime earlier = now.minusMinutes(30);
26 System.out.println("Earlier: " + earlier);
27
28 // Calculating difference in days
29 long daysBetween = ChronoUnit.DAYS.between(now, tomorrow);
30 System.out.println("Days between today and tomorrow: " + daysBetween);
31 }
32}
Output
Tomorrow: 2023-10-16T14:30:00.123456789
Last month: 2023-09-15T14:30:00.123456789
Next year: 2024-10-15T14:30:00.123456789
Later: 2023-10-15T16:30:00.123456789
Earlier: 2023-10-15T14:00:00.123456789
Days between today and tomorrow: 1

4. DateTimeFormatter

DateTimeFormatter is used to format date-time objects into readable strings and parse strings back into date-time objects.

Formatting Date-Time

You can format LocalDate, LocalTime, or LocalDateTime using a formatter:

DateTimeFormatterExample.java
1import java.time.LocalDate;
2import java.time.LocalDateTime;
3import java.time.LocalTime;
4import java.time.format.DateTimeFormatter;
5
6public class DateTimeFormatterExample {
7 public static void main(String[] args) {
8 LocalDate date = LocalDate.now();
9 LocalTime time = LocalTime.now();
10 LocalDateTime dateTime = LocalDateTime.now();
11
12 // Custom formatter
13 DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
14 DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
15 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
16
17 // Formatting
18 String formattedDate = date.format(dateFormatter);
19 String formattedTime = time.format(timeFormatter);
20 String formattedDateTime = dateTime.format(dateTimeFormatter);
21
22 System.out.println("Formatted Date: " + formattedDate);
23 System.out.println("Formatted Time: " + formattedTime);
24 System.out.println("Formatted DateTime: " + formattedDateTime);
25 }
26}
Output
Formatted Date: 15-10-2023
Formatted Time: 02:30 PM
Formatted DateTime: 2023-10-15 14:30:00

Parsing Date-Time

You can parse strings back into date-time objects using a formatter:

DateTimeFormatterParsingExample.java
1import java.time.LocalDate;
2import java.time.LocalDateTime;
3import java.time.LocalTime;
4import java.time.format.DateTimeFormatter;
5
6public class DateTimeFormatterParsingExample {
7 public static void main(String[] args) {
8 // Custom formatter
9 DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
10 DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("hh:mm a");
11 DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
12
13 // Parsing
14 LocalDate date = LocalDate.parse("15-10-2023", dateFormatter);
15 LocalTime time = LocalTime.parse("02:30 PM", timeFormatter);
16 LocalDateTime dateTime = LocalDateTime.parse("2023-10-15 14:30:00", dateTimeFormatter);
17
18 System.out.println("Parsed Date: " + date);
19 System.out.println("Parsed Time: " + time);
20 System.out.println("Parsed DateTime: " + dateTime);
21 }
22}
Output
Parsed Date: 2023-10-15
Parsed Time: 14:30
Parsed DateTime: 2023-10-15T14:30

Practical Example

Let's create a simple application that asks the user for their birthdate and calculates how old they are in years.

AgeCalculator.java
1import java.time.LocalDate;
2import java.time.Period;
3import java.time.format.DateTimeFormatter;
4import java.util.Scanner;
5
6public class AgeCalculator {
7 public static void main(String[] args) {
8 Scanner scanner = new Scanner(System.in);
9
10 // Input format
11 DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
12
13 System.out.print("Enter your birthdate (dd-MM-yyyy): ");
14 String inputDate = scanner.nextLine();
15
16 try {
17 // Parse the input date
18 LocalDate birthDate = LocalDate.parse(inputDate, formatter);
19
20 // Calculate age
21 Period age = Period.between(birthDate, LocalDate.now());
22
23 System.out.println("You are " + age.getYears() + " years old.");
24 } catch (Exception e) {
25 System.out.println("Invalid date format. Please enter the date in dd-MM-yyyy format.");
26 }
27
28 scanner.close();
29 }
30}
Output
Enter your birthdate (dd-MM-yyyy): 15-10-1990
You are 33 years old.

Summary

ConceptDescription
LocalDateRepresents a date without time-zone in the ISO-8601 calendar system.
LocalTimeRepresents a time without a time-zone in the ISO-8601 calendar system.
LocalDateTimeRepresents a date-time without time-zone in the ISO-8601 calendar system.
DateTimeFormatterUsed to format and parse date-time objects into readable strings.

What's Next?

In the next tutorial, we'll explore Java exceptions, including how to handle errors gracefully using try-catch blocks and custom exception classes. Understanding exceptions is crucial for building robust and error-resistant applications.

Stay tuned!


PreviousJava User InputNext Java Exceptions

Recommended Gear

Java User InputJava Exceptions