
Java date formatter how to#
Now let's move on to how to modify date and time values using the Period and Duration classes.Using the `` class, available in Java 8 and later versions, you can easily format a date and time to match your desired output pattern. We now have a localDateTime of 06:30 +02:00. OffsetDateTime offSetByTwo = OffsetDateTime

Then we add two hours to the time by creating a ZoneOffset and setting for the localDateTime instance: ZoneOffset offset = ZoneOffset.of("+02:00") on February 20, 2015: LocalDateTime localDateTime = LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30) Here, we create a LocalDateTime representing 6:30 a.m. The OffSetDateTime instance can be created using ZoneOffset. This class stores all date and time fields, to a precision of nanoseconds, as well as the offset from UTC/Greenwich. The OffsetDateTime is an immutable representation of a date-time with an offset. The ZonedDateTime provides the parse method to get time-zone-specific date-time: ZonedDateTime.parse("T10:15:30+01:00") Īnother way to work with time zone is by using OffsetDateTime. The LocalDateTime can be converted to a specific zone: ZonedDateTime zonedDateTime = ZonedDateTime.of(localDateTime, zoneId) ZoneId zoneId = ZoneId.of("Europe/Paris") Īnd we can get a set of all zone ids: Set allZoneIds = ZoneId.getAvailableZoneIds()
Java date formatter code#
Given the above instance of LocalDateTime, this code sample will return the month February: localDateTime.getMonth() 4. Getter methods are also available to extract specific units similar to the date and time classes. These APIs behave exactly like their counterparts in LocalDate and LocalTime: usDays(1) localDateTime.minusHours(2) The below code demonstrates the “plus” and “minus” methods. There are utility APIs to support addition and subtraction of specific units of time like days, months, years and minutes. The result would be a LocalDateTime instance representing February 20, 2015, 6:30 a.m.: LocalDateTime.of(2015, Month.FEBRUARY, 20, 06, 30) LocalDateTime.parse("T06:30:00") The below code samples explain how to create an instance using the factory “of” and “parse” methods. Here, we'll look at some of the most commonly used ones.Īn instance of LocalDateTime can be obtained from the system clock similar to LocalDate and LocalTime: LocalDateTime.now() This is very useful when performing database queries to find records within a given span of time.įor example, the below code represents 23:59:59.99: LocalTime maxTime = LocalTime.MAX This code sample compares two LocalTime for which the result would be true: boolean isbefore = LocalTime.parse("06:30").isBefore(LocalTime.parse("07:30")) įinally, the max, min and noon time of a day can be obtained by constants in LocalTime class. We can also check if a specific time is before or after another specific time.

Various getter methods are available that can be used to get specific units of time like hour, min and secs: int six = LocalTime.parse("06:30").getHour()

The result would be LocalTime representing 7:30 a.m.: LocalTime sevenThirty = LocalTime.parse("06:30").plus(1, ChronoUnit.HOURS) Let's create a LocalTime by parsing a string and adding an hour to it by using the “plus” API. using the factory method: LocalTime sixThirty = LocalTime.of(6, 30) This code creates LocalTime representing 6:30 a.m. The factory method of can also be used to create a LocalTime. LocalTime sixThirty = LocalTime.parse("06:30")
