Date date = new Date();
int hours = date.getHours();
date.setHours(hours + 1);
Disponible depuis les premières versions de Java
Très déconseillée
The class Date
represents a specific instant in time, with millisecond precision.
Date date = new Date();
int hours = date.getHours();
date.setHours(hours + 1);
La classe confond la notion d’instant, et la mesure de cet instant dans un référentiel spécifique (p.e. le calendrier grégorien)
TheCalendar
class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such asYEAR
,MONTH
,DAY_OF_MONTH
,HOUR
, and so on, and for manipulating the calendar fields, such as getting the date of the next week.
Calendar now = Calendar.getInstance();
int hours = now.get(Calendar.HOUR_OF_DAY);
now.set(Calendar.HOUR_OF_DAY, 1);
now.add(Calendar.HOUR_OF_DAY, 1);
now.roll(Calendar.HOUR_OF_DAY, 1);
Calendar est une classe mutable: son état peut changer, ce qui rend compliqué son utilisation.
Corrige les problèmes de Date
et de Calendar
Disponible depuis Java 8 dans le JDK
Directement inspiré de la librairie Joda Time
An instantaneous point on the time-line.
A time-based amount of time, such as '34.5 seconds'.
A date-based amount of time in the ISO-8601 calendar system, such as '2 years, 3 months and 4 days'.
Instant now = Instant.now();
Instant in5hours4minutes =
instant.plus(Duration.ofHours(5).plusMinutes(4));
| A date without a time-zone in the ISO-8601 calendar system, such as |
| A time without a time-zone in the ISO-8601 calendar system, such as |
| A date-time without a time-zone in the ISO-8601 calendar system, such as |
LocalDate now = LocalDate.now();
LocalDate christmasThisYear =
LocalDate.of(now.getYear(), 12, 25);
LocalDate now = LocalDate.now();
LocalDate thirdMondayFromNowOn = now
.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
.with(TemporalAdjusters.next(DayOfWeek.MONDAY))
.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
LocalDate now = LocalDate.now();
DateTimeFormatter formatter =
DateTimeFormatter.ofPattern("yyyy/MM/dd");
String text = now.format(formatter);
System.out.println(text);