Java Articles

Page 11 of 450

How to parse date sting to date in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 2K+ Views

You can parse a string containing a data to date value using the following ways −The constructor SimpleDateFormat class accepts a String value representing the desired date format and creates this object. You can parse the date string using the parse() method of this class.The parse() method of the LocalDate class accepts a String value representing a date and returns a LocalDate object.The DateUtils provides utility to format date you can find it in apache.commons package. The parseDate() method of the DateUtils class accepts a format string and a date string as parameters and returns a Date object.The parse() method of ...

Read More

How to print date using GregorianCalendar class in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

The GregorianCalendar class supports standard calendars it supports Julian and Gregorian calendars you can create an object of GregorianCalendar using one of its constructors. Following are various examples demonstrating how to print date using this class −ExampleFollowing example creates GregorianCalander by passing year, month and date values as parameters to its constructor and prints the date −import java.util.Calendar; import java.util.GregorianCalendar; public class Test {    public static void main(String args[]){       //Instantiating the GregorianCalendar       GregorianCalendar cal = new GregorianCalendar(2018, 6, 27);       System.out.println(cal);       System.out.println("Date: "+cal.get(Calendar.DATE));       System.out.println("Month: "+cal.get(Calendar.MONTH)); ...

Read More

Can we define an enum inside a class in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 17K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntaxenum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }Yes, we can define an enumeration inside a class. You can retrieve the values in an enumeration using the values() method.Examplepublic class EnumerationExample {    enum Enum {       Mango, Banana, Orange, Grapes, Thursday, Apple    }    public static void main(String args[]) {       Enum constants[] = Enum.values();       System.out.println("Value of constants: ");         for(Enum d: constants) {          System.out.println(d); ...

Read More

How to format time using printf() method in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 277 Views

The printf() method is used to print a formatted string, it accepts a string representing a format string and an array of objects representing the elements that are to be in the resultant string, if the number of arguments are more than the number of characters in the format string the excess objects are ignored.Following table lists the various format characters to print date printf() method along with their description −Format CharactersDescription'B'The corresponding argument is formatted as full month name.'b'The corresponding argument is formatted as abbreviated month name.'h'The corresponding argument is formatted as abbreviated month name.'A'The corresponding argument is formatted as ...

Read More

Can we define an enum inside a method in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

Enumerations in Java represents a group of named constants, you can create an enumeration using the following syntax −enum Days {    SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY }We can an enumeration inside a class. But, we cannot define an enum inside a method. If you try to do so it generates a compile time error saying “enum types must not be local”.Examplepublic class EnumExample{    public void sample() {       enum Vehicles {          Activa125, Activa5G, Access125, Vespa, TVSJupiter;       }    } }ErrorEnumExample.java:3: error: enum types must not be local ...

Read More

How to use formatting with printf() correctly in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 382 Views

The printf() method is used to print a formatted string, it accepts a string representing a format string and an array of objects representing the elements that are to be in the resultant string, if the number of arguments are more than the number of characters in the format string the excess objects are ignored.Following table lists the various format characters to format time by the Java printf() method along with their description −Format CharactersDescription'H'The corresponding argument is formatted as Hour of the day (00-24).'I'The corresponding argument is formatted as hour of the day (01 -12).'k'The corresponding argument is formatted ...

Read More

How to convert a double value into a Java String using append method?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 333 Views

The append method of the StringBuilder or StringBuffer objects accept a boolean or, char or, char array or, double or, float or, int or, long or, String value as parameter and adds it to the current object.You can append the required double value to the method and retrieve a String from obtained StringBuffer (or, StringBuilder) objects.Exampleimport java.util.Scanner; public class ConversionOfDouble {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a double value:");       double d = sc.nextDouble();       StringBuffer sb = new StringBuffer();       sb.append(d);   ...

Read More

How to measure elapsed time in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

In general, the elapsed time is the time from the starting point to ending point of an event. Following are various ways to find elapsed time in Java −Using the currentTimeMillis() methodThe currentTimeMillis() method returns the current time in milliseconds. To find the elapsed time for a method you can get the difference between time values before and after the execution of the desired method.Examplepublic class Example {    public void test(){ int num = 0;       for(int i=0; i

Read More

What are date temporal fields in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 3K+ Views

A temporal field is a field of date-time, such as month-of-year or hour-of-minute. These fields are represented by the TemporalField interface and the ChronoField class implements this interface.Following are the list of various temporal fields regarding date supported by the ChronoField class −FieldDescriptionALIGNED_DAY_OF_WEEK_IN_MONTHThis field represents the day of the week with in a month.ALIGNED_DAY_OF_WEEK_IN_YEARThis field represents the aligned day of a week in an year.ALIGNED_WEEK_OF_MONTHThis field represents the aligned wee of a month.ALIGNED_WEEK_OF_YEARThis field represents the aligned week of an year.DAY_OF_MONTHThis field represents the day of a month.DAY_OF_WEEKThis field represents the day of a week.DAY_OF_YEARThis field represents the day of ...

Read More

Can we override default methods in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

An interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since Java8 static methods and default methods are introduced in interfaces. Unlike other abstract methods these are the methods can have a default implementation. If you have default method in an interface, it is not mandatory to override (provide body) it in the classes that are already implementing this interface.In short, you can access the default methods of an interface using the objects of the implementing classes.Exampleinterface MyInterface{      public static int num = 100;    public default void ...

Read More
Showing 101–110 of 4,495 articles
« Prev 1 9 10 11 12 13 450 Next »
Advertisements