# java.time.temporal

***

**1. Creating a Temporal**

```java
LocalDateTime now = LocalDateTime.now();
```

**2. Getting a Field from a Temporal**

```java
int year = now.getYear();
```

**3. Setting a Field on a Temporal**

```java
LocalDateTime newDate = now.withYear(2020);
```

**4. Adding to a Temporal**

```java
LocalDateTime newDate = now.plusYears(1);
```

**5. Subtracting from a Temporal**

```java
LocalDateTime newDate = now.minusYears(1);
```

**6. Comparing Temporals**

```java
boolean isAfter = now.isAfter(newDate);
```

**7. Checking if a Temporal is Valid**

```java
boolean isValid = now.isValid();
```

**8. Getting the Chronology of a Temporal**

```java
Chronology chronology = now.getChronology();
```

**9. Converting a Temporal to another Date/Time API**

```java
Date date = now.toDate();
```

**10. Creating a TemporalAdjuster**

```java
TemporalAdjuster adjuster = TemporalAdjusters.firstDayOfMonth();
```

**11. Applying a TemporalAdjuster**

```java
LocalDateTime newDate = now.with(adjuster);
```

**12. Creating a TemporalQuery**

```java
TemporalQuery<Integer> query = TemporalQueries.year();
```

**13. Getting a Value from a Temporal using a TemporalQuery**

```java
int year = now.query(query);
```

**14. Creating a TemporalUnit**

```java
TemporalUnit unit = ChronoUnit.DAYS;
```

**15. Adding a TemporalUnit**

```java
LocalDateTime newDate = now.plus(1, unit);
```

**16. Subtracting a TemporalUnit**

```java
LocalDateTime newDate = now.minus(1, unit);
```

**17. Calculating the Difference between two Temporals**

```java
long days = ChronoUnit.DAYS.between(now, newDate);
```

**18. Creating a ZoneId**

```java
ZoneId zoneId = ZoneId.of("Europe/Paris");
```

**19. Getting the TimeZone from a ZoneId**

```java
TimeZone timeZone = zoneId.toTimeZone();
```

**20. Creating a ZonedDateTime**

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

**21. Converting a ZonedDateTime to a LocalDateTime**

```java
LocalDateTime localDateTime = zonedDateTime.toLocalDateTime();
```

**22. Creating a DateTimeFormatter**

```java
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
```

**23. Parsing a String into a Temporal**

```java
LocalDateTime parsedDate = LocalDateTime.parse("2020-01-01", formatter);
```

**24. Formatting a Temporal into a String**

```java
String formattedDate = now.format(formatter);
```

**25. Creating a TemporalAmount**

```java
TemporalAmount amount = Period.ofDays(1);
```

**26. Adding a TemporalAmount**

```java
LocalDateTime newDate = now.plus(amount);
```

**27. Subtracting a TemporalAmount**

```java
LocalDateTime newDate = now.minus(amount);
```

**28. Calculating the Duration between two Temporals**

```java
Duration duration = Duration.between(now, newDate);
```

**29. Creating a ChronoField**

```java
ChronoField field = ChronoField.DAY_OF_MONTH;
```

**30. Getting the Value of a ChronoField**

```java
int dayOfMonth = now.get(field);
```

**31. Setting the Value of a ChronoField**

```java
LocalDateTime newDate = now.with(field, 1);
```

**32. Creating a ChronoUnit**

```java
ChronoUnit unit = ChronoUnit.MONTHS;
```

**33. Adding a ChronoUnit**

```java
LocalDateTime newDate = now.plus(1, unit);
```

**34. Subtracting a ChronoUnit**

```java
LocalDateTime newDate = now.minus(1, unit);
```

**35. Creating a ZoneOffset**

```java
ZoneOffset offset = ZoneOffset.of("+01:00");
```

**36. Getting the Hours from a ZoneOffset**

```java
int hours = offset.getTotalSeconds() / 3600;
```

**37. Converting a ZoneOffset to a ZoneId**

```java
ZoneId zoneId = offset.toZoneId();
```

**38. Creating a Year**

```java
Year year = Year.of(2020);
```

**39. Getting the Value of a Year**

```java
int yearValue = year.getValue();
```

**40. Creating a Month**

```java
Month month = Month.JANUARY;
```

**41. Getting the Value of a Month**

```java
int monthValue = month.getValue();
```

**42. Creating a DayOfWeek**

```java
DayOfWeek dayOfWeek = DayOfWeek.MONDAY;
```

**43. Getting the Value of a DayOfWeek**

```java
int dayOfWeekValue = dayOfWeek.getValue();
```

**44. Creating a Duration**

```
```
