· java

Have a look on Java SE 8 Date and Time

Are you still using java.util.Date and SimpleDateFormatter?

If so, read the article in the link below to leverage the power of java.time, the API for Date and Time from Java 8.

―Core Ideas

―LocalDate and LocalTime

―Creating Objects

―Truncation

―Time Zones

―Time Zone Classes

―Periods

―Durations

―Chronologies

―The Rest of the API

Java SE 8 shipped with a new date and time API in java.time that offers greatly improved safety and functionality for developers. The new API models the domain well, with a good selection of classes for modeling a wide variety of developer use cases.

https://www.oracle.com/technical-resources/articles/java/jf14-date-time.html

Time formatting and parsing with DateTimeFormatter

DateTimeFormatter is a replacement for the old SimpleDateFormat that is thread-safe and provides additional functionality.

// DON'T

// DO

Getting time span between two points in time

Duration is an amount of time represented in seconds, minutes and hours. Has nanosecond precision.

Its method between(Temporal startInclusive, Temporal endExclusive) obtains a Duration representing the duration between two temporal objects.

 

// DON'T

// DO

Extracting specific fields

LocalDateTime is an immutable date-time object that represents a date-time, often viewed as year-month-day-hour-minute-second.

It has getters and setters to access date fields.

// DON'T

// DO

Time zone conversion

ZonedDateTime is an immutable representation of a date-time with a time-zone.

This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.

A ZoneID is a time-zone ID, such as Europe/Paris.

// DON'T

// DO

Adding and subtracting time

// DON'T

// DO

// SEE ALSO

Truncating

Truncating resets all time fields smaller than the specified field.

In the example below minutes and everything below will be set to zero

// DON'T

// DO

// SEE ALSO LocalDateTime

Returns a copy of this LocalDateTime with the time truncated.

Representing specific time

Given:

―LocalDate is a date without a time-zone in the ISO-8601 calendar system, such as 2007-12-03.

―Month is an enum representing the 12 months of the year from 1 (January) to 12 (December).

// DON'T

// DO

Altering specific fields

withMonth(int month), it returns a copy of this LocalDateTime with the month-of-year altered.

The time does not affect the calculation and will be the same in the result. If the day-of-month is invalid for the year, it will be changed to the last valid day of the month. This instance is immutable and unaffected by this method call.

// DON'T

// DO

// SEE ALSO