Tutorialspoint

Java Program to create a BigDecimal from a string type value

import java.math.BigDecimal;
public class Demo {
	public static void main(String[] argv) throws Exception {
		BigDecimal one = new BigDecimal("562537627.8787867");
		BigDecimal two = BigDecimal.valueOf(562L);
		one = one.add(two);
		System.out.println(one);
	}
}

Java Program to adjust Adjust LocalDate to first Day Of next month

import 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.JUNE, 15);
		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);
		day = localDate.with(TemporalAdjusters.firstDayOfNextMonth());
		System.out.println("First day of next month = "+day);
	}
}

Java Program to adjust LocalDate to first Day of Month

import 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);
	}
}

Java Program to get first Friday in a month

import 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);
	}
}

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

import 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) {
		List<DayOfWeek>dayOfWeek = new ArrayList<>();
		for (Month m: Month.values()) {
			DayOfWeek days = LocalDate.now().withYear(2019).with(m).with(TemporalAdjusters.lastDayOfMonth()).getDayOfWeek();
		dayOfWeek.add(days);
		}
		System.out.println("Last day of each month in 2019 = "+dayOfWeek);
	}
}

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

import 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 = Locale.FRENCH;
	    System.out.printf("%s%n", DayOfWeek.SUNDAY.minus(10).getDisplayName(TextStyle.SHORT, locale2));
	}
}

Java Program to get the reverse of the NavigableSet

import java.util.NavigableSet;
import java.util.TreeSet;
public class Demo {
	public static void main(String[] args) {
		NavigableSet<String> set = new TreeSet<>();
		set.add("ABC");
		set.add("DEF");
		set.add("GHI");
		set.add("JKL");
		set.add("MNO");
		set.add("PQR");
		set.add("STU");
		NavigableSet<String>reverse = set.descendingSet();
		System.out.println("NavigableSet = " + set);
		System.out.println("NavigableSet (Reverse) = " + reverse);
	}
}

Make a Hashtable read-only in Java

import java.util.Collections;
import java.util.Hashtable;
import java.util.Map;
public class Demo {
	public static void main(String[] s) {
		Hashtable<String, String>hash = new Hashtable<String, String>();
		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");
		hash.put("8", "H");
		hash.put("9", "I");
		hash.put("10", "J");
		System.out.println("Hashtable = " + hash);
		Map<String, String>m = Collections.unmodifiableMap(hash);
		System.out.println("Hashtable is now read-only!");
	}
}

Checking if a HashSet contains certain value in Java

import 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 };
		Set<Integer>set = new HashSet<Integer>(Arrays.asList(arr));
		System.out.println(set.contains(200));
		System.out.println(set.contains(150));
		System.out.println(set.contains(100));
		System.out.println(set.contains(10));
	}
}

Java Program to insert a value to a SortedSet

import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
public class Demo {
	public static void main(String[] argv) throws Exception {
		SortedSet<String> set = new TreeSet<String>();
		set.add("T");
		set.add("R");
		set.add("S");
		set.add("Q");
		set.add("V");
		set.add("U");
		set.add("W");
		Iterator<String> i = set.iterator();
		System.out.println("SortedSet elements...");
		while (i.hasNext()) {
			Object ob = i.next();
			System.out.println(ob.toString());
		}
		set.add("Z");
		set.add("Y");
		i = set.iterator();
		System.out.println("Updated SortedSet elements...");
		while (i.hasNext()) {
			Object ob = i.next();
			System.out.println(ob.toString());
		}
	}
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.