- Prefer java.time package introduce in Java 8
- Prefer java.time.format.DateTimeFormatter than
java.text.SimpleDateFormatter
- Prefer java.time.Instant than
java.sql.Timestamp
- Prefer java.time.format.DateTimeFormatter than
The time since January 1st 1970 in milliseconds
System.currentTimeMillis()
new Date().getTime()
Instant.now().toEpochMilli()
new Timestamp(System.currentTimeMillis()).getTime()
jshell> System.currentTimeMillis()
==> 1608325829399
jshell> new Date().getTime() == System.currentTimeMillis()
==> true
jshell> import java.time.Instant
jshell> Instant.now()
==> 2020-12-18T21:12:26.327318Z
jshell> Instant.now().toEpochMilli() == System.currentTimeMillis()
==> true
jshell> import java.sql.Timestamp
jshell> Timestamp.from(Instant.now())
==> 2020-12-18 13:15:36.259147
jshell> Instant now = Instant.now()
==> 2020-12-18T21:16:51.373129Z
jshell> Timestamp.from(now).getTime() == now.toEpochMilli()
==> true
Measure Code Execution Duration
long startTime = System.nanoTime();
// ... the code being measured
long endTime = System.nanoTime();
long elapsed = endTime - startTime;
Instant start = Instant.now();
// ... the code being measured
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
import java.util.concurrent.TimeUnit;
public class Timer {
private long start;
private long end;
public Timer() {
}
public static Timer startTimer() {
Timer t = new Timer();
t.start();
return t;
}
public void start() {
this.start = System.nanoTime();
}
public void end() {
this.end = System.nanoTime();
}
public long getElapsedNano() {
if (end == 0) {
end();
}
return end - start;
}
public long getElapsed(TimeUnit unit) {
return unit.convert(getElapsedNano(), TimeUnit.NANOSECONDS);
}
}
java.util.TimeZone
jshell> TimeZone.getAvailableIDs()
==> String[629] { "Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", "Africa/Asmara", "Africa/Asmera", "Africa/Bamako", "Africa/Bangui", "Africa/Banjul", "Africa/Bissau", "Africa/Blantyre", "Africa/Brazzaville", "Africa/Bujumbura", "Africa/Cairo", "Africa/Casablanca", "Africa/Ceuta", "Africa/Conakry", "Africa/Dakar", "Africa/Dar_es_Salaam", "Africa/Djibouti", "Africa/Douala", "Africa/El_Aaiun", "Africa/Freetown", "Africa/Gaborone", "Africa/Harare", "Africa/Johannesburg", "Africa/Juba", "Africa/Kampala", "Africa/Khartoum", "Africa/Kigali", "Africa/Kinshasa", "Africa/Lagos", "Africa/Libreville", "Africa/Lome", "Africa/Luanda", "Africa/Lu … US/Indiana-Starke", "US/Michigan", "US/Mountain", "US/Pacific", "US/Pacific-New", "US/Samoa", "UTC", "Universal", "W-SU", "WET", "Zulu", "EST", "HST", "MST", "ACT", "AET", "AGT", "ART", "AST", "BET", "BST", "CAT", "CNT", "CST", "CTT", "EAT", "ECT", "IET", "IST", "JST", "MIT", "NET", "NST", "PLT", "PNT", "PRT", "PST", "SST", "VST" }
java.time.format.DateTimeFormatter
jshell> import java.time.format.DateTimeFormatter
jshell> import java.time.LocalDate
jshell> DateTimeFormatter formatter = DateTimeFormatter.BASIC_ISO_DATE;
jshell> formatter.format(LocalDate.now())
==> "20201218"
jshell> DateTimeFormatter.ISO_DATE_TIME.format(LocalDateTime.now())
==> "2020-12-18T13:54:34.593405"
jshell> DateTimeFormatter.ISO_TIME.format(LocalTime.now())
==> "13:55:18.595585"
jshell> DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now())
==> "2020-12-18 14:01:11"
java.time Package
java.time – The main API for dates, times, instants, and durations
Clock | A clock providing access to the current instant, date and time using a time-zone. |
Duration | A time-based amount of time, such as ‘34.5 seconds’. |
Instant | An instantaneous point on the time-line. |
LocalDate | A date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03 . |
LocalDateTime | A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30 . |
LocalTime | A time without a time-zone in the ISO-8601 calendar system, such as 10:15:30 . |
MonthDay | A month-day in the ISO-8601 calendar system, such as --12-03 . |
OffsetDateTime | A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 . |
OffsetTime | A time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 10:15:30+01:00 . |
Period | A date-based amount of time in the ISO-8601 calendar system, such as ‘2 years, 3 months and 4 days’. |
Year | A year in the ISO-8601 calendar system, such as 2007 . |
YearMonth | A year-month in the ISO-8601 calendar system, such as 2007-12 . |
ZonedDateTime | A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris . |
ZoneId | A time-zone ID, such as Europe/Paris . |
ZoneOffset | A time-zone offset from Greenwich/UTC, such as +02:00 . |