Tutorialspoint

Java Hello World

public class MyFirstJavaProgram {
   public static void main(String []args) {
      System.out.println("Hello World");
   }
} 

Java Program to get the difference between two time zones

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
public class Demo {
	public static void main(String[] args) {
		ZoneId zone1 = ZoneId.of("America/Panama");
		ZoneId zone2 = ZoneId.of("Asia/Taipei");
		LocalDateTime dateTime = LocalDateTime.of(2019, 04, 11, 10, 5);
		ZonedDateTime panamaDateTime = ZonedDateTime.of(dateTime, zone1);
		ZonedDateTime taipeiDateTime = panamaDateTime.withZoneSameInstant(zone2);
		System.out.println("Difference between two time zones in seconds = "+taipeiDateTime.getOffset().getTotalSeconds());
	}
}

Java Program to create custom DateTime formatter

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
public class Demo {
	public static void main(String[] args) {
		DateTimeFormatter dtFormat =  new DateTimeFormatterBuilder()
		.appendValue(ChronoField.HOUR_OF_DAY)
		.appendLiteral(":")
		.appendValue(ChronoField.MINUTE_OF_HOUR)
		.appendLiteral(":")
		.appendValue(ChronoField.SECOND_OF_MINUTE)
		.toFormatter();
		System.out.println("Time = "+dtFormat.format(LocalDateTime.now()));
		dtFormat =  new DateTimeFormatterBuilder()
		.appendValue(ChronoField.YEAR)
		.appendLiteral("/")
		.appendValue(ChronoField.MONTH_OF_YEAR)
		.appendLiteral("/")
		.appendValue(ChronoField.DAY_OF_MONTH)
		.toFormatter();
		System.out.println("Date = "+dtFormat.format(LocalDateTime.now()));
	}
}

Java Program to convert java.util.Date to java.time.LocalDateTime

import java.time.ZoneId;
import java.util.Date;
public class Demo {
	public static void main(String[] args) {
		java.util.Date  date = new Date();
		System.out.println("Date = "+date);
		java.time.LocalDateTime dateTime = java.time.LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
		System.out.println("LocalDateTime = "+dateTime);
	}
}

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

1 2 3 4 5 6 7 ... 499 Next
Advertisements
Loading...

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