# java.time

***

**1. Getting the Current Date and Time**

```java
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
```

**2. Creating a Specific Date and Time**

```java
LocalDate birthDate = LocalDate.of(1980, 1, 1);
LocalTime birthTime = LocalTime.of(12, 30);
LocalDateTime birthDateTime = LocalDateTime.of(birthDate, birthTime);
```

**3. Adding and Subtracting Time**

```java
LocalDateTime now = LocalDateTime.now();
LocalDateTime later = now.plusHours(2);
LocalDateTime earlier = now.minusMinutes(30);
```

**4. Comparing Dates and Times**

```java
LocalDate date1 = LocalDate.of(2023, 3, 8);
LocalDate date2 = LocalDate.of(2023, 3, 10);
boolean isDate1BeforeDate2 = date1.isBefore(date2);
```

**5. Parsing and Formatting Dates and Times**

```java
String dateString = "2023-03-08";
LocalDate date = LocalDate.parse(dateString);

String formattedDateString = date.format(DateTimeFormatter.ofPattern("dd/MM/yyyy"));
```

**6. Converting Between Time Zones**

```java
ZoneId fromZone = ZoneId.of("Europe/Paris");
ZoneId toZone = ZoneId.of("America/New_York");
ZonedDateTime fromDateTime = ZonedDateTime.now(fromZone);
ZonedDateTime toDateTime = fromDateTime.withZoneSameInstant(toZone);
```

**7. Calculating Differences Between Dates and Times**

```java
LocalDate date1 = LocalDate.of(2023, 3, 8);
LocalDate date2 = LocalDate.of(2023, 3, 10);
Period period = Period.between(date1, date2);
int daysBetween = period.getDays();
```

**8. Getting Weekday and Day of Year**

```java
LocalDate date = LocalDate.of(2023, 3, 8);
DayOfWeek dayOfWeek = date.getDayOfWeek();
int dayOfYear = date.getDayOfYear();
```

**9. Checking for Temporal Validity**

```java
LocalDate invalidDate = LocalDate.of(2023, 2, 30); // February only has 28 days in a non-leap year
boolean isValid = invalidDate.isValid();
```

**10. Creating Time Ranges**

```java
LocalDateTime startDateTime = LocalDateTime.of(2023, 3, 8, 12, 0);
LocalDateTime endDateTime = LocalDateTime.of(2023, 3, 8, 14, 0);
Duration duration = Duration.between(startDateTime, endDateTime);
```

**11. Converting to and from Epoch Time**

```java
long epochTime = Instant.now().getEpochSecond();
LocalDateTime dateTime = LocalDateTime.ofEpochSecond(epochTime, 0, ZoneOffset.UTC);
```

**12. Managing Daylight Saving Time (DST)**

```java
LocalDateTime nonDST = LocalDateTime.of(2023, 1, 1, 12, 0);
LocalDateTime DST = LocalDateTime.of(2023, 7, 1, 12, 0);
boolean isDST = DST.isAfter(nonDST);
```

**13. Creating TimeStamp**

```java
Instant instant = Instant.now();
long timestamp = instant.toEpochMilli();
```

**14. Creating Timer Schedules**

```java
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
ScheduledFuture<?> scheduledFuture = executor.schedule(() -> {
    // Task to be executed
}, 5, TimeUnit.SECONDS);
```

**15. Getting Today's Date**

```java
LocalDate today = LocalDate.now(ZoneId.systemDefault());
```

**16. Getting Month Value**

```java
LocalDate date = LocalDate.of(2023, 3, 8);
int monthValue = date.getMonthValue(); // 3
```

**17. Creating Interval**

```java
LocalDate startDate = LocalDate.of(2023, 3, 1);
LocalDate endDate = LocalDate.of(2023, 3, 31);
Interval interval = Interval.of(startDate, endDate);
```

**18. Getting Year**

```java
LocalDate date = LocalDate.of(2023, 3, 8);
int year = date.getYear(); // 2023
```

**19. Getting Day Value**

```java
LocalDate date = LocalDate.of(2023, 3, 8);
int dayValue = date.getDayOfMonth(); // 8
```

**20. Checking if Year is Leap Year**

```java
LocalDate date = LocalDate.of(2023, 3, 8);
boolean isLeapYear = date.isLeapYear();
```

**21. Getting Time Zone**

```java
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneId timeZone = zonedDateTime.getZone();
```

**22. Getting Offset from UTC**

```java
ZonedDateTime zonedDateTime = ZonedDateTime.now();
ZoneOffset offset = zonedDateTime.getOffset();
```

**23. Creating Duration**

```java
Duration duration = Duration.ofHours(2);
```

**24. Adding Duration to Instant**

```java
Instant instant = Instant.now();
Instant later = instant.plus(Duration.ofHours(2));
```

**25. Calculating Duration Between Two Instants**

```java
Instant start = Instant.now();
Instant end = Instant.now().plusSeconds(30);
Duration duration = Duration.between(start, end);
```

**26. Creating Period**

```java
Period period = Period.ofMonths(2);
```

**27. Adding Period to LocalDate**

```java
LocalDate date = LocalDate.now();
LocalDate later = date.plus(Period.ofMonths(2));
```

**28. Calculating Period Between Two LocalDates**

```java
LocalDate start = LocalDate.of(2023, 3, 8);
LocalDate end = LocalDate.of(2023, 5, 8);
Period period = Period.between(start, end);
```

**29. Creating OffsetDateTime**

```java
OffsetDateTime offsetDateTime = OffsetDateTime.now();
```

**30. Getting Offset**

```java
OffsetDateTime offsetDateTime = OffsetDateTime.now();
ZonedDateTime zonedDateTime = offsetDateTime.toZonedDateTime();
ZoneOffset offset = zonedDateTime.getOffset();
```

**31. Creating ZoneId**

```java
ZoneId zoneId = ZoneId.of("America/Los_Angeles");
```

**32. Creating ZoneDateTime**

```java
ZonedDateTime zonedDateTime = ZonedDateTime.now();
```

**33. Creating Duration from Nanoseconds**

```java
Duration duration = Duration.ofNanos(3000000000L);
```

**34. Creating Duration from Milliseconds**

```java
Duration duration = Duration.ofMillis(3000);
```

**35. Calculating Duration Between Two LocalDateTime**

```java
LocalDateTime start = LocalDateTime.of(2023, 3, 8, 12, 0);
LocalDateTime end = LocalDateTime.of(2023, 3, 8, 14, 0);
Duration duration = Duration.between(start, end);
```

**36. Converting LocalDateTime to Epoch Seconds**

```
```
