Found 33676 Articles for Programming

Java program to adjust LocalDate to first day of month with TemporalAdjusters class

karthikeya Boyini
Updated on 29-Sep-2024 02:47:31

1K+ Views

In this article, we will learn how to adjust a LocalDate object to find the first day of the month in Java. The program demonstrates how to take a given date and use the TemporalAdjusters class to easily get the first day of that month. This functionality is useful in various applications, such as scheduling events or generating monthly reportsTemporalAdjusters provides utility methods for common date adjustments. We will learn how to set a specific date and adjust it to find the first day of the month. Problem Statement Write a Java program that adjusts a LocalDate to find and ... Read More

Java program to get first Friday in a month

Samual Sam
Updated on 29-Sep-2024 02:47:00

709 Views

In this article, we'll explore how to get the first Friday of any given month in Java. The program will use Java's LocalDate class and TemporalAdjusters to automatically calculate the first Friday based on the input date. We will walk through how this works step by step and see how the code performs this task. Problem Statement Write a program in Java to get the first Friday in a month. Below is a demonstration of the same − Input Current date = 2019-04-01 Output Current date = 2019-04-01 First Friday date = 2019-04-05 Steps to get the first Friday in a ... 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

348 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 23-Nov-2024 03:47:56

351 Views

In this article, we will learn how to get the display name for a day of the week in different locales using Java. The DayOfWeek class in Java provides methods to work with days of the week, and with the help of getDisplayName(), you can retrieve the name of a day in different formats based on the locale. Java.util.Locale.getDisplayName() Method The java.util.Locale.getDisplayName(Locale inLocale) method returns a name for the locale that is appropriate for display to the user. This will be the values returned by getDisplayLanguage(), getDisplayCountry(), and getDisplayVariant() assembled into a single string. Steps to get the display ... Read More

Java Program to get the reverse of the NavigableSet

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

114 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

Shriansh Kumar
Updated on 11-Sep-2024 11:08:40

1K+ Views

Initialization of a Java HashMap means creating an instance of the HashMap class and adding elements to it. The standard way to initialize a HashMap is by using the new keyword. However, we will use the lambda expression in this article. Lambda expressions are methods without any names. It was introduced with the release of Java8. And, HashMap is a class of Java Collection Framework that implements Map Interface. It stores its element in key-value pairs. The Key is an object that is used to retrieve value associated with it. Example 1 The following Java program explains how to initialize ... Read More

Make a Hashtable read-only in Java

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

224 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

861 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

159 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

128 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