Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 2 of 50

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

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 284 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 390 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 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

What is Java reg ex to check for date and time?

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

To match a regular expression with the given string You need to:.Compile the regular expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.The matches() method of the Matcher class returns true if a match occurs else it returns false. Therefore, invoke this method to validate the data.ExampleFollowing is a Java regular expression example matches only dateimport java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Sample {    public static void main(String args[]){       //Creating the list to store ...

Read More

How to create Directories using the File utility methods in Java?

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

Since Java 7 the File.02s class was introduced this contains (static) methods that operate on files, directories, or other types of files.The createDirectory() method of the Files class accepts the path of the required directory and creates a new directory.ExampleFollowing Java example reads the path and name of the directory to be created, from the user, and creates it.import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Scanner; public class Test {    public static void main(String args[]) throws IOException {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);       ...

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

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 convert a double value into a Java String using append method?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 340 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
Showing 11–20 of 500 articles
« Prev 1 2 3 4 5 50 Next »
Advertisements