Found 4337 Articles for Java 8

Java Program to adjust LocalDate to first Day of Month with TemporalAdjusters class

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

815 Views

Let us first set a date −LocalDate localDate = LocalDate.of(2019, Month.APRIL, 10);Now, adjust LocalDate to first Day of Month −LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth());Example Live Demoimport java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Demo {    public static void main(String[] args) {       LocalDate localDate = LocalDate.of(2019, Month.APRIL, 10);       System.out.println("Current Date = "+localDate);       System.out.println("Current Month = "+localDate.getMonth());       LocalDate day = localDate.with(TemporalAdjusters.firstDayOfMonth());       System.out.println("First day of month = "+day);    } }OutputCurrent Date = 2019-04-10 Current Month = APRIL First day of month = 2019-04-01

Java Program to get first Friday in a month

Samual Sam
Updated on 30-Jul-2019 22:30:25

456 Views

At first, set a date −LocalDate date = LocalDate.of(2019, Month.APRIL, 1);Now, get the first Friday using the firstInMonth() method. Here, you have to set DayOfWeek as Friday since we want the first Friday in a month −LocalDate firstFriday = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));Example Live Demoimport java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; public class Demo {    public static void main(String[] args) {       LocalDate date = LocalDate.of(2019, Month.APRIL, 1);       System.out.println("Current date = "+date);       LocalDate firstFriday = date.with(TemporalAdjusters.firstInMonth(DayOfWeek.FRIDAY));       System.out.println("First Friday date = "+firstFriday);    } }OutputCurrent date = 2019-04-01 First Friday date = ... Read More

Java Program to get day of week for the last day of each month in 2019

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

245 Views

For last day of each month, at first create a List collection for day of week −ListdayOfWeek = new ArrayList();Now, get day of week for the last day of each month in 2019, set the year using withYear() and use lastDayOfMonth() and getDayOfWeek methods −for (Month m: Month.values()) {    DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek(); }Example Live Demoimport java.time.DayOfWeek; import java.time.LocalDate; import java.time.Month; import java.time.temporal.TemporalAdjusters; import java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] argv) {       ListdayOfWeek = new ArrayList();       for (Month m: Month.values()) {          DayOfWeek days = ... Read More

Java Program to get display name for Day of Week in different locale

Samual Sam
Updated on 30-Jul-2019 22:30:25

227 Views

To get the display name for Day of Week in different locale, let us first get the default −Locale locale = Locale.getDefault();Now, let’s say we want to consider Canada locale −Locale locale1 = Locale.CANADA;Now, get the Day of Week name for locale default and Canada −System.out.printf("%s%n", DayOfWeek.THURSDAY.minus(2).getDisplayName(TextStyle.SHORT, locale)); System.out.printf("%s%n", DayOfWeek.THURSDAY.minus(2).getDisplayName(TextStyle.SHORT, locale1));Example Live Demoimport java.time.DayOfWeek; import java.time.format.TextStyle; import java.util.Locale; public class Demo {    public static void main(String[] args) {       Locale locale = Locale.getDefault();       Locale locale1 = Locale.CANADA;       System.out.printf("%s%n", DayOfWeek.THURSDAY.minus(2).getDisplayName(TextStyle.SHORT, locale));       System.out.printf("%s%n", DayOfWeek.THURSDAY.minus(2).getDisplayName(TextStyle.SHORT, locale1));       Locale locale2 = ... Read More

Java Program to get the reverse of the NavigableSet

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

64 Views

We will create a NavigableSet collection and add some elements −NavigableSet set = new TreeSet(); set.add("ABC"); set.add("DEF"); set.add("GHI"); set.add("JKL"); set.add("MNO"); set.add("PQR"); set.add("STU");Now, use descendingSet() for the reverse of NavigableSet −NavigableSetreverse = set.descendingSet();Example Live Demoimport java.util.NavigableSet; import java.util.TreeSet; public class Demo {    public static void main(String[] args) {       NavigableSet set = new TreeSet();       set.add("ABC");       set.add("DEF");       set.add("GHI");       set.add("JKL");       set.add("MNO");       set.add("PQR");       set.add("STU");       NavigableSetreverse = set.descendingSet();       System.out.println("NavigableSet = " + set);       ... Read More

Java Program to initialize a HashMap with Lambda Expressions

Samual Sam
Updated on 30-Jul-2019 22:30:25

911 Views

To initialize a HashMap with Lambda, at first create a HashMpa and use Callable −HashMapmap = newHashMap()For key-value pair, you need to initialize them in the following way −HashMapmap = newHashMap() {    {       put(0, () -> {          return 10;       });       put(1, () -> {          return 20;       });    } };Exampleimport java.util.HashMap; import java.util.concurrent.Callable; public class Demo {    public static void main(String[] args) throws Exception {       HashMapmap = newHashMap() {          {             put(0, () -> {                return 10;             });             put(1, () -> {                return 20;             });             put(2, () -> {                return 30;             });             put(3, () -> {                return 40;             });          }};          System.out.println(map.get(0).call());          System.out.println(map.get(1).call());          System.out.println(map.get(2).call());          System.out.println(map.get(3).call());       }    } }Output10 20 30 40

Make a Hashtable read-only in Java

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

162 Views

Read-only Hashtable would mean users won’t be able to add or remove elements from it. Let us first create a Hashtable with key-value pair −Hashtablehash = new Hashtable(); hash.put("1", "A"); hash.put("2", "B"); hash.put("3", "C"); hash.put("4", "D"); hash.put("5", "E"); hash.put("6", "F"); hash.put("7", "G");Now, use unmodifiableMap() to form a Hashtable read-only −Mapm = Collections.unmodifiableMap(hash);Example Live Demoimport java.util.Collections; import java.util.Hashtable; import java.util.Map; public class Demo {    public static void main(String[] s) {       Hashtablehash = new Hashtable();       hash.put("1", "A");       hash.put("2", "B");       hash.put("3", "C");       hash.put("4", "D");       hash.put("5", ... Read More

Checking if a HashSet contains certain value in Java

Samual Sam
Updated on 30-Jul-2019 22:30:25

679 Views

Let’s say the following is our Integer array −Integer arr[] = { 50, 100, 150, 200, 250, 300 };Set the above integer array in HashSet −Setset = new HashSet(Arrays.asList(arr));Now, let us check whether the HashSet contains certain value or not −set.contains(150)TRUE is returned if the value is in the List, else FALSE is the return value.Example Live Demoimport java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Demo {    public static void main(String[] a) {       Integer arr[] = { 50, 100, 150, 200, 250, 300 };       Setset = new HashSet(Arrays.asList(arr));       System.out.println(set.contains(200));     ... Read More

Java Program to insert a value to a SortedSet

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

113 Views

Let’s say we have the following SortedSet −SortedSet set = new TreeSet(); set.add("T"); set.add("R"); set.add("S"); set.add("Q"); set.add("V"); set.add("U"); set.add("W");Now, after displaying the above elements with Iterator, you can insert values like this to a SortedSetset.add("Z"); set.add("Y");Example Live Demoimport java.util.Iterator; import java.util.SortedSet; import java.util.TreeSet; public class Demo {    public static void main(String[] argv) throws Exception {       SortedSet set = new TreeSet();       set.add("T");       set.add("R");       set.add("S");       set.add("Q");       set.add("V");       set.add("U");       set.add("W");       Iterator i = set.iterator();     ... Read More

Java Program to get Sub Map from TreeMap

Samual Sam
Updated on 30-Jul-2019 22:30:25

68 Views

To get Sub Map from TreeMap, let us first create a TreeMap and set key-value pair −TreeMaptMap = new TreeMap(); tMap.put("1", "A"); tMap.put("2", "B"); tMap.put("3", "C"); tMap.put("4", "D"); tMap.put("5", "E"); tMap.put("6", "F"); tMap.put("7", "G");Now, let’s say you need to get submap from 3 to 7. For that, use the method submap() as −=SortedMapmap = tMap.subMap("3", "7");Example Live Demoimport java.util.SortedMap; import java.util.TreeMap; public class Demo {    public static void main(String[] args) {       TreeMaptMap = new TreeMap();       tMap.put("1", "A");       tMap.put("2", "B");       tMap.put("3", "C");       tMap.put("4", "D");     ... Read More

Advertisements