Java : Date con ejemplos
Date (Java SE 21 & JDK 21) en Java con ejemplos.
Encontrará ejemplos de código en la mayoría de los métodos de Date.
Advertencia :
La clase Date es una API heredada. Se recomienda utilizar las nuevas API como Instant y LocalDate en el paquete java.time.
Nota :
- Este artículo puede utilizar software de traducción para su comodidad. Consulte también la versión original en inglés.
Summary
final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles
final var date1 = new Date(0);
System.out.println(date1); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date1.getTime()); // 0
final var date2 = new Date(4133894400000L);
System.out.println(date2); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date2.getTime()); // 4133894400000
Constructors
Date ()
final var date = new Date();
System.out.println(date); // Fri Jun 28 23:30:30 PDT 2024
Date (int year, int month, int date)
Deprecated.
Date (int year, int month, int date, int hrs, int min)
Deprecated.
Date (int year, int month, int date, int hrs, int min, int sec)
Deprecated.
Date (long date)
final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles
final var date1 = new Date(0);
System.out.println(date1); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date1.getTime()); // 0
final var date2 = new Date(4133894400000L);
System.out.println(date2); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date2.getTime()); // 4133894400000
Date (String s)
Deprecated.
Methods
boolean after (Date when)
final var date = new Date(1000);
final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);
System.out.println(date.after(date2)); // true
System.out.println(date.after(date3)); // false
System.out.println(date.after(date4)); // false
boolean before (Date when)
final var date = new Date(1000);
final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);
System.out.println(date.before(date2)); // false
System.out.println(date.before(date3)); // false
System.out.println(date.before(date4)); // true
Object clone ()
final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles
final var date = new Date(4133894400000L);
System.out.println(date); // Thu Dec 30 16:00:00 PST 2100
final var cloned = date.clone();
System.out.println(cloned); // Thu Dec 30 16:00:00 PST 2100
int compareTo (Date anotherDate)
final var date = new Date(1000);
final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);
System.out.println(date.compareTo(date2)); // 1
System.out.println(date.compareTo(date3)); // 0
System.out.println(date.compareTo(date4)); // -1
boolean equals (Object obj)
final var date = new Date(1000);
final var date2 = new Date(500);
final var date3 = new Date(1000);
final var date4 = new Date(9999);
System.out.println(date.equals(date2)); // false
System.out.println(date.equals(date3)); // true
System.out.println(date.equals(date4)); // false
static Date from (Instant instant)
final var instant = Instant.ofEpochMilli(4133894400000L);
System.out.println(instant); // 2100-12-31T00:00:00Z
final var date = Date.from(instant);
System.out.println(date.getTime()); // 4133894400000
int getDate ()
Deprecated.
int getDay ()
Deprecated.
int getHours ()
Deprecated.
int getMinutes ()
Deprecated.
int getMonth ()
Deprecated.
int getSeconds ()
Deprecated.
long getTime ()
final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles
final var date = new Date(0);
System.out.println(date); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date.getTime()); // 0
date.setTime(4133894400000L);
System.out.println(date); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date.getTime()); // 4133894400000
int getTimezoneOffset ()
Deprecated.
int getYear ()
Deprecated.
int hashCode ()
final var ret1 = new Date(0).hashCode();
System.out.println(ret1); // 0
final var ret2 = new Date(1000).hashCode();
System.out.println(ret2); // 1000
final var ret3 = new Date(-10000).hashCode();
System.out.println(ret3); // 9999
static long parse (String s)
Deprecated.
void setDate (int date)
Deprecated.
void setHours (int hours)
Deprecated.
void setMinutes (int minutes)
Deprecated.
void setMonth (int month)
Deprecated.
void setSeconds (int seconds)
Deprecated.
void setTime (long time)
final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles
final var date = new Date(0);
System.out.println(date); // Wed Dec 31 16:00:00 PST 1969
System.out.println(date.getTime()); // 0
date.setTime(4133894400000L);
System.out.println(date); // Thu Dec 30 16:00:00 PST 2100
System.out.println(date.getTime()); // 4133894400000
void setYear (int year)
Deprecated.
String toGMTString ()
Deprecated.
Instant toInstant ()
final var date = new Date(4133894400000L);
final var instant = date.toInstant();
System.out.println(instant); // 2100-12-31T00:00:00Z
System.out.println(instant.toEpochMilli()); // 4133894400000
String toLocaleString ()
Deprecated.
String toString ()
final var timeZone = TimeZone.getDefault();
System.out.println(timeZone.getID()); // America/Los_Angeles
final var date1 = new Date(0);
final var str1 = date1.toString();
System.out.println(str1); // Wed Dec 31 16:00:00 PST 1969
final var date2 = new Date(4133894400000L);
final var str2 = date2.toString();
System.out.println(str2); // Thu Dec 30 16:00:00 PST 2100
static long UTC (int year, int month, int date, int hrs, int min, int sec)
Deprecated.
Related posts
- Ejemplos de API
- Calendar
- ChronoLocalDate
- ChronoLocalDateTime
- ChronoZonedDateTime
- Clock
- DateTimeException
- DateTimeParseException
- DayOfWeek
- Duration
- Era
- Instant
- InstantSource
- JapaneseDate
- LocalDate
- LocalDateTime
- LocalTime
- Month
- MonthDay
- OffsetDateTime
- OffsetTime
- Period
- Temporal
- TemporalAccessor
- TemporalAdjuster
- TemporalAdjusters
- TimeZone
- Year
- YearMonth
- ZonedDateTime
- ZoneId
- ZoneOffset