Here is an example Java class that showcases several important methods related to date and time.
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
public class DateTimeExample {
public static void main(String[] args) {
// Getting the current date
LocalDate currentDate = LocalDate.now();
System.out.println("Current Date: " + currentDate);
// Getting the current time
LocalTime currentTime = LocalTime.now();
System.out.println("Current Time: " + currentTime);
// Getting the current date and time
LocalDateTime currentDateTime = LocalDateTime.now();
System.out.println("Current Date and Time: " + currentDateTime);
// Formatting date and time
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
String formattedDate = currentDate.format(dateFormatter);
String formattedTime = currentTime.format(timeFormatter);
String formattedDateTime = currentDateTime.format(dateTimeFormatter);
System.out.println("Formatted Date: " + formattedDate);
System.out.println("Formatted Time: " + formattedTime);
System.out.println("Formatted Date and Time: " + formattedDateTime);
// Parsing date and time from strings
String dateStr = "27-06-2024";
String timeStr = "14:30:00";
String dateTimeStr = "27-06-2024 14:30:00";
LocalDate parsedDate = LocalDate.parse(dateStr, dateFormatter);
LocalTime parsedTime = LocalTime.parse(timeStr, timeFormatter);
LocalDateTime parsedDateTime = LocalDateTime.parse(dateTimeStr, dateTimeFormatter);
System.out.println("Parsed Date: " + parsedDate);
System.out.println("Parsed Time: " + parsedTime);
System.out.println("Parsed Date and Time: " + parsedDateTime);
// Adding and subtracting dates and times
LocalDate futureDate = currentDate.plusDays(10);
LocalTime futureTime = currentTime.plusHours(2);
LocalDateTime futureDateTime = currentDateTime.plusWeeks(1);
System.out.println("Future Date (10 days from now): " + futureDate);
System.out.println("Future Time (2 hours from now): " + futureTime);
System.out.println("Future Date and Time (1 week from now): " + futureDateTime);
LocalDate pastDate = currentDate.minusMonths(1);
LocalTime pastTime = currentTime.minusMinutes(30);
LocalDateTime pastDateTime = currentDateTime.minusYears(1);
System.out.println("Past Date (1 month ago): " + pastDate);
System.out.println("Past Time (30 minutes ago): " + pastTime);
System.out.println("Past Date and Time (1 year ago): " + pastDateTime);
// Calculating the difference between dates and times
long daysBetween = ChronoUnit.DAYS.between(parsedDate, currentDate);
long hoursBetween = ChronoUnit.HOURS.between(parsedTime, currentTime);
long weeksBetween = ChronoUnit.WEEKS.between(parsedDateTime, currentDateTime);
System.out.println("Days between parsed date and current date: " + daysBetween);
System.out.println("Hours between parsed time and current time: " + hoursBetween);
System.out.println("Weeks between parsed date and time and current date and time: " + weeksBetween);
}
}
Explanation
Getting the Current Date and Time:
LocalDate.now()
: Retrieves the current date.LocalTime.now()
: Retrieves the current time.LocalDateTime.now()
: Retrieves the current date and time.
Formatting Date and Time:
DateTimeFormatter.ofPattern("pattern")
: Creates a formatter with the specified pattern.format(formatter)
: Formats the date/time using the specified formatter.
Parsing Date and Time from Strings:
parse(CharSequence text, DateTimeFormatter formatter)
: Parses a date/time from a string using the specified formatter.
Adding and Subtracting Dates and Times:
plusDays(long days)
,plusHours(long hours)
,plusWeeks(long weeks)
, etc.: Adds the specified amount of time to the date/time.minusMonths(long months)
,minusMinutes(long minutes)
,minusYears(long years)
, etc.: Subtracts the specified amount of time from the date/time.
Calculating the Difference Between Dates and Times:
ChronoUnit.DAYS.between(start, end)
,ChronoUnit.HOURS.between(start, end)
,ChronoUnit.WEEKS.between(start, end)
, etc.: Calculates the difference between two dates/times in the specified units.
Comments
Post a Comment