This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.LocalDate; | |
import java.time.LocalTime; | |
import java.time.Month; | |
import java.time.format.DateTimeFormatter; | |
import java.time.format.FormatStyle; | |
import java.util.Locale; | |
import java.time.LocalDateTime; | |
public class LocalDateTimeExample { | |
public static void main(String[] args) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.Duration; | |
import java.time.LocalTime; | |
import java.time.temporal.ChronoField; | |
import java.time.temporal.ChronoUnit; | |
public class LocalTimeExample { | |
public static void main(String[] args) { | |
LocalTime now = LocalTime.now(); | |
System.out.println("now: " + now); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.*; | |
import java.time.temporal.ChronoField; | |
import java.time.temporal.ChronoUnit; | |
import java.time.temporal.TemporalAdjusters; | |
public class LocalDateExample { | |
public static void main(String[] args) { | |
LocalDate today = LocalDate.now(); | |
System.out.println("today: " + today); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.ParseException; | |
import java.text.SimpleDateFormat; | |
import java.util.Calendar; | |
import java.util.Date; | |
import java.util.GregorianCalendar; | |
/* | |
Date starts from 1900 | |
Unix epoch: Time starts from 1970-1-1 | |
*/ | |
public class DateCalendarExamples{ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.SimpleDateFormat; | |
import java.util.Date; | |
/* | |
Date starts from 1900 | |
Unix epoch: Time starts from 1970-1-1 | |
There ara three common date formats: | |
D M Y | |
Y M D |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
public class SortStringBySecondChar { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(List.of("Byyyyyyyyyyyy", "Dww", "Cxxxxxx", "az")); | |
Comparator<String> sortBySecondCharComparator = (s1, s2) -> Integer.compare(s2.charAt(1), s1.charAt(1)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
public class SortStringListByLength { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(List.of("Byyyyyyyyyyyy", "Dww", "Cxxxxxx", "az")); | |
Comparator<String> lengthComparator = Comparator.comparingInt(String::length); | |
Comparator<String> lengthComparator2 = (s1, s2) -> Integer.compare(s1.length(), s2.length()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class SortStringListUsingCaseInsensitiveOrderComparator { | |
public static void main(String[] args) { | |
List<String> list = new ArrayList<>(List.of("Byyyyyyyyyyyy", "Dww", "Cxxxxxx", "az")); | |
list.sort(String.CASE_INSENSITIVE_ORDER); | |
//output: [az, Byyyyyyyyyyyy, Cxxxxxx, Dww] | |
System.out.println(list); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
public class SortStringListUsingComparatorNaturalOrder { | |
public static void main(String[] args) { | |
/* | |
In this line of code there are four strings first one is starting with B | |
and last one is starting with a, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Comparator; | |
import java.util.List; | |
public class SortListUsingComparatorNullsFirst { | |
public static void main(String[] args) { | |
List<Integer> list = new ArrayList<>(){{ | |
add(6); | |
add(null); | |
add(8); |