> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud3-solucions/datas-e-horas.md).

# Datas e Horas

```java
package Fechas;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

public class CalendarioUsuario {

    public static LocalDateTime getDataHoraActual() {
        return LocalDateTime.now();
    }

    public static LocalTime getHoraActual() {
        return LocalTime.now();
    }

    public static LocalDate sumarDias(LocalDate data, long dias) {
        return data.plusDays(dias);
    }

    public static long diferenciaEntreDias(LocalDate date, LocalDate date2){
        return date.until(date2, ChronoUnit.DAYS);
    }


    public static int contarLuns(int ano) {
        int contador = 0;
        LocalDate data = LocalDate.of(ano, 1, 1);

        while (data.getDayOfWeek() != DayOfWeek.MONDAY) {
            data = data.plusDays(1);
        }

        while (data.getYear() == ano) {
            contador++;
            data = data.plusWeeks(1);
        }

        return contador;
    }
}

```

```java
package Fechas;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class App {

    public static void main(String[] args) {
        LocalDateTime hoy = CalendarioUsuario.getDataHoraActual();
        System.out.println(hoy);
        LocalTime hora = CalendarioUsuario.getHoraActual();
        System.out.println(hora);
        LocalDate nFecha = CalendarioUsuario.sumarDias(LocalDate.of(2024,11,10), 7);
        System.out.println(nFecha);
        System.out.println(" Diferencia entre dos fechas:");
        LocalDate fecha1 = LocalDate.of(2024, 1, 1);
        LocalDate fecha2 = LocalDate.of(2024, 12, 31);
        long diferenciaDias = CalendarioUsuario.diferenciaEntreDias(fecha1, fecha2);
        System.out.println("   Fecha 1: " + fecha1);
        System.out.println("   Fecha 2: " + fecha2);
        System.out.println("   Diferencia en días: " + diferenciaDias + "\n");

        System.out.println(" Contar lunes en un año:");
        int[] anos = {2024, 2023, 2025};
        for (int ano : anos) {
            int cantidadLunes = CalendarioUsuario.contarLuns(ano);
            System.out.println("   Año " + ano + ": " + cantidadLunes + " lunes");
        }
        System.out.println();

    }
}

```


---

# 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://educacion.gitbook.io/programacion/exercicios-java/ud3-solucions/datas-e-horas.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.
