Skip to content

Commit ce34998

Browse files
New Examples
1 parent 5811443 commit ce34998

File tree

3 files changed

+132
-0
lines changed

3 files changed

+132
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.howtodoinjava.core.basic.math;
2+
3+
public class MathFloorDivCeilDiv {
4+
5+
public static void main(String[] args) {
6+
7+
}
8+
9+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.howtodoinjava.core.datetime;
2+
3+
import java.time.Month;
4+
import java.time.format.TextStyle;
5+
import java.util.Arrays;
6+
import java.util.Locale;
7+
import java.util.Optional;
8+
9+
public class ConvertBetweenMonthNameAndNumber {
10+
11+
public static void main(String[] args) {
12+
13+
// MONTH NUMBER -> NAME
14+
15+
System.out.println(monthNumberToAbbr(1));
16+
System.out.println(monthNumberToFullName(1));
17+
System.out.println(monthNumberToName(1));
18+
19+
// MONTH ABBR -> Number
20+
21+
System.out.println(monthNameToNumber("January"));
22+
System.out.println(monthAbbrToNumber("Jan"));
23+
24+
// MONTH NAME -> NAME
25+
26+
System.out.println(monthAbbrToFullName("Jan"));
27+
}
28+
29+
public static String monthNumberToAbbr(int monthNumber) {
30+
return Month.of(monthNumber).getDisplayName(
31+
TextStyle.SHORT, Locale.getDefault()
32+
);
33+
}
34+
35+
public static String monthNumberToFullName(int monthNumber) {
36+
return Month.of(monthNumber).getDisplayName(
37+
TextStyle.FULL, Locale.getDefault()
38+
);
39+
}
40+
41+
public static String monthNumberToName(int monthNumber) {
42+
return Month.of(monthNumber).name();
43+
}
44+
45+
public static int monthNameToNumber(String monthName) {
46+
return Month.valueOf(monthName.toUpperCase()).getValue();
47+
}
48+
49+
public static int monthAbbrToNumber(String abbreviation) {
50+
Optional<Month> monthOptional = Arrays.stream(Month.values())
51+
.filter(month -> month.name().substring(0, 3).equalsIgnoreCase(abbreviation))
52+
.findFirst();
53+
54+
return monthOptional.orElseThrow(IllegalArgumentException::new).getValue();
55+
}
56+
57+
public static String monthAbbrToFullName(String abbreviation) {
58+
Optional<Month> monthOptional = Arrays.stream(Month.values())
59+
.filter(month -> month.name().substring(0, 3).equalsIgnoreCase(abbreviation))
60+
.findFirst();
61+
62+
return monthOptional.orElseThrow(IllegalArgumentException::new)
63+
.getDisplayName(TextStyle.FULL, Locale.getDefault());
64+
}
65+
66+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.howtodoinjava.core.datetime;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.LocalTime;
5+
import java.time.ZoneId;
6+
import java.time.ZonedDateTime;
7+
import java.time.format.DateTimeFormatter;
8+
9+
public class DayPeriodFormatter {
10+
11+
static LocalTime night = LocalTime.of(21, 0, 0);
12+
static LocalTime morning = LocalTime.of(6, 0, 0);
13+
static LocalTime afternoon = LocalTime.of(12, 0, 0);
14+
static LocalTime evening = LocalTime.of(18, 0, 0);
15+
static LocalTime almostMidnight = LocalTime.of(23, 59, 59);
16+
static LocalTime midnight = LocalTime.of(0, 0, 0);
17+
18+
public static void main(String[] args) {
19+
20+
LocalDateTime localDateTime = LocalDateTime.now();
21+
22+
System.out.println(DayPeriodFormatter.formatBeforeJava16(localDateTime, ZoneId.systemDefault()));
23+
System.out.println(DayPeriodFormatter.formatSinceJava16(localDateTime, ZoneId.systemDefault()));
24+
}
25+
26+
public static String formatBeforeJava16(LocalDateTime localDateTime, ZoneId zoneId) {
27+
28+
ZonedDateTime zdt = localDateTime.atZone(zoneId);
29+
LocalTime lt = zdt.toLocalTime();
30+
31+
DateTimeFormatter formatter
32+
= DateTimeFormatter.ofPattern("yyyy-MMM-dd");
33+
34+
String dayTime = "";
35+
36+
if ((lt.isAfter(night) && lt.isBefore(almostMidnight))
37+
|| lt.isAfter(midnight) && (lt.isBefore(morning))) {
38+
dayTime = " at night";
39+
} else if (lt.isAfter(morning) && lt.isBefore(afternoon)) {
40+
dayTime = " in the morning";
41+
} else if (lt.isAfter(afternoon) && lt.isBefore(evening)) {
42+
dayTime = " in the afternoon";
43+
} else if (lt.isAfter(evening) && lt.isBefore(night)) {
44+
dayTime = " in the evening";
45+
}
46+
47+
return zdt.withZoneSameInstant(zoneId).format(formatter) + dayTime;
48+
}
49+
50+
public static String formatSinceJava16(LocalDateTime localDateTime, ZoneId zoneId) {
51+
52+
ZonedDateTime zdt = localDateTime.atZone(zoneId);
53+
DateTimeFormatter formatter
54+
= DateTimeFormatter.ofPattern("yyyy-MMM-dd [B]");
55+
return zdt.withZoneSameInstant(zoneId).format(formatter);
56+
}
57+
}

0 commit comments

Comments
 (0)