Java 8 Date Time Interview Questions

JAVA 8 DATE TIME INTERVIEW QUESTIONS

How can we create LocalDate ?

There are two ways
  • Using now
  • Using of

package com.example.preparationforinterview;

import java.time.LocalDate;
import java.time.Month;

public class Java8DateTimeExample {
	public static void main(String[] args) {
		LocalDate localDate = LocalDate.now();
		System.out.println("LocalDate using now " + localDate);
		
		LocalDate localDate1 = LocalDate.of(1997, Month.APRIL, 1);
		System.out.println("LocalDate using of " + localDate1);
	}

}

How can we add number of Days in today date using LocalDate?

There are two ways

package com.example.preparationforinterview;

import java.time.LocalDate;

public class Java8DateTimeExample {
	public static void main(String[] args) {
		LocalDate localDate = LocalDate.now().plusDays(10);
		System.out.println("After adding Days in current date " + localDate);

	}

}

How to find duration between two instant ?


package com.example.preparationforinterview;

import java.time.Duration;
import java.time.Instant;

public class Java8DateTimeExample {
	public static void main(String[] args) {
		Instant instant1 = Instant.now();
		Instant instant2 = Instant.now().plusMillis(10000);
		Duration duration = Duration.between(instant1, instant2);
		System.out.println("Duration between two instant " + duration.toMillis());

	}

}

How to find Day of the week ?


package com.example.preparationforinterview;

import java.time.DayOfWeek;
import java.time.LocalDate;

public class Java8DateTimeExample {
	public static void main(String[] args) {
		DayOfWeek dayOfWeek = LocalDate.now().getDayOfWeek();
		System.out.println("Today day " + dayOfWeek.name());

	}

}

How can we create LocalTime ?

There are two ways
  • Using now
  • Using of

package com.example.preparationforinterview;

import java.time.LocalTime;

public class Java8DateTimeExample {
	public static void main(String[] args) {
		LocalTime localTime = LocalTime.now();
		System.out.println("current time " + localTime);
		
		LocalTime localTime1 = LocalTime.of(02, 20);
		System.out.println("specified time " + localTime1);

	}

}

How java gets information for time zones ?

Java uses Internet Assigned Numbers Authority (IANA) 's database which have information of time zones around the world.

What are the formatters provided by DateTimeFormatter ?

  • Predefined standard formatters
  • Locale-specific formatters
  • Formatters with custom patterns



Visit Others Links

Interview Questions Top Elasticsearch Interview Question
Top Kubernetes Interview Question
Top HTTP Interview Question
Top PouchDB Interview Question
Top Pivotal Cloud Foundry (pcf) Interview Question
Top Apache Camel Interview Question
Top Docker Interview Question
Top Gradle Interview Question
Top Generics Interview Questions In Java
Java 9 Interview Questions
Top Maven Interview Question
Top RabbitMQ Interview Question
Top Spring Batch Interview Question
Top Spring Boot Interview Question
Top Tika Interview Question
Top Kotlin Interview Question
Top OpenShift Interview Question
Top Polymer Interview Question
Top WebGL Interview Question
Top Continuous Integration Interview Question
Top Spring Cloud Interview Question
Top JOGL Interview Question
Top JFreeChart Interview Question
Java MultiThreading Interview Question
Top Spring ROO Interview Question
Top Seaborn Interview Question
Top TestRail Interview Question
Top LolCode Interview Question
Top Spring Transaction Management Interview Question
Top DynamoDB Interview Question
Top PySpark Interview Question
Top SaltStack Interview Question
Top Spring AOP Interview Question
© 2019 PreparationForInterview.com