Found 4338 Articles for Java 8

How to get current Time in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:16:22

7K+ Views

From Java8 java.time package was introduced. This provides classes like LocalDate, LocalTime, LocalDateTime, MonthDay etc. Using classes of this package you can get time and date in a simpler way.Java.time.LocalTime − This class represents a time object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current time from the system clock.Java.time.LocalDateTime − This class represents a date-time object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date-time from the system clock.ExampleFollowing example retrieves the current time java.time package of Java8.import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; ... Read More

How to check if two dates are equal in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:10:02

1K+ Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The of() method of the java.time.LocalDate class accepts three integer parameters representing and year, a month of an year, day of a month and, returns the ... Read More

How to get a particular date in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:06:02

855 Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth, etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.The of() method of java.time.LocalDate class accepts three integer parameters representing and year, a month of an year, day of a month and, returns the instance of ... Read More

How to get current day, month and year in Java 8?

Maruthi Krishna
Updated on 07-Aug-2019 11:02:18

22K+ Views

The java.time package of Java provides API’s for dates, times, instances and durations. It provides various classes like Clock, LocalDate, LocalDateTime, LocalTime, MonthDay, Year, YearMonth etc. Using classes of this package you can get details related to date and time in much simpler way compared to previous alternatives.Java.time.LocalDate − This class represents a date object without time zone in ISO-8601 calendar system. The now() method of this class obtains the current date from the system clock.This class also provides various other useful methods among them −The getYear() method returns an integer representing the year filed in the current LocalDate object.The ... Read More

How to get today's date in Java8?

Maruthi Krishna
Updated on 07-Aug-2019 11:00:41

3K+ Views

Prior to Java8 to get current date and time we use to rely on various classes like SimpleDateFormat, Calendar etc.Exampleimport java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; public class LocalDateJava8 {    public static void main(String args[]) {       Date date = new Date();       String timeFormatString = "hh:mm:ss a";       DateFormat timeFormat = new SimpleDateFormat(timeFormatString);       String currentTime = timeFormat.format(date);       System.out.println("Current time: "+currentTime);       String dateFormatString = "EEE, MMM d, ''yy";       DateFormat dateFormat = new SimpleDateFormat(dateFormatString);       String currentDate = dateFormat.format(date);     ... Read More

Is it possible to resume java execution after exception occurs?

Maruthi Krishna
Updated on 03-Jul-2020 08:05:25

5K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.Checked Exception − A checked exception is an exception that occurs at ... Read More

Is there any way to skip finally block even if some exception occurs in exception block using java?

Maruthi Krishna
Updated on 03-Jul-2020 08:07:01

4K+ Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.Try, catch, finally blocksTo handle exceptions Java provides a try-catch block mechanism.A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code.Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }When an exception raised inside a try block, instead of terminating the program JVM stores ... Read More

Can We handle the RuntimeException in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:08:02

80 Views

A Run time exception or an unchecked exception is the one which occurs at the time of execution. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.IndexOutOfBoundsException, ArithmeticException, ArrayStoreException and, ClassCastException are the examples of run time exceptions.ExampleIn following Java program, we have an array with size 5 and we are trying to access the 6th element, this generates ArrayIndexOutOfBoundsException.public class ExceptionExample {    public static void main(String[] args) {       //Creating an integer array with size 5       int inpuArray[] = ... Read More

What is InputMisMatchException in Java how do we handle it?

Maruthi Krishna
Updated on 07-Aug-2019 10:47:22

4K+ Views

From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions.To read various datatypes from the source using the nextXXX() methods provided by this class namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), next().Whenever you take inputs from the user using a Scanner class. If the inputs passed doesn’t match the method or an InputMisMatchException is thrown. For example, if you reading an integer data using the nextInt() method and the value ... Read More

What changes has been introduced in JDK7 related to Exception handling in Java?

Maruthi Krishna
Updated on 07-Aug-2019 09:17:57

59 Views

Since Java 7 try-with resources was introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)The resources we declare in the try block should extend the java.lang.AutoCloseable class.ExampleFollowing program demonstrates the try-with-resources in Java.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying {    public static void main(String[] args) {       try(FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt"));       FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))){          byte[] buffer = new byte[1024];     ... Read More

Advertisements