Skip to content

Commit 57ed00a

Browse files
GhostInAMachinepivovarit
authored andcommitted
Code examples for Java 8 date migration (eugenp#460)
1 parent 024b4f8 commit 57ed00a

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.date_migration;
2+
3+
import java.time.Instant;
4+
import java.time.ZoneId;
5+
import java.time.ZonedDateTime;
6+
import java.util.Date;
7+
import java.util.GregorianCalendar;
8+
import java.util.TimeZone;
9+
10+
public class Conversion {
11+
public static void main(String[] args) {
12+
Instant instantFromCalendar = GregorianCalendar.getInstance().toInstant();
13+
ZonedDateTime zonedDateTimeFromCalendar = new GregorianCalendar().toZonedDateTime();
14+
Date dateFromInstant = Date.from(Instant.now());
15+
GregorianCalendar calendarFromZonedDateTime = GregorianCalendar.from(ZonedDateTime.now());
16+
Instant instantFromDate = new Date().toInstant();
17+
ZoneId zoneIdFromTimeZone = TimeZone.getTimeZone("PST").toZoneId();
18+
}
19+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.date_migration;
2+
3+
import java.text.ParseException;
4+
import java.time.*;
5+
import java.time.format.DateTimeFormatter;
6+
import java.time.temporal.ChronoUnit;
7+
8+
public class NewApi {
9+
public void currentTime() {
10+
ZonedDateTime now = ZonedDateTime.now();
11+
}
12+
13+
public void specificTime() {
14+
LocalDate birthDay = LocalDate.of(1990, Month.DECEMBER, 15);
15+
}
16+
17+
public void extractMonth() {
18+
Month month = LocalDateTime.now().getMonth();
19+
}
20+
21+
public void subtractTime() {
22+
LocalDateTime fiveHoursBefore = LocalDateTime.now().minusHours(5);
23+
}
24+
25+
public void alterField() {
26+
LocalDateTime inJune = LocalDateTime.now().withMonth(Month.JUNE.getValue());
27+
}
28+
29+
public void truncate() {
30+
LocalTime truncated = LocalTime.now().truncatedTo(ChronoUnit.HOURS);
31+
}
32+
33+
public void convertTimeZone() {
34+
ZonedDateTime centralEastern = LocalDateTime.now().atZone(ZoneId.of("CET"));
35+
}
36+
37+
public void getTimeSpan() {
38+
LocalDateTime now = LocalDateTime.now();
39+
LocalDateTime hourLater = LocalDateTime.now().plusHours(1);
40+
Duration span = Duration.between(now, hourLater);
41+
}
42+
43+
public void formatAndParse() throws ParseException {
44+
LocalDate now = LocalDate.now();
45+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
46+
String formattedDate = now.format(formatter);
47+
LocalDate parsedDate = LocalDate.parse(formattedDate, formatter);
48+
}
49+
50+
public void daysInMonth() {
51+
int daysInMonth = YearMonth.of(1990, 2).lengthOfMonth();
52+
}
53+
54+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.date_migration;
2+
3+
import java.text.ParseException;
4+
import java.text.SimpleDateFormat;
5+
import java.util.Calendar;
6+
import java.util.Date;
7+
import java.util.GregorianCalendar;
8+
import java.util.TimeZone;
9+
10+
public class OldApi {
11+
public void currentTime() {
12+
Date now = new Date();
13+
}
14+
15+
public void specificTime () {
16+
Date birthDay = new GregorianCalendar(1990, Calendar.DECEMBER, 15).getTime();
17+
}
18+
19+
public void extractMonth() {
20+
int month = new GregorianCalendar().get(Calendar.MONTH);
21+
}
22+
23+
public void subtractTime() {
24+
GregorianCalendar calendar = new GregorianCalendar();
25+
calendar.add(Calendar.HOUR_OF_DAY, -5);
26+
Date fiveHoursBefore = calendar.getTime();
27+
}
28+
29+
public void alterField() {
30+
GregorianCalendar calendar = new GregorianCalendar();
31+
calendar.set(Calendar.MONTH, Calendar.JUNE);
32+
Date inJune = calendar.getTime();
33+
}
34+
35+
public void truncate() {
36+
Calendar now = Calendar.getInstance();
37+
now.set(Calendar.MINUTE, 0);
38+
now.set(Calendar.SECOND, 0);
39+
now.set(Calendar.MILLISECOND, 0);
40+
Date truncated = now.getTime();
41+
}
42+
43+
public void convertTimeZone() {
44+
GregorianCalendar calendar = new GregorianCalendar();
45+
calendar.setTimeZone(TimeZone.getTimeZone("CET"));
46+
Date centralEastern = calendar.getTime();
47+
}
48+
49+
public void getTimeSpan() {
50+
GregorianCalendar calendar = new GregorianCalendar();
51+
Date now = new Date();
52+
calendar.add(Calendar.HOUR, 1);
53+
Date hourLater = calendar.getTime();
54+
long elapsed = hourLater.getTime() - now.getTime();
55+
}
56+
57+
public void formatAndParse() throws ParseException {
58+
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
59+
Date now = new Date();
60+
String formattedDate = dateFormat.format(now);
61+
Date parsedDate = dateFormat.parse(formattedDate);
62+
}
63+
64+
public void daysInMonth() {
65+
Calendar calendar = new GregorianCalendar(1990, Calendar.FEBRUARY, 20);
66+
int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
67+
}
68+
}

0 commit comments

Comments
 (0)