Working with Dates
Date Creation and Initialization
Basic Date Creation Methods
import java.time.LocalDate;
public class DateCreation {
public static void main(String[] args) {
// Current date
LocalDate today = LocalDate.now();
// Specific date
LocalDate specificDate = LocalDate.of(2023, 6, 15);
// Date from string
LocalDate parsedDate = LocalDate.parse("2023-12-31");
System.out.println("Current Date: " + today);
System.out.println("Specific Date: " + specificDate);
System.out.println("Parsed Date: " + parsedDate);
}
}
Date Manipulation Techniques
Common Date Operations
graph TD
A[Date Manipulation] --> B[Adding Time]
A --> C[Subtracting Time]
A --> D[Comparing Dates]
A --> E[Adjusting Dates]
Date Arithmetic and Comparisons
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
public class DateManipulation {
public static void main(String[] args) {
LocalDate baseDate = LocalDate.of(2023, 6, 15);
// Adding days, months, years
LocalDate futureDate = baseDate.plusDays(10);
LocalDate nextMonth = baseDate.plusMonths(1);
LocalDate nextYear = baseDate.plusYears(1);
// Subtracting time
LocalDate pastDate = baseDate.minusWeeks(2);
// Date comparison
boolean isBefore = baseDate.isBefore(futureDate);
boolean isAfter = baseDate.isAfter(pastDate);
// Calculate days between dates
long daysBetween = ChronoUnit.DAYS.between(baseDate, futureDate);
System.out.println("Future Date: " + futureDate);
System.out.println("Days Between: " + daysBetween);
}
}
Advanced Date Handling
Date Adjusters and Queries
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
public class DateAdjustment {
public static void main(String[] args) {
LocalDate currentDate = LocalDate.now();
// First day of month
LocalDate firstDay = currentDate.with(TemporalAdjusters.firstDayOfMonth());
// Last day of month
LocalDate lastDay = currentDate.with(TemporalAdjusters.lastDayOfMonth());
// Next Monday
LocalDate nextMonday = currentDate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY));
System.out.println("First Day of Month: " + firstDay);
System.out.println("Last Day of Month: " + lastDay);
System.out.println("Next Monday: " + nextMonday);
}
}
Pattern |
Description |
Example |
yyyy |
4-digit year |
2023 |
MM |
2-digit month |
06 |
dd |
2-digit day |
15 |
EEEE |
Full day name |
Thursday |
MMM |
Short month name |
Jun |
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
public class DateFormatting {
public static void main(String[] args) {
LocalDate date = LocalDate.now();
// Custom formatters
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("dd/MM/yyyy");
DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("EEEE, MMM dd, yyyy");
String formattedDate1 = date.format(formatter1);
String formattedDate2 = date.format(formatter2);
System.out.println("Format 1: " + formattedDate1);
System.out.println("Format 2: " + formattedDate2);
}
}
Best Practices
- Use
LocalDate
for date-only operations
- Prefer immutable date objects
- Use
DateTimeFormatter
for consistent formatting
- Handle potential parsing exceptions
LabEx Recommendation
LabEx provides interactive coding environments to practice and master date manipulation techniques in Java, helping developers build robust date-handling skills.