Code examples for Java 8 date migration (#460)
This commit is contained in:
parent
024b4f8f61
commit
57ed00a134
@ -0,0 +1,19 @@
|
|||||||
|
package com.baeldung.date_migration;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.time.ZoneId;
|
||||||
|
import java.time.ZonedDateTime;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
public class Conversion {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant();
|
||||||
|
ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime();
|
||||||
|
Date dateFromInstant = Date.from(Instant.now());
|
||||||
|
GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now());
|
||||||
|
Instant instantFromDate = new Date().toInstant();
|
||||||
|
ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.baeldung.date_migration;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.time.*;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.time.temporal.ChronoUnit;
|
||||||
|
|
||||||
|
public class NewApi {
|
||||||
|
public void currentTime() {
|
||||||
|
ZonedDateTime now = ZonedDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void specificTime() {
|
||||||
|
LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void extractMonth() {
|
||||||
|
Month month = LocalDateTime.now().getMonth();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void subtractTime() {
|
||||||
|
LocalDateTime fiveHoursBefore = LocalDateTime.now().minusHours(5);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void alterField() {
|
||||||
|
LocalDateTime inJune = LocalDateTime.now().withMonth(Month.JUNE.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void truncate() {
|
||||||
|
LocalTime truncated = LocalTime.now().truncatedTo(ChronoUnit.HOURS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void convertTimeZone() {
|
||||||
|
ZonedDateTime centralEastern = LocalDateTime.now().atZone(ZoneId.of("CET"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getTimeSpan() {
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
LocalDateTime hourLater = LocalDateTime.now().plusHours(1);
|
||||||
|
Duration span = Duration.between(now, hourLater);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void formatAndParse() throws ParseException {
|
||||||
|
LocalDate now = LocalDate.now();
|
||||||
|
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
|
||||||
|
String formattedDate = now.format(formatter);
|
||||||
|
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void daysInMonth() {
|
||||||
|
int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,68 @@
|
|||||||
|
package com.baeldung.date_migration;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
public class OldApi {
|
||||||
|
public void currentTime() {
|
||||||
|
Date now = new Date();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void specificTime () {
|
||||||
|
Date birthDay = new GregorianCalendar(1990, Calendar.DECEMBER, 15).getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void extractMonth() {
|
||||||
|
int month = new GregorianCalendar().get(Calendar.MONTH);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void subtractTime() {
|
||||||
|
GregorianCalendar calendar = new GregorianCalendar();
|
||||||
|
calendar.add(Calendar.HOUR_OF_DAY, -5);
|
||||||
|
Date fiveHoursBefore = calendar.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void alterField() {
|
||||||
|
GregorianCalendar calendar = new GregorianCalendar();
|
||||||
|
calendar.set(Calendar.MONTH, Calendar.JUNE);
|
||||||
|
Date inJune = calendar.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void truncate() {
|
||||||
|
Calendar now = Calendar.getInstance();
|
||||||
|
now.set(Calendar.MINUTE, 0);
|
||||||
|
now.set(Calendar.SECOND, 0);
|
||||||
|
now.set(Calendar.MILLISECOND, 0);
|
||||||
|
Date truncated = now.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void convertTimeZone() {
|
||||||
|
GregorianCalendar calendar = new GregorianCalendar();
|
||||||
|
calendar.setTimeZone(TimeZone.getTimeZone("CET"));
|
||||||
|
Date centralEastern = calendar.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getTimeSpan() {
|
||||||
|
GregorianCalendar calendar = new GregorianCalendar();
|
||||||
|
Date now = new Date();
|
||||||
|
calendar.add(Calendar.HOUR, 1);
|
||||||
|
Date hourLater = calendar.getTime();
|
||||||
|
long elapsed = hourLater.getTime() - now.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void formatAndParse() throws ParseException {
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||||
|
Date now = new Date();
|
||||||
|
String formattedDate = dateFormat.format(now);
|
||||||
|
Date parsedDate = dateFormat.parse(formattedDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void daysInMonth() {
|
||||||
|
Calendar calendar = new GregorianCalendar(1990, Calendar.FEBRUARY, 20);
|
||||||
|
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user