Skip to content

Instantly share code, notes, and snippets.

View juanpabloprado's full-sized avatar
🛖
Working From Hut

Juan Pablo Prado juanpabloprado

🛖
Working From Hut
View GitHub Profile
@juanpabloprado
juanpabloprado / LocalDateTimeFormattersJShell.java
Last active April 27, 2019 21:39
LocalDateTime Formatters on the Java Shell tool
jshell> import java.util.ArrayList;
jshell> import java.time.format.DateTimeFormatter; ^
jshell> ArrayList<DateTimeFormatter> ldtFormattersList = new ArrayList<>();
ldtFormattersList ==> []
jshell> ldtFormattersList.add(DateTimeFormatter.BASIC_ISO_DATE);
$57 ==> true
@juanpabloprado
juanpabloprado / DateTimePredefinedFormattersJShell.java
Created April 27, 2019 21:28
DateTime Predefined Formatters on the Java Shell tool
jshell> import static java.time.format.DateTimeFormatter.*;
jshell> ld.format(ISO_WEEK_DATE);
$43 ==> "2019-W17-6"
jshell> import java.time.OffsetDateTime;
jshell> OffsetDateTime odt = OffsetDateTime.now();
odt ==> 2019-04-27T16:22:54.724099-05:00
@juanpabloprado
juanpabloprado / PeriodNormalizedJShell.java
Created April 27, 2019 21:17
Period normalized method on the Java Shell tool
jshell> Period p1 = Period.parse("P0Y13M");
p1 ==> P13M
jshell> "Original: " + p1 + " Normalized: " + p1.normalized();
$36 ==> "Original: P13M Normalized: P1Y1M"
jshell> Period p2 = Period.parse("P2Y-1M");
p2 ==> P2Y-1M
jshell> "Original: " + p2 + " Normalized: " + p2.normalized();
@juanpabloprado
juanpabloprado / PeriodBetweenJShell.java
Created April 27, 2019 21:02
Period between method on the Java Shell tool
shell> String WAR_OF_1812_START_DATE = "1812-06-18";
WAR_OF_1812_START_DATE ==> "1812-06-18"
jshell> String WAR_OF_1812_END_DATE = "1815-02-18";
WAR_OF_1812_END_DATE ==> "1815-02-18"
jshell> import java.time.LocalDate;
jshell> LocalDate warBegins = LocalDate.parse(WAR_OF_1812_START_DATE);
warBegins ==> 1812-06-18
@juanpabloprado
juanpabloprado / PeriodIsJShell.java
Created April 27, 2019 20:56
Period is[state] methods on the Java Shell tool
jshell> Period p1 = Period.parse("P10D").minusDays(10);
p1 ==> P0D
jshell> "Is zero: " + p1.isZero();
$24 ==> "Is zero: true"
jshell> // Period equals negative value
jshell> Period p2 = Period.parse("P2019M");
p2 ==> P2019M
@juanpabloprado
juanpabloprado / PeriodPlusMinusJShell.java
Created April 27, 2019 20:48
Period plus/minus methods on the Java Shell tool
jshell> Period period = Period.of(5, 2, 1);
period ==> P5Y2M1D
jshell> period = period.plusYears(10);
period ==> P15Y2M1D
jshell> period = period.plusMonths(10);
period ==> P15Y12M1D
jshell> period = period.plusDays(15);
@juanpabloprado
juanpabloprado / PeriodJShell.java
Last active April 27, 2019 20:39
Period Class on the Java Shell tool
jshell> Period P1 = Period.ofYears(1); // 1 year
P1 ==> P1Y
jshell> Period P2 = Period.ofMonths(12) // 1 year
P2 ==> P12M
jshell> Period P3 = Period.ofWeeks(52) // 1 year
P3 ==> P364D
jshell> Period P4 = Period.ofDays(366) // 1 year (leap)
@juanpabloprado
juanpabloprado / LocalDateAndTimePlusMinusJShell.java
Created April 24, 2019 03:14
LocalDate and LocalTime plus/minus methods on the Java Shell tool
jshell> LocalDate ld = LocalDate.now();
ld ==> 2019-04-23
jshell> // All plus methods
jshell> ld = ld.plusYears(1).plusMonths(12).plusWeeks(52).plusDays(365);
ld ==> 2023-04-22
jshell> // All minus methods
@juanpabloprado
juanpabloprado / LegacyDateTimeJShell.java
Created April 24, 2019 02:53
Legacy calendar classes on the Java Shell tool
jshell> import java.util.Calendar;
jshell> import java.time.Instant;
jshell> Calendar calendar = Calendar.getInstance();
calendar ==> java.util.GregorianCalendar[time=1556074159985,ar ... 600000,DST_OFFSET=3600000]
jshell> Instant instant = calendar.toInstant();
instant ==> 2019-04-24T02:49:19.985Z
@juanpabloprado
juanpabloprado / LocalDateTimeJShell.java
Created April 24, 2019 02:47
LocalDateTime Class on the Java Shell tool
jshell> LocalDateTime ldt1 = LocalDateTime.now();
ldt1 ==> 2019-04-23T21:41:59.084165
jshell> LocalDateTime ldt2 = LocalDateTime.parse("2019-01-01T12:00:00");
ldt2 ==> 2019-01-01T12:00
jshell> LocalDateTime ldt3 = LocalDateTime.of(2019, 1, 1, 12, 0);
ldt3 ==> 2019-01-01T12:00
jshell> LocalDateTime ldt4 = LocalDateTime.of(2019, Month.JANUARY, 1, 12, 0);