java.time
The JSR-310 implementation, also known as java.time
or the modern date-time API provides us with two ways to get it:
LocalDate#atStartOfDay
: Combines a date with the time of midnight to create a LocalDateTime
at the start of this date.
LocalDate#atStartOfDay(ZoneId zone)
: Returns a ZonedDateTime
from this date at the earliest valid time according to the rules in the time-zone.
Demo:
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Main {
public static void main(String[] args) {
LocalDateTime ldt1 = LocalDate.of(2016, Month.MARCH, 27).atStartOfDay();
LocalDateTime ldt2 = LocalDate.of(2016, Month.MARCH, 28).atStartOfDay();
ZonedDateTime zdt1 = LocalDate.of(2016, Month.MARCH, 27).atStartOfDay(ZoneId.of("Europe/London"));
ZonedDateTime zdt2 = LocalDate.of(2016, Month.MARCH, 28).atStartOfDay(ZoneId.of("Europe/London"));
System.out.println(ldt1);
System.out.println(ldt2);
System.out.println(zdt1);
System.out.println(zdt2);
}
}
Output:
2016-03-27T00:00
2016-03-28T00:00
2016-03-27T00:00Z[Europe/London]
2016-03-28T00:00+01:00[Europe/London]
Source: timeanddate.com
Learn more about java.time
, the modern date-time API* from Trail: Date Time.
* For any reason, if you have to stick to Java 6 or Java 7, you can use ThreeTen-Backport which backports most of the java.time functionality to Java 6 & 7. If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring and How to use ThreeTenABP in Android Project.