> For the complete documentation index, see [llms.txt](https://www.pranaypourkar.co.in/the-programmers-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/specialized-classes/date-and-time/examples.md).

# Examples

## LocalDate

### 1. Extract day of week

Given input is year, monthe and day. Find the day of week.

```java
public String getDay(int day, int month, int year) {
    LocalDate localDate = LocalDate.of(year, month, day);
    return  localDate.getDayOfWeek().name(); // Example: WEDNESDAY
}
```

## LocalDateTime

### 1. Given a date and time in string. Attach a timezone to it.

```java
LocalDateTime date = LocalDateTime.parse("2023-09-11T11:40");
```

This `LocalDateTime` **has no timezone information** — it just means *"11:40 on Sept 11th, 2023"*, but not in any particular zone.

```java
// System default: UTC
ZonedDateTime utcZoned = date.atZone(ZoneId.of("UTC"));

// KSA: Asia/Riyadh (UTC+3)
ZonedDateTime ksaZoned = date.atZone(ZoneId.of("Asia/Riyadh"));

System.out.println("UTC Zoned: " + utcZoned.toInstant()); // UTC Zoned: 2023-09-11T11:40:00Z
System.out.println("KSA Zoned: " + ksaZoned.toInstant()); // KSA Zoned: 2023-09-11T08:40:00Z

// Convert to Date
Date ksaDate = Date.from(ksaZoned.toInstant());
Date utcDate = Date.from(utcZoned.toInstant());
```

{% hint style="info" %}
Notice: **same `LocalDateTime`, different `Instant`** based on the zone.
{% endhint %}

## ZonedDateTime

### 1. Get current UTC time in yyyy-MM-dd'T'HH:mm:ss.SSSZ format

```java
ZonedDateTime utcNow = ZonedDateTime.now(ZoneOffset.UTC);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String formatted = utcNow.format(formatter);
System.out.println(formatted);  // Example: 2025-06-21T07:33:46.115+0000
```

### 2. Convert the current time to **Saudi Arabia time**, which is in the `Asia/Riyadh` time zone, and yyyy-MM-dd'T'HH:mm:ss.SSSZ format

```java
ZonedDateTime ksaTime = ZonedDateTime.now(ZoneId.of("Asia/Riyadh"));
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
String formatted = ksaTime.format(formatter);
System.out.println(formatted);  // Example: 2025-06-21T10:33:46.123+0300
```

### 3. Get a datetime in "dd/MM/yyyy HH:mm" with ZoneId ZONEID\_ASIA\_RIYADH = ZoneId.of("Asia/Riyadh")

```java
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class DateTimeFormattingExample {
    public static final ZoneId ZONEID_ASIA_RIYADH = ZoneId.of("Asia/Riyadh");

    public static void main(String[] args) {
        // Get the current LocalDateTime
        LocalDateTime localDateTime = LocalDateTime.now();

        // Convert LocalDateTime to ZonedDateTime with the specified ZoneId
        ZonedDateTime zonedDateTime = localDateTime.atZone(ZONEID_ASIA_RIYADH);

        // Format ZonedDateTime using a DateTimeFormatter
        String formattedDateTime = zonedDateTime.format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));

        // Print the formatted datetime
        System.out.println("Formatted DateTime: " + formattedDateTime);
    }
}
```

## OffsetDateTime

### 1. Convert a given String OffsetDateTime to different Timezone

```java
import java.time.OffsetDateTime;
import java.time.ZoneId;

public class TimeZoneExample {
    public static void main(String[] args) {
        // Example OffsetDateTime
        OffsetDateTime dateTime = OffsetDateTime.parse("2023-05-15T07:08:59.077561Z");

        // Convert to a different time zone
        ZoneId desiredTimeZone = ZoneId.of("America/New_York");
        OffsetDateTime convertedDateTime = dateTime.withOffsetSameInstant(desiredTimeZone.getRules().getOffset(dateTime.toInstant()));

        System.out.println("Original DateTime: " + dateTime);
        System.out.println("Converted DateTime: " + convertedDateTime);
    }
}
```

### 2. OffsetDateTime to LocalDate

```java
import java.time.OffsetDateTime;
import java.time.LocalDate;

public class LocalDateExample {
    public static void main(String[] args) {
        // Example OffsetDateTime
        OffsetDateTime dateTime = OffsetDateTime.parse("2023-05-15T07:08:59.077561Z");

        // Extract the date component
        LocalDate localDate = dateTime.toLocalDate();

        System.out.println("Original DateTime: " + dateTime); // Original DateTime: 2023-05-15T07:08:59.077561Z
        System.out.println("Extracted Date: " + localDate); // Extracted Date: 2023-05-15
    }
}
```

### 3. OffsetDateTime to ZonedDateTime

```java
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

public class TimeZoneExample {
    public static void main(String[] args) {
        // Example OffsetDateTime
        OffsetDateTime dateTime = OffsetDateTime.parse("2023-05-15T07:08:59.077561Z");

        // Convert to the KSA time zone (Arabia Standard Time)
        ZoneId ksaTimeZone = ZoneId.of("Asia/Riyadh");
        ZonedDateTime convertedDateTime = dateTime.atZoneSameInstant(ksaTimeZone);

        System.out.println("Original DateTime: " + dateTime);
        System.out.println("Converted DateTime (KSA): " + convertedDateTime);
    }
}
```

### 4. OffsetDateTime to String

```java
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

public class TimeZoneExample {
    public static final ZoneId ZONEID_ASIA_RIYADH = ZoneId.of("Asia/Riyadh");
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String TIME_FORMAT = "HH:mm:ss";

    public static void main(String[] args) {
        // Example OffsetDateTime
        OffsetDateTime paymentDateTime = OffsetDateTime.parse("2023-05-15T07:08:59.077561Z");

        // Convert to the "Asia/Riyadh" time zone and format the date and time components separately
        String formattedDate = paymentDateTime
                .atZoneSameInstant(ZONEID_ASIA_RIYADH)
                .format(DateTimeFormatter.ofPattern(DATE_FORMAT));

        String formattedTime = paymentDateTime
                .atZoneSameInstant(ZONEID_ASIA_RIYADH)
                .format(DateTimeFormatter.ofPattern(TIME_FORMAT));

        System.out.println("Original DateTime: " + paymentDateTime);
        System.out.println("Formatted Date: " + formattedDate);
        System.out.println("Formatted Time: " + formattedTime);
    }
}
```

## Comparison

1. In java, I have below samples file with pattern \<string>-DDMMYYYY\_HHmmss.csv

```
Test1-04082024_153000.csv
Test1-05082024_153000.csv
```

Write a code to compare timestamp and select latest file

```java
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class TimestampComparison {
    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("ddMMyyyy_HHmmss");

    private boolean isNewer(String newFileName, String oldFileName) {
        String newTimestamp = extractTimestamp(newFileName);
        String oldTimestamp = extractTimestamp(oldFileName);

        // Parse the timestamps into LocalDateTime objects
        LocalDateTime newDateTime = LocalDateTime.parse(newTimestamp, FORMATTER);
        LocalDateTime oldDateTime = LocalDateTime.parse(oldTimestamp, FORMATTER);

        // Compare LocalDateTime objects
        return newDateTime.isAfter(oldDateTime);
    }

    private String extractTimestamp(String fileName) {
        // Extract the part of the filename containing the date and time
        return fileName.split("-")[1].replace(".csv", "");
    }

    public static void main(String[] args) {
        TimestampComparison comparison = new TimestampComparison();
        System.out.println(comparison.isNewer("Test1-04082024_153000.csv", "Test1-04082024_163000.csv")); // false
        System.out.println(comparison.isNewer("Test1-05082024_153000.csv", "Test1-04082024_153000.csv")); // true
    }
}
```


---

# 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://www.pranaypourkar.co.in/the-programmers-guide/java/java-basics/java-data-types/specialized-classes/date-and-time/examples.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.
