Tutorialspoint

Java Program to convert java.util.Date to Instant

import java.time.Instant;
public class Demo {
	public static void main(String[] args) {
		java.util.Date date = new java.util.Date();
		System.out.println("Date = "+date);
		Instant instant = date.toInstant();
		System.out.println("java.util.Date to Instant = "+instant);
	}
}

Java Program to fill an array of characters from user input

import java.util.Scanner;
public class Demo {
	public static void main(String args[]) {
		Scanner s = new Scanner(System.in);
		System.out.println("First add some characters...");
		char[] a = s.next().toCharArray();
		System.out.println("Elements = ");
		for (int i = 0; i < a.length; i++)
			System.out.println(a[i]);
	}
}

Java Program to double the size of an array

public class Demo {
	public static void main (String args[]) {
		int arr[] = {5, 10, 15, 20, 25, 30, 35, 40, 45, 50};
		System.out.println("Length of initial array = " + arr.length);
		int len = arr.length;
		int newArray[] = new int[len*2];
		System.arraycopy(arr, 0, newArray, 0, len);
		System.out.println("Length of new array = "+newArray.length);
	}
}

Java Program to generate random numbers with no duplicates

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() < 5) {
			set.add(randNum.nextInt(5)+1);
		}
		System.out.println("Random numbers with no duplicates = "+set);
	}
}

Java Program to shuffle an array using list

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Demo {
	public static void main(String[] args) {
		String str[] = {"A", "B", "C", "D", "E"};
		List<String>list = Arrays.asList(str);
		Collections.shuffle(list);
		System.out.println("Shuffled the array using List = "+list.toString());
	}
}

Java Program to convert java.util.Date to any local date

import java.time.ZoneId;
import java.util.Date;
public class Demo {
	public static void main(String[] args) {
		Date date = new Date();
		ZoneId zone = ZoneId.systemDefault();
		System.out.println("LocalDate = "+date.toInstant().atZone(zone).toLocalDate());
		System.out.println("LocalTime= "+date.toInstant().atZone(zone).toLocalTime());
		System.out.println("Hour = "+date.toInstant().atZone(zone).getHour());
		System.out.println("Minute = "+date.toInstant().atZone(zone).getMinute());
		System.out.println("Seconds = "+date.toInstant().atZone(zone).getSecond());
	}
}

Number of quarters between two dates in Java

import java.time.LocalDate;
import java.time.temporal.IsoFields;
public class Demo {
	public static void main(String[] args) {
		long quarters =
		IsoFields.QUARTER_YEARS.between(
		LocalDate.of(2019, 3, 20),
		LocalDate.of(2019, 10, 25));
		System.out.println("Quarters between the two dates = " + quarters);
	}
}

Discover the Quarter of a given Date in JAva

import java.time.LocalDate;
import java.util.Calendar;
import java.util.Locale;
public class Demo {
	public static void main(String[] args) {
		LocalDate currentDate = LocalDate.now();
		System.out.println("Current Date = "+currentDate);
		Calendar cal = Calendar.getInstance(Locale.US);
		int month = cal.get(Calendar.MONTH);
		int quarter = (month / 3) + 1;
		System.out.println("Quarter = "+quarter);
	}
}

Java Program to get Temporal Queries precision

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Year;
import java.time.YearMonth;
import java.time.temporal.TemporalQueries;
import java.time.temporal.TemporalQuery;
import java.time.temporal.TemporalUnit;
public class Demo {
	public static void main(String[] args) {
		TemporalQuery<TemporalUnit>precision = TemporalQueries.precision();
		System.out.println("TemporalQueries precision...");
		System.out.println(LocalDate.now().query(precision));
		System.out.println(LocalTime.now().query(precision));
		System.out.println(LocalDateTime.now().query(precision));
		System.out.println(YearMonth.now().query(precision));
		System.out.println(Year.now().query(precision));
	}
}

Java Program to get the seconds since the beginning

import java.time.Instant;
import java.time.temporal.ChronoUnit;
public class Demo {
	public static void main(String[] args) {
		long seconds = Instant.ofEpochSecond(0L).until(Instant.now(), ChronoUnit.SECONDS);
		System.out.println("Seconds since the beginning of the Java epoch = "+seconds);
	}
}

Advertisements
Loading...

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