> For the complete documentation index, see [llms.txt](https://hanfak.gitbook.io/workspace/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hanfak.gitbook.io/workspace/languages/java/intermediate/date-time.md).

# Date and Time

* [Date and Time](/workspace/languages/java/intermediate/date-time.md#date-and-time)
  * [LocalDate](/workspace/languages/java/intermediate/date-time.md#localdate)
  * [LocalTime](/workspace/languages/java/intermediate/date-time.md#localtime)
  * [LocalDateTime](/workspace/languages/java/intermediate/date-time.md#localdatetime)
    * [Links](/workspace/languages/java/intermediate/date-time.md#links)
  * [ZoneDateTime](/workspace/languages/java/intermediate/date-time.md#zonedatetime)
  * [Duration](/workspace/languages/java/intermediate/date-time.md#duration)
  * [Time measurement](/workspace/languages/java/intermediate/date-time.md#time-measurement)
  * [links](/workspace/languages/java/intermediate/date-time.md#links)
* Use different libraries for differnt needs
  * `LocalDate` for just the date
  * `LocalTime` for just the time
  * `LocalDateTime` for both time and date
  * `ZoneDateTime` for time, date and time zone

## LocalDate

Examples:

The date to day is 13 May 2018

NOTE the return values is just the toString() representation of LocalDate.

* Getting current date, in format YYYY-MM-DD

  ```java
  LocalDate dateNow = LocalDate.now();
  //return 2018-05-13
  ```

Can do some maths with the date

```java
dateNow.plusDays(3);
// return 2018-05-16

dateNow.minus(5, ChronoUnit.MONTHS);
// returns 2017-12-13
```

* There are lots of methods to use

Can do some comparisions

```java
boolean equal = dateNow.isEqual(dateNow);
// returns true

dateNow.isBefore(dateNow.minusDays(1));
// reutrns false
```

Formatting the date

```java
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d/MM/uuuu");
String formattedDate = dateNow.format(formatter);
//returns 13/05/2018
```

Parsing string date

```java
LocalDate parseDate = LocalDate.parse("2016-08-16");

LocalDate parsedDateFormatted = LocalDate.parse("16-Aug-2016", DateTimeFormatter.ofPattern("d-MMM-yyyy"));

LocalDate parsedDateWordFormatted = LocalDate.parse("Tue, Aug 16 2016", DateTimeFormatter.ofPattern("E, MMM d yyyy"));
```

* Note the pattern of formatter matches the string representation of the date, and thus can turn into LocalDate format
* list of patterns
  * <https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html>

Creating Date

```java
LocalDate someDate = LocalDate.of(2015, Month.JANUARY, 20);
//returns 2015-01-20

LocalDate someOtherDate = LocalDate.of(2016, 11, 19);
// return 2016-11-19
```

Get parts of the date

```java
dateNow.get(ChronoField.MONTH_OF_YEAR);
// returns 5 (May is 5th month of year)

DayOfWeek dayOfWeek = dateNow.getDayOfWeek();
// return sunday
```

Alter parts of the date

```java
LocalDate changedDayOfWeek = dateNow.withDayOfMonth(DayOfWeek.FRIDAY.getValue());
// return 2018-05-05

LocalDate dateWithChangedYear = dateNow.with(ChronoField.YEAR, 2024);
// returns 2024-05-13
```

## LocalTime

Very similar methods to those in LocalDate

## LocalDateTime

Very similar methods to those in LocalDate

```java
```

### Links

## ZoneDateTime

## Duration

* <https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html>

## Time measurement

A great way of measuring how long a method call takes.

```java
long startTime = System.currentTimeMillis();

someMethodToMeasure();

long endTime   = System.currentTimeMillis();

long totalTime = endTime - startTime;
```

* <http://tutorials.jenkov.com/java-date-time/time-measurement.html>

  **links**
* <https://dzone.com/articles/java-8-date-and-time>
* <http://www.oracle.com/technetwork/articles/java/jf14-date-time-2125367.html>
* <http://www.baeldung.com/java-8-date-time-intro>
* <https://www.tutorialspoint.com/java8/java8_datetime_api.htm>
* <https://www.journaldev.com/2800/java-8-date-localdate-localdatetime-instant>
* <http://tutorials.jenkov.com/java-date-time/index.html>
* <https://github.com/winterbe/java8-tutorial#date-api>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://hanfak.gitbook.io/workspace/languages/java/intermediate/date-time.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
