Remove Leading Zeroes from a String in Java

Venkata Sai
Updated on 29-Jun-2020 14:48:21

3K+ Views

Whenever you read an integer value into a String, you can remove leading zeroes of it using StringBuffer class, using regular expressions or, by converting the given String to character array.Converting to character arrayFollowing Java program reads an integer value from the user into a String and removes the leading zeroes from it by converting the given String into Character array.Exampleimport java.util.Scanner; public class LeadingZeroes {    public static String removeLeadingZeroes(String num){       int i=0;       char charArray[] = num.toCharArray();       for( ; i

Convert Date Object to LocalDate Object in Java

Narasimha Murthi
Updated on 29-Jun-2020 14:46:51

1K+ Views

To convert a Date object to LocalDate object in Java −Convert the obtained date object to an Instant object using the toInstant() method.Instant instant = date.toInstant();Create the ZonedDateTime object using the atZone() method of the Instant class.ZonedDateTime zone = instant.atZone(ZoneId.systemDefault());Finally, convert the ZonedDateTime object to the LocalDate object using the toLocalDate() method.LocalDate givenDate = zone.toLocalDate();ExampleFollowing example accepts a name and Date of birth from the user in String formats and converts it to LocalDate object and prints it. Live Demoimport java.text.ParseException; import java.text.SimpleDateFormat; import java.time.Instant; import java.time.LocalDate; import java.time.ZoneId; import java.time.ZonedDateTime; import java.util.Date; import java.util.Scanner; public class DateToLocalDate {    public static void ... Read More

Declare an Interface Inside a Class in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:45:53

887 Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or, make a contract specifying how the methods and fields of a type should be you can define an interface.To create an object of this type you need to implement this interface, provide body for all the abstract methods of the interface and obtain the object of the implementing class.Nested interfacesJava allows writing/declaring interfaces within another interface or, within a class these are known as nested interfaces.ExampleIn the following Java example, we have a class with name Sample which contains a nested interface ... Read More

Can We Write an Interface Without Any Methods in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:44:24

2K+ Views

Yes, you can write an interface without any methods. These are known as marking interfaces or, tagging interfaces.A marker interface i.e. it does not contain any methods or fields by implementing these interfaces a class will exhibit a special behavior with respect to the interface implemented.ExampleConsider the following example, here we have class with name Student which implements the marking interface Cloneable. In the main method we are trying to create an object of the Student class and clone it using the clone() method. Live Demoimport java.util.Scanner; public class Student implements Cloneable {    int age;    String name;    public ... Read More

What is a Remote Interface in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:43:24

4K+ Views

A Remote interface is available in the java.rmi package it is a marking/tagging interface, it is used with remote method invocation(RMI).RMI is a mechanism that allows an object residing in one system (JVM) to access/invoke an object running on another JVM.To it is a marking interface, to mark an object of a class remote, you need to implement this interface.To create a remote interface −Create an interface that extends the predefined interface Remote which belongs to the package or, implement the Remote interface with the class, which you need to make remote.Declare all the business methods that can be invoked ... Read More

Format String to Date as DD-MM-YYYY in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:42:48

17K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).Using the methods of this class you can parse String to Date or, format Date to String.Parsing String to DateYou can parse a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To parse a String to Date object −Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");Parse/convert ... Read More

Find Age When Date of Birth is Known Using Java

Narasimha Murthi
Updated on 29-Jun-2020 14:41:59

5K+ Views

Java provides a class named Period in the java.time package. This is used to calculate the time period between two given dates as days, months and, years etc.The between() method of this class accepts two LocalDate objects and finds out the period between the two given dates (number of years, months, and days) and returns as a period object.From this, you can extract the number of days, months and, years of the period in between using the getDays(), getMonths() and, getYears() respectively.Finding the ageIf you already have the date of birth of a person, to find the age −Get the ... Read More

Format Date to String in Java

Narasimha Murthi
Updated on 29-Jun-2020 14:41:19

2K+ Views

The java.text package provides a class named SimpleDateFormat which is used to format and parse dates in required manner (local).Using the methods of this class you can parse String to Date or, format Date to String.Formatting Date to StringYou can format a given String to Date object using the parse() method of the SimpleDateFormat class. To this method you need to pass the Date in String format. To format a String to Date object −Instantiate the SimpleDateFormat class by passing the required pattern of the date in String format to its constructor.//Instantiating the SimpleDateFormat class SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");Format/convert the ... Read More

What Happens If the Subclass Does Not Override Abstract Methods in Java

Maruthi Krishna
Updated on 29-Jun-2020 14:39:48

6K+ Views

A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.A class which contains 0 or more abstract methods is known as abstract class. If it contains at least one abstract method, it must be declared abstract.Extending an abstract classOnce you extend an abstract class in Java you need to override all the abstractmethods in it or, declare it abstract. If you don’t, a ... Read More

Declare Variables of a Java Interface as Private and Protected

Maruthi Krishna
Updated on 29-Jun-2020 14:37:41

4K+ Views

Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Private fields of an interfaceIf the fields of the interface are private, you cannot access them in the implementing class.If you try to declare the fields of an interface private, a compile time error is generated saying “modifier private not allowed here”.ExampleIn the following Java example, we are trying to declare the field and method of an interface private.public interface MyInterface{    private static final int num = 10;    private abstract void demo(); }Compile time errorOn compiling, the above program ... Read More

Advertisements