Tutorialspoint

Java Program to get day of week as string

import java.time.DayOfWeek;
import java.time.LocalDate;
public class Demo {
	public static void main(String[] args) {
		LocalDate currentDate = LocalDate.now();
		System.out.println("Current Date = "+currentDate);
		DayOfWeek day = currentDate.getDayOfWeek();
		int weekVal = day.getValue();
		String weekName = day.name();
		System.out.println("Week Number = "+weekVal);
		System.out.println("Week Name = "+weekName);
	}
}

Java Program to get timezone id strings

import java.time.ZoneId;
public class Demo {
	public static void main(String[] args) {
		System.out.println("TimeZones in the US");
		ZoneId.getAvailableZoneIds().stream().filter(s ->s.startsWith("America"))
		.forEach(System.out::println);
	}
}

Java Program to get milliseconds between dates

import java.time.LocalDateTime;
import static java.time.temporal.ChronoUnit.MILLIS;
public class Demo {
	public static void main(String[] argv) {
		LocalDateTime dateOne = LocalDateTime.now();
		LocalDateTime dateTwo = LocalDateTime.of(2019, 4, 10, 11, 20);
		System.out.printf("Date One = "+dateOne);
		System.out.printf("\nDate Two = "+dateTwo);
		long res = MILLIS.between(dateOne, dateTwo);
		System.out.println("\nMilliseconds between two dates = " + res);
	}
}

Java Program to check if two dates are equal

import java.time.LocalDate;
public class Demo {
	public static void main(String[] argv) {
		LocalDate dateOne = LocalDate.now();
		LocalDate dateTwo = LocalDate.of(dateOne.getYear(), dateOne.getMonth(), dateOne.getDayOfMonth());
		System.out.printf("Date One = "+dateOne);
		System.out.printf("\nDate Two = "+dateTwo);
		if (dateOne.equals(dateTwo)) {
			System.out.printf("\nBoth the dates are same!", dateOne, dateTwo);
		}
	}
}

Java Program to get the value stored in a byte as an unsigned integer

public class Demo {
	public static void main(String[] args) {
		byte signedVal = -100;
		int unsignedVal = Byte.toUnsignedInt(signedVal);
		System.out.println("Signed value (byte) = " + signedVal);
		System.out.println("Unsigned value (byte) = " + unsignedVal);
	}
}

Java Program to generate n distinct random numbers

import java.util.LinkedHashSet;
import java.util.Random;
import java.util.Set;
public class Demo {
	public static void main(final String[] args) throws Exception {
		Random randNum = new Random();
		Set<Integer>set = new LinkedHashSet<Integer>();
		while (set.size() < 10) {
			set.add(randNum.nextInt(10)+1);
		}
		System.out.println("Distinct random numbers = "+set);
	}
}

Fill array values in Java

import java.util.Arrays;
public class Demo {
	public static void main(String[] args) {
		int[] arr = new int[10];
		System.out.println("Array = "+Arrays.toString(arr));
		Arrays.fill(arr, 2, 7, 100);
		System.out.println("Fill = "+Arrays.toString(arr));
	}
}

Java Program to fill an array with random numbers

import java.util.Arrays;
import java.util.Random;
public class Demo {
	public static void main(String args[]) {
		double[] arr = new double[5];
		Random randNum = new Random();
		for (int i = 0; i< 5; i++) {
			arr[i] = randNum.nextInt();
		}
		System.out.println("Random numbers = "+Arrays.toString(arr));
	}
}

Java Program to shift array elements to the left

import java.util.Arrays;
public class Demo {
	public static void main(String[] argv) throws Exception {
		int[] arr = { 10, 20, 30, 40, 50, 60, 70, 80, 90 };
		System.out.println("Initial array...\n"+Arrays.toString(arr));
		System.arraycopy(arr, 1, arr, 0, arr.length - 1);
		System.out.println("Array after shifting to the left...");
		System.out.println(Arrays.toString(arr));
	}
}

Java Program to create random BigInteger within a given range

import java.math.BigInteger;
import java.util.Random;
public class Demo {
	public static void main(String[] args) {
		Random random = new Random();
		BigInteger i = new BigInteger(1024, random);
		System.out.println("Random BigInteger = "+i);
	}
}

Advertisements
Loading...

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