📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
- LocalTime
- LocalDateTime
1. LocalTime
The LocalTime class is similar to the other classes whose names are prefixed with Local, but deals in time only. This class is useful for representing human-based time of day, such as movie times, or the opening and closing times of the local library.1.1 LocalTime Class Examples
Example 1: An instance of current LocalTime can be created from the system clock as below:LocalTime now = LocalTime.now();
LocalTime sixThirty = LocalTime.parse("06:30");
LocalTime sixThirty = LocalTime.of(6, 30);
LocalTime sevenThirty = LocalTime.parse("06:30").plus(1, ChronoUnit.HOURS);
int six = LocalTime.parse("06:30").getHour();
boolean isbefore = LocalTime.parse("06:30").isBefore(LocalTime.parse("07:30"));
For example, the below code represents 23:59:59.99:
LocalTime maxTime = LocalTime.MAX
1.2 Complete Example for Reference
public class UseLocalTime {
LocalTime getLocalTimeUsingFactoryOfMethod(int hour, int min, int seconds) {
return LocalTime.of(hour, min, seconds);
}
LocalTime getLocalTimeUsingParseMethod(String timeRepresentation) {
return LocalTime.parse(timeRepresentation);
}
//Obtains the current time from the system clock in the default time-zone.
private LocalTime getLocalTimeFromClock() {
return LocalTime.now();
}
// Returns a copy of this time with the specified amount added.
LocalTime addAnHour(LocalTime localTime) {
return localTime.plus(1, ChronoUnit.HOURS);
}
int getHourFromLocalTime(LocalTime localTime) {
return localTime.getHour();
}
LocalTime getLocalTimeWithMinuteSetToValue(LocalTime localTime, int minute) {
return localTime.withMinute(minute);
}
}
2. LocalDateTime
The LocalDateTime is used to represent a combination of date and time.This is the most commonly used class when we need a combination of date and time. The class offers a variety of APIs and we will look at some of the most commonly used ones.
2.1 LocalDateTime Class Examples
Example 1: An instance of LocalDateTime can be obtained from the system clock similar to LocalDate and LocalTime:LocalDateTime.now();
LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30);
LocalDateTime.parse("2015-02-20T06:30:00");
localDateTime.plusDays(1);
localDateTime.minusHours(2);
localDateTime.getMonth();
2.2 Complete Example for Reference
public class UseLocalDateTime {
public static LocalDateTime getLocalDateTimeUsingParseMethod(String representation) {
return LocalDateTime.parse(representation);
}
public static LocalDateTime getCurrentDateTime() {
return LocalDateTime.now();
}
public static void main(String[] args) {
System.out.println("Current datetime :: "
+ UseLocalDateTime.getCurrentDateTime());
System.out.println(" Parse to :: "
+ UseLocalDateTime.getLocalDateTimeUsingParseMethod("2015-02-20T06:30:00"));
}
}
Current datetime :: 2018-07-11T16:02:53.295
Parse to :: 2015-02-20T06:30
Comments
Post a Comment
Leave Comment